Skip to content

Commit

Permalink
refactor: change method to add languages
Browse files Browse the repository at this point in the history
  • Loading branch information
brunomous committed Jun 28, 2024
1 parent 03008ea commit cec1efa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 0 additions & 3 deletions packages/react-material-ui/src/utils/i18n/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ Adds a new language to the i18n resource list.
| Name | Type | Description |
| --- | --- | --- |
| lng | `string` | The name of the language that will have the strings appended. |
| ns | `string` | Namespace to which the strings will belong. |
| resources | `object` | The object containing string and their correspondent values. |
| deep | `boolean` | Indicates if the language bundle should be appended to an existing one of the same name. Default to false. |
| overwrite | `boolean` | Indicates if the language bundle should overwrite an existing one with the same name. Default to false. |

```tsx
"use client";
Expand Down
9 changes: 8 additions & 1 deletion packages/react-material-ui/src/utils/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import LanguageDetector from 'i18next-browser-languagedetector';

import locales from './locales';

const namespaces = Object.keys(locales.en_US);

i18next
.use(LanguageDetector)
.use(initReactI18next)
.init({
fallbackLng: 'en-US',
debug: true,
ns: ['translation', ...namespaces],
resources: {
'en-US': locales.en_US,
'pt-BR': locales.pt_BR,
Expand All @@ -24,6 +27,10 @@ const reactI18n = getI18n();
const isInitialized = reactI18n['isInitialized'];

const appendStrings = reactI18n['addResources'];
const addLanguage = reactI18n['addResourceBundle'];
const addLanguage = (lng: string, resources: any) => {
namespaces.forEach((namespace) => {
reactI18n.addResourceBundle(lng, namespace, resources[namespace], true);
});
};

export { useTranslation, reactI18n, isInitialized, appendStrings, addLanguage };

0 comments on commit cec1efa

Please sign in to comment.