This small library implements an example backend for i18next which retrieves the translations from Phrase OTA releases. The distribution should be created for i18next platform.
A demo project can be found at https://github.com/phrase/ota-web-demo
npm install --save @phrase/i18next-backend
import i18n from "i18next";
import { I18nextPhraseBackend } from "@phrase/i18next-backend";
i18n
.use(I18nextPhraseBackend)
.init({
fallbackLng: 'en',
backend: {
distribution: 'DISTRIBUTION_ID',
secret: 'YOUR_ENVIRONMENT_SECRET',
appVersion: '1.0.0',
}
});The backend accepts several options:
distribution(required): The ID of your Phrase Strings OTA distributionsecret(required): The secret token of your Phrase Strings OTA distribution. There are different secrets fordevelopment(i.e. beta) andproductionenvironmentsappVersion: You can prevent some OTA releases from being serverd to certain app versions by providing minimum and maximum app version in Phrase StringscacheExpirationTime: See Caching belowhost: By default, this library uses EU instance of Phrase Strings, if you use US DC, set this tohttps://ota.us.phrase.comformat: By default this library uses the i18next format, if you use i18next v4 (for the new pluralization resolution strategy), set this toi18next_4
The library is caching translations and won't check for new translations for 5 minutes. This can be configured by setting the cacheExpirationTime option in the backend configuration for testing purposes. It's recommended to use at least 5 minutes in production.
i18n
.use(I18nextPhraseBackend)
.init({
fallbackLng: 'en',
backend: {
distribution: 'DISTRIBUTION_ID',
secret: 'YOUR_ENVIRONMENT_SECRET',
appVersion: '1.0.0',
cacheExpirationTime: 60 * 5, // time in seconds
}
});