如何从 en.json 中删除翻译密钥



我不想在 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;

从 JSON 中删除翻译并尝试。默认情况下,它应该可以工作。如果未设置。

fallbackNS= false; 

如果 STill 不起作用。创建一个新的 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",
   }
}

希望对您有所帮助.. :)

最新更新