2025-04-12- translation: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
=Translation Microfrontend= | =Translation Microfrontend= | ||
==i18n== | |||
<syntaxhighlight lang="Javascript"> | |||
i18n | |||
.use(Backend) // load translation using xhr -> see /public/locales. We will add locales in the next step | |||
.use(LanguageDetector) // detect user language | |||
.use(initReactI18next) // pass the i18n instance to react-i18next. | |||
.init({ | |||
fallbackLng, // if user computer language is not on the list of available languages, than we will be using the fallback language specified earlier | |||
debug: true, | |||
lng: "en", | |||
// whitelist: availableLanguages, | |||
interpolation: { | |||
escapeValue: false, | |||
}, | |||
react: { | |||
useSuspense: false, | |||
}, | |||
backend: { | |||
loadPath: | |||
"https://translations-language.s3.us-east-2.amazonaws.com/locales/{{lng}}/{{ns}}.json", | |||
crossDomain: true, | |||
allowMultiLoading: true, | |||
}, | |||
ns,//get all translated word in namespace follow by list of ns then store it in i18n.store.data | |||
defaultNS: "buying", //use this namespace first | |||
}); | |||
</syntaxhighlight> | |||
==function== | ==function== | ||
===MyTranslation=== | ===MyTranslation=== | ||
| Line 5: | Line 33: | ||
format | format | ||
<syntaxhighlight lang="Javascript"> | <syntaxhighlight lang="Javascript"> | ||
//for use defaultNS only if reference word come frome another namespace that not set in defaultNS reference word will not found. | |||
MyTranslation("reference_word") | |||
//if want to use reference word in another namespace but namespace must have to set in ns. | |||
MyTranslation("nameSpace_name:reference_word") | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 44: | Line 75: | ||
=Translation NPM Library= | =Translation NPM Library= | ||
*don't need to do anything with this library | |||
[[Category:Working documents| 2025-04-12]] | |||
[[Category:Working documents - translation| 2025-04-12]] | |||
Latest revision as of 00:35, 18 April 2025
Translation Microfrontend
i18n
i18n
.use(Backend) // load translation using xhr -> see /public/locales. We will add locales in the next step
.use(LanguageDetector) // detect user language
.use(initReactI18next) // pass the i18n instance to react-i18next.
.init({
fallbackLng, // if user computer language is not on the list of available languages, than we will be using the fallback language specified earlier
debug: true,
lng: "en",
// whitelist: availableLanguages,
interpolation: {
escapeValue: false,
},
react: {
useSuspense: false,
},
backend: {
loadPath:
"https://translations-language.s3.us-east-2.amazonaws.com/locales/{{lng}}/{{ns}}.json",
crossDomain: true,
allowMultiLoading: true,
},
ns,//get all translated word in namespace follow by list of ns then store it in i18n.store.data
defaultNS: "buying", //use this namespace first
});
function
MyTranslation
- send reference word to i18n for output translated word follow by current using language
format
//for use defaultNS only if reference word come frome another namespace that not set in defaultNS reference word will not found.
MyTranslation("reference_word")
//if want to use reference word in another namespace but namespace must have to set in ns.
MyTranslation("nameSpace_name:reference_word")
changeLanguage
- set new language
example
changeLanguage("jp");
S3
- Store translations of each language in their respective directory.
S3 tree
/translations-language
/locales
/{language_name}
-{namespace_name}.json
.json file
- key = reference word
- value = translated word
{
"greeting": "Hello",
"text": "Thank you for visiting our website.",
"language": "Language",
"Hi": "What's up",
"Hello": "greeting from translation"
}
Object link
- format
"https://translations-language.s3.us-east-2.amazonaws.com/locales/Template:Lng/Template:Ns.json"
- example
"https://translations-language.s3.us-east-2.amazonaws.com/locales/en/buying.json"
Translation NPM Library
- don't need to do anything with this library