Skip to content

Commit

Permalink
Add support for returnObjects: true (#172)
Browse files Browse the repository at this point in the history
* Add support for returnObjects: true

* changeset
  • Loading branch information
ryanwilsonperkin authored Sep 8, 2023
1 parent 673be57 commit 75ba1dc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-eels-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/i18next-shopify': patch
---

Allow t() to accept returnObjects: true
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class ShopifyFormat {
parse(res, options) {
// const hadSuccessfulLookup = info && info.resolved && info.resolved.res;

// returnObjects parameter can cause objects to be resolved, rather than a single string
if (typeof res === 'object') {
return res;
}

// Interpolations
const matches = res.match(MUSTACHE_FORMAT);
if (!matches) {
Expand Down
12 changes: 12 additions & 0 deletions test/shopify_format_with_react_i18next.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ describe('shopify format with react-i18next (t)', () => {
other: 'This is my {{ordinal}}th car',
},
},
nested_level_1: {
nested_level_2: 'Nested content',
},
},
},
},
Expand Down Expand Up @@ -163,6 +166,15 @@ describe('shopify format with react-i18next (t)', () => {
}),
).toBe('This is my 2st car');
});

it('handles returnObjects: true', () => {
const {result} = renderHook(() => useTranslation('translation'));
const {t} = result.current;

expect(t('nested_level_1', {returnObjects: true})).toStrictEqual({
nested_level_2: 'Nested content',
});
});
});

describe('with react-i18next (Trans)', () => {
Expand Down

0 comments on commit 75ba1dc

Please sign in to comment.