首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将i18next与redux集成

将i18next与redux集成
EN

Stack Overflow用户
提问于 2018-04-02 13:50:00
回答 1查看 852关注 0票数 0

我正在尝试从这两个选项中的一个检测区域设置: 1.如果用户选择了一个应用程序,至少打开一次;如果应用程序第一次打开,则为第一次使用设备区域设置。

我尝试使用这个指南和这个代码I18下一反应本机语言检测器。但没有人帮忙。我的i18n.js文件看起来如下:

代码语言:javascript
复制
import i18next from 'i18next';
import { AsyncStorage } from 'react-native';
(*) // import i18nextReactNative from 'i18next-react-native-language-detector';  
import locale from 'react-native-locale-detector';
import en from './en';
import de from './de';

const languageDetector = {
    init: Function.prototype,
    type: 'languageDetector',
    async: true, // flags below detection to be async
    detect: () => AsyncStorage.getItem('APP:lnag')
      .then((savedDataJSON) => {
        const savedLocal = JSON.parse(savedDataJSON);
        const selectLanguage = savedLocal || locale;
        return selectLanguage;
    }),
    cacheUserLanguage: Function.prototype,
};

let translate;
i18next
    (*)//.use(i18nextReactNative)
    (**).use(languageDetector)
    .init({
        fallbackLng: 'en',
        resources: {
            en,
            de,
        },
        react: {
            wait: true,
        },
        // have a common namespace used around the full app
        ns: ['common'],
        defaultNS: 'common',
        debug: true,
        interpolation: {
            escapeValue: false, // not needed for react!!
            formatSeparator: ',',
            format(value, format) {
                if (format === 'uppercase') return value.toUpperCase();
                return value;
            },
        },
    }, (err, t) => {
        translate = t;
    });
export { translate as t };
export default i18next;

但我得到了一个错误:TypeError: (0, _18n.t) is not a function

当我使用默认的languageDetector时,从(*)行中删除注释,并注释自定义的languageDetector --它工作正常,但不是我想要的--总是采用设备区域设置

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-03 06:14:32

我找到了一个解决办法:

代码语言:javascript
复制
import i18next from 'i18next';
import { AsyncStorage } from 'react-native';
import locale from 'react-native-locale-detector';
import en from './en';
import de from './de';

const languageDetector = {
    init: Function.prototype,
    type: 'languageDetector',
    async: true, // flags below detection to be async
    detect: async (callback) => {
        const savedDataJSON = await AsyncStorage.getItem(STORAGE_KEY);
        const lng = (savedDataJSON) ? JSON.parse(savedDataJSON): null;
        const selectLanguage = lng || locale;
        console.log('detect - selectLanguage:', selectLanguage);
        callback(selectLanguage);
    },
    cacheUserLanguage: () => {}
}

let translate;
i18next
    .use(languageDetector)
    .init({
        fallbackLng: 'en',
        resources: { en, de},
        react: { wait: false },
        // have a common namespace used around the full app
        ns: ['common'],
        defaultNS: 'common',
        debug: true,
        interpolation: {
            escapeValue: false, // not needed for react!!
            formatSeparator: ',',
            format(value, format) {
            if (format === 'uppercase') return value.toUpperCase();
                return value;
            },
       },
    }, (err, t) => {
        translate = t;
});
export { translate as t };
export default i18next;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49612572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档