简体   繁体   English

在运行时更改一些翻译

[英]Change some translation during runtime

I have an AngularJS Application, which is available in 3 Languages and it uses .json files to load all the language key value pairs. 我有一个AngularJS应用程序,提供3种语言版本,它使用.json文件加载所有语言键值对。

However I want to dynamically change some of the key value pairs during runtime. 但是,我想在运行时动态更改某些键值对。

So for example I have "ORDER.HEADING" to be "Heading" by default. 因此,例如,我默认将“ ORDER.HEADING”设置为“ Heading”。 And if the user does something specific it should be "Different Heading". 并且,如果用户执行特定操作,则应为“不同标题”。

How do I do that? 我怎么做? I can't access the translateProvider Service in a controller and the $translate service doesn't seem to have a method which can update key value pairs... 我无法在控制器中访问translateProvider服务,并且$ translate服务似乎没有可以更新键值对的方法...

Unfortunately you can not update the json file and its key/value paris on runtime. 不幸的是,您无法在运行时更新json文件及其键/值巴黎。 I suggest you do the following: 我建议您执行以下操作:

In your template call a function to retrieve your key: 在模板中调用一个函数来检索您的密钥:

<h1>{{ $ctrl.getI18nHeadingKey() | translate }}</h1>

Inside your controller: 在控制器内部:

this.someCondition = false;

getI18nHeadingKey() {
  return this.someCondition ? 'YOUR_TRANSLATION_KEY_IF_IT_IS_TRUE' : 'YOUR_TRANSLATION_KEY_IF_IT_IS_FALSE';
}

Make sure to have both keys added and translated inside your json files, just as you did with all your other translations. 确保在json文件中添加并翻译了密钥,就像处理所有其他翻译一样。 The given example can be expanded in case you have more than one condition of course. 如果您有多个条件,则可以扩展给定的示例。 Just return the translation key you want in your template for the given condition. 只需在给定条件下在模板中返回所需的翻译键即可。

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

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