import { BASE_URL } from '@env'
i18n
i18n module provides with tools to localize your product.
Requirements
@startupjs/ui: >= 0.34.0
axios: *
express: 4.x
react: 16.9 - 17
react-native: >= 0.61.4 < 0.64.0
startupjs: >= 0.34.0
Installation & basic setup
- Install the module
The module does not need to be installed, it comes with the startupjs
module.
- Init module on server side
import { initI18n } from '@startupjs/i18n/server'
startupjsServer({
}, (ee, options) => {
initI18n(ee)
})
- Init the isomorphic model
import initI18nModel from 'startupjs/i18n/model'
export default function initOrm (racer) {
initI18nModel(racer, i18nPageAccess)
}
where i18nPageAccess(model, next, redirect)
is the /i18n
page access filter.
- Create
i18n/index.js
file and initialize the client-size app on it
import { initI18n, useI18nGlobalInit } from '@startupjs/i18n'
export default initI18n({
lang: 'ru',
langs: ['en', 'ru'],
getLangs: () => {
const langs = ...
return langs
},
languageDetector: () => {
return $user.get('language')
}
})
export { useI18nGlobalInit }
- Add client-side app to
Root/App.js
import React from 'react'
import i18n, { useI18nGlobalInit } from 'PATH_TO_I18N_FILE_CREATED_IN_SECTION_ABOVE'
return pug`
App(
apps={ i18n }
useGlobalInit=useI18nGlobalInit
)
`
How to change the language ?
import { observer } from 'statupjs'
import { useLang, useLangs } from 'startupjs/i18n'
import { Select } from '@startupjs/ui'
export default observer(function LanguageSelect () {
const [lang, setLang] = useLang()
const langs = useLangs()
return pug`
Select(
value=lang
onChange={setLang}
options=langs
)
`
})
Working with translations
To work correctly with translations, you need to use the function - t
import React from 'react'
import { t } from 'startupjs'
import { Span } from '@startupjs/ui'
export default function MyPage () {
return pug`
Div
Span= t('backToHome', 'Back to Home')
`
}
The first argument is the name of the tag for translations, the second is the default
Initially, words are used only for the default language, the rest of the words of other languages can be changed on the page - ${BASE_URL}/i18n
Depending on the selected language, the t
function will match the value in the layout