Localization for ISO 639 language codes. We support the Set 1
abbreviation codes from https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes.
This package is heavily dependent on i18next, iso-639-1 and setup to run in a Vite project.
- No Context or global state needed
- Single-point setup
- Use your own locales
- 2-step process to support more languages
Install through npm.
npm i react-iso-localization
This package accepts files like en.json
for English, el.json
for Greek, ...
It was developed with the files exported from POEditor (or similar tools) in the Key-Value JSON
format in mind.
Let's say we have 3 JSON files.
-
en.json
{ "HELLO_WORLD": "Hello world!" }
-
el.json
{ "HELLO_WORLD": "Γεια σου, κόσμε!" }
-
ro.json
{ "HELLO_WORLD": "Bună ziua lume!" }
For the sake of this documentation, I save all locale files in the directory
/src/resources/locales/
.
Initialize the library inside App.js
/ App.jsx
/ App.ts
/ App.tsx
import { setupI18n } from 'react-iso-localization';
import el from 'resources/locales/el.json';
import en from 'resources/locales/en.json';
import ro from 'resources/locales/ro.json';
setupI18n({
el: { translation: el },
en: { translation: en },
ro: { translation: ro }
});
export const App = () => {
...
};
import React from 'react';
import { useI18n } from 'react-iso-localization';
const TestComponent = (): React.JSX.Element => {
const i18n = useI18n();
return (
<React.Fragment>
{i18n.get('HELLO_WORLD')}
</React.Fragment>
);
};
export default TestComponent;
import React from 'react';
import { useI18n } from 'react-iso-localization';
const SetToGreek = (): React.JSX.Element => {
const i18n = useI18n();
const onClick = () => i18n.set('el');
// const onClick = () => i18n.set('xx'); // throws error
return (
<button onClick={onClick}>
Change language
</button>
);
};
export default SetToGreek;
-
locale
propertyThe currently active language code.
-
languages
propertyThe languages registered to your app during Setup
-
get(string)
methodThis method accepts a locale's key and returns the value in the selected language.
E.g.
console.log(i18n.get('HELLO_WORLD')); // Returns Γεια σου, κόσμε!
-
getNoAccents(string)
methodThis method accepts a locale's key and returns the value with the accents removed in the selected language.
E.g.
console.log(i18n.get('HELLO_WORLD')); // Returns Γεια σου, κοσμε!
-
getCapitalized(string)
methodThis method accepts a locale's key and returns the value with the accents removed in the selected language.
E.g.
console.log(i18n.get('HELLO_WORLD')); // Returns ΓΕΙΑ ΣΟΥ, ΚΟΣΜΕ!
-
set(string)
methodIt accepts a language code (e.g.
'en'
for English) and sets the respective language to your app as active. Also, it saves that language code to the local storage.For more, see Change language.
-
defaultLanguage
propertyThis property is used for the library to determine the language used when starting the app.
It uses 3 parameters with different priority for each:
locale
item in the browser's local storageVITE_DEFAULT_LOCALE
variable in your app's environment- The system's language
First, it checks in the local storage for the user's preference. Then, if that is not defined, the library gets the default language from the environment variables. This is useful for applications that want all users to have a certain language as default. Lastly, if nothing is set in the above instances, the user's system's language is used.
-
getShortNativeName(string)
methodThis method simply returns the 2 first characters of a language's native name. E.g.
ΕΛ
for Greek,EN
for English, ...import { getShortNativeName } from 'react-iso-localization'; console.log(getShortNativeName('el')) // Returns ΕΛ
Uses
iso-639-1
'sgetNativeName
method in the background. -
getNativeName(string)
methodIt's simply a wrapper for
iso-639-1
'sgetNativeName
method.Returns
Ελληνικά
for Greek,English
for English, ...import { getNativeName } from 'react-iso-localization'; console.log(getNativeName('el')) // Returns Ελληνικά
-
setupI18n
methodIt does exactly what it says it does. It sets up the whole library.
It accepts an object with all your locales. For each locale, use the language's code as key and
translation: <locales>
as value.For more, see Setup.
This package was created reading TypeScript NPM Package Publishing: A Beginner’s Guide.