Language Detector plugins are used to detect language in user land.
{
type: 'languageDetector',
async: true, // If this is set to true, your detect function receives a callback function that you should call with your language, useful to retrieve your language stored in AsyncStorage for example
init: function(services, detectorOptions, i18nextOptions) { // optional since v22.3.0
/* use services and options */
},
detect: function(callback) { // You'll receive a callback if you passed async true
/* return detected language */
// callback('de'); if you used the async flag
return 'de';
},
// or new since v22.3.0
// detect: async function () { // you can also return a normal Promise
// /* return detected language */
// return 'de';
// // or
// // return Promise.resolve('de')
// },
cacheUserLanguage: function(lng) { // optional since v22.3.0
/* cache language */
}
// or new since v22.3.0, but i18next will not await for it... so it's basically a fire and forget
// cacheUserLanguage: async function(lng) {
// /* await cache language */
// }
}
post processor
Post Processors are used to extend or manipulate the translated values before returning them in t function.
(Post Processors do not need to be prototype functions)
If you do not set the plugin type, you may get an error like this.
... No [plugin type] was added via i18next.use. Will not load resources.
If you are creating a class for your plugin, you may set the type like in the following example (the following is an example if you are making a backend plugin):
class Backend {
constructor(services, backendOptions, i18nextOptions){
}
// other required methods;
// ie. read, create, etc.
}
Backend.type = "backend";
export default Backend;
Create a private method to initialize your plugin
The constructor of your plugin (if the plugin is of type backend or languageDetector) will be called without arguments if you use the plugin as a class. Using the plugin as a class looks like this:
import i18n from "i18next";
import {
initReactI18next
} from "react-i18next";
import i18nBackend from "my-custom-backend";
i18n
.use(i18nBackend)
.use(initReactI18next)
.init({
backend: {
// custom options
},
// other options
});
While using your plugin in this way, you may want to validate the options passed into the backend property of the .init method. A good way to validate them is to have a private method where you initialize your plugin.