简体   繁体   English

角翻译。 使用存储在服务器而非客户端上的json文件实现i18n的最佳方法是什么

[英]angular-translate. What is the best way to implement i18n using json files that are stored on the server, not the client

Implementing i18n isn't very difficult when you have a list of json documents stored on the client. 当您在客户端上存储了json文档列表时,实现i18n并不是很困难。 But what if you can't have the json files stored locally? 但是,如果您无法将json文件存储在本地怎么办? They have to be retrieved at runtime. 必须在运行时检索它们。 What is the best way to achieve this. 实现此目标的最佳方法是什么。

I thought about storing them in sessionStorage as they are required but how do I get angular-translate to work with that or something similar? 我考虑过按需要将它们存储在sessionStorage中,但是如何使它们进行角度平移以进行处理?

When I work with local files I simply point to their location: 当我使用本地文件时,我只是指向它们的位置:

urlTemplate: 'assets/translation/{lang}/{part}.json'

I don't know if this is the best way, but I have been using the static files loader from angular-translate for this. 我不知道这是否是最好的方法,但是我一直在使用angular-translate的静态文件加载器。

Let's say you have a folder full of translation files: 假设您有一个充满翻译文件的文件夹:

locales/locale-de_DE.json 
locales/locale-en_GB.json 
locales/locale-nl_BE.json 
...

Then in your app, you would do this: 然后在您的应用中,您将执行以下操作:

var app = angular.module('myApp', [ ]);

app.config([ '$translateProvider',
  function($translateProvider) {
    $translateProvider.useStaticFilesLoader({
      prefix: 'locales/',
      suffix: '.json'
    });
  }
]);

app.run([ '$translate',
  function($translate) {
    // Set default language to German
    $translate.use('locale-de_DE');
  }
]);

Whenever you want to switch the language later on in your code, you just need to call the $translate-service with the language you want to switch to, eg 每当您以后想要在代码中切换语言时,只需使用要切换的语言调用$ translate-service即可,例如

[...]
$translate.use('locale-en_GB'); // switch to British English
[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM