From 75ba1dcc755a945af4fec92dca178b0c4c6acdaa Mon Sep 17 00:00:00 2001 From: Ryan Wilson-Perkin Date: Fri, 8 Sep 2023 10:39:59 -0400 Subject: [PATCH] Add support for returnObjects: true (#172) * Add support for returnObjects: true * changeset --- .changeset/thirty-eels-hang.md | 5 +++++ src/index.js | 5 +++++ test/shopify_format_with_react_i18next.spec.js | 12 ++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 .changeset/thirty-eels-hang.md diff --git a/.changeset/thirty-eels-hang.md b/.changeset/thirty-eels-hang.md new file mode 100644 index 0000000..d50220a --- /dev/null +++ b/.changeset/thirty-eels-hang.md @@ -0,0 +1,5 @@ +--- +'@shopify/i18next-shopify': patch +--- + +Allow t() to accept returnObjects: true diff --git a/src/index.js b/src/index.js index df73ea4..5b2d4ad 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { diff --git a/test/shopify_format_with_react_i18next.spec.js b/test/shopify_format_with_react_i18next.spec.js index 9e44f63..bc9dae2 100644 --- a/test/shopify_format_with_react_i18next.spec.js +++ b/test/shopify_format_with_react_i18next.spec.js @@ -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', + }, }, }, }, @@ -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)', () => {