【问题标题】:How can I remove the translation key from en.json?如何从 en.json 中删除翻译键?
【发布时间】:2018-04-26 12:28:22
【问题描述】:

我不想在 json 文件中使用翻译键。 我希望文件只有 2 个级别,我的意思是我不想在每个语言环境文件中包含翻译键:

{
  "translation": {
    "sidebar": {
      "dashboard": "Dashboard",
      "clients": "Clients",
      "coaches": "Coaches",
      "candidates": "Candidates",
      "trackList": "Track List",
      "reports": "Reports"
    },
    "clients": {
      "clients": "Clients",
      "companyName": "Company Name",
      "previous": "Previous",
      "next": "Next"
    }
  }
}

这是我初始化 i18next 的 i18n.js 文件。

import angular from 'angular';
import ngSanitize from 'angular-sanitize';
import i18next from 'i18next';
import ngI18next from 'ng-i18next';
import enLocale from './locales/en.json'

const module = angular.module('app.i18n', [ngSanitize, ngI18next]);

module
  .config(function() {

    window.i18next = i18next;

    i18next.init({
      debug: true,
      lng: 'en',
      fallbackLng: false,
      resources: {
        en: enLocale
      },
      useCookie: false,
      useLocalStorage: false
    }, function(err, t) {
      console.log('i18next was initialized');
      console.log( t('global.clients') );
      console.log( t('global.addNewClient') );
    });

});
export default module.name;

【问题讨论】:

    标签: angularjs webpack i18next


    【解决方案1】:

    从您的 Json 中删除翻译并尝试。它应该默认工作。 如果没有设置。

    fallbackNS= false; 
    

    如果仍然不起作用。创建一个新的 Json 变量以将其传递给资源。

    import enLocale from './locales/en.json'
    const enLocaleData = {"en":{"transalation":enLocale["en"] }};
    
     i18next.init({
          debug: true,
          lng: 'en',
          resources: {
            en: enLocaleData 
          });
    

    这样,你就可以传递两层的json了。

    {
    "en":
       {
        "key":"keyValue",
        "key2":"keyValue2",
       }
    }
    

    希望对你有帮助.. :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-16
      • 2011-07-04
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多