Skip to content

Commit

Permalink
0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
QuiiBz committed Aug 20, 2022
1 parent c49e353 commit 0f1fa12
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"next": "12.2.2",
"next-international": "workspace:0.2.0",
"next-international": "workspace:0.3.0",
"react": "18.2.0",
"react-dom": "18.2.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/international-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "international-types",
"version": "0.2.0",
"version": "0.3.0",
"description": "Type-safe internationalization (i18n) utility types",
"types": "dist/index.d.ts",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions packages/next-international/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-international",
"version": "0.2.0",
"version": "0.3.0",
"description": "Type-safe internationalization (i18n) for Next.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -36,6 +36,6 @@
"react": "^18.0.0 || ^17.0.0"
},
"dependencies": {
"international-types": "0.2.0"
"international-types": "0.3.0"
}
}
27 changes: 13 additions & 14 deletions packages/next-international/src/i18n/create-use-i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, Context } from 'react';
import React, { useContext, Context, isValidElement, cloneElement, ReactNode } from 'react';
import type {
BaseLocale,
LocaleKeys,
Expand Down Expand Up @@ -45,29 +45,28 @@ export function createUsei18n<Locale extends BaseLocale>(I18nContext: Context<Lo
}

let isString = true;

const result = value.split(/({[^}]*})/).map((part, index) => {
const match = part.match(/{(.*)}/);

if (match) {
const param = match[1] as keyof Locale;
const paramValue = (paramObject as LocaleMap<Locale>)[param];
if (typeof paramValue !== 'string') {
isString = false;
}
if (React.isValidElement(paramValue)) {
return React.cloneElement(paramValue, { key: `${String(param)}-${index}` });
} else {
return paramValue as React.ReactNode;
}

if (typeof paramValue !== 'string') isString = false;

return isValidElement(paramValue)
? cloneElement(paramValue, { key: `${String(param)}-${index}` })
: (paramValue as ReactNode);
}

// if there's no match - it's not a variable and just a normal string
return part;
});
if (isString) {
return result.join('');
} else {
return result;
}

return isString ? result.join('') : result;
}

return t;
}

Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0f1fa12

Please sign in to comment.