在Aurelia中刷新i18n转换后的字符串插值 [英] Refreshing i18n translated string interpolated values in Aurelia

查看:66
本文介绍了在Aurelia中刷新i18n转换后的字符串插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我例如使用标题中的选择下拉输入字段(更确切地说,是我的站点范围导航栏中的自定义元素),并在共享状态对象中全局设置该值.如果我-在languageChanged(value)上(导航栏自定义元素内),还更改了this.i18n.setLocale('de-DE')

If i eg. use a select drop down input field in my header (more precisely in my sitewide nav-bar which is custom element), and have that value set globally in a shared state object. If I - on languageChanged(value) (inside the nav-bar custom element) also change the this.i18n.setLocale('de-DE')

然后我该如何在模板中刷新i18n转换后的字符串插值(例如${'status_deceased' | t}),而不必像现在一样导航到新路线并返回?

How would I then refresh the i18n translated string interpolated values (eg. ${'status_deceased' | t} ) inside my templates without having to navigate to a new route and back as I do right now?

我在github上发现了关于问题 https://github.com/aurelia/i18n的问题/issues/6 ,但由于我需要这样做,因此我希望可以找到一些巧妙的解决方法,除了必须使用window.location重新加载页面:/

I found this issue on github about the problem https://github.com/aurelia/i18n/issues/6 but since I need this to work, I'm hoping that some clever workaround exists, except from having to use window.location to reload the page :/

编辑

如果我正确地理解了这一点,那似乎只是我的幸运之日,而​​该功能刚刚在8天前被添加,尽管仍未记录在案:

If I'm understanding this correctly it seems it might just be my lucky day, and such a feature has just been added 8 days ago, although still undocumented: https://github.com/aurelia/templating-resources/pull/126 - Can anyone figure out and tell me how to implement this, using this new feature perhaps? If I figure this out myself, I'll update this thread with the solution :-)

推荐答案

下一个发行版发布时,您将可以使用如下所示的signal绑定行为为绑定指定信号名称":

When the next release goes out you'll be able to assign a "signal name" to a binding using the signal binding behavior like this:

<h1>${'title_key' | t & signal:'i18n'}</h1>
<p>${'content_key' | t & signal:'i18n'}</p>

&符号表示绑定行为"(与值转换器的|相反).绑定行为是为绑定添加行为"的资源.他们具有对绑定实例的完全访问权限,并在绑定的bindunbind生命周期事件之前得到通知.

The & symbol denotes a "Binding Behavior" (as opposed to | for value-converters). Binding behaviors are resources that add "behavior" to a binding. They have full access to the binding instance and are notified prior to the binding's bind and unbind lifecycle events.

Aurelia将附带几种内置的绑定行为:油门",去抖动",一次性",信号"等.您还可以选择创建自己的绑定行为.

Aurelia will ship with several built-in binding behaviors: "throttle", "debounce", "one-time", "signal" etc. You also have the option of creating your own binding behaviors.

在上面的示例中,我们为titlecontent插值绑定指定了"i18n"的信号"名称.该名称是任意的,我们只需要知道它是什么就可以使用BindingSignaler这样标记"绑定以刷新:

In the example above we've given the title and content interpolation bindings a "signal" name of "i18n". The name is arbitrary, we just need to know what it is so we can "signal" the bindings to refresh using the BindingSignaler like this:

import {BindingSignaler} from 'aurelia-templating-resources';
import {inject} from 'aurelia-framework';

@inject(BindingSignaler)
export class App {
  constructor(signaler) {
    this.signaler = signaler;
  }

  // invoking this method will refresh all bindings in the application
  // with the "signal name" of "i18n"
  refreshBindings() {
    this.signaler.signal('i18n');
  }
}

我想像一旦绑定行为功能下降,i18n插件中就会进行其他工作,以将t值转换器与某些版本的signal绑定行为结合起来,以使简洁的绑定表达式既能处理翻译又能处理在语言更改时刷新绑定,因此您可能暂时想坐一会儿.

I imagine once the binding behavior feature drops there will be additional work in the i18n plugin to combine the t value converter with some version of the signal binding behavior to enable terse binding expressions that take care of both translation and refreshing the bindings when the language changes so you might want to sit tight for the time being.

编辑 如果您今天需要一些东西,您可以利用现有的Aurelia功能:在转换器参数更改时重新评估绑定.

EDIT If you need something today you could take advantage of an existing Aurelia feature: bindings are re-evaluated when converter parameters change.

  1. 创建一个新类:

export class LanguageChangedNotifier {
  signal = 0;

  notify() {
    this.signal++;
  }
}

  1. 将该类注入所有视图模型,并将实例添加为属性:

@inject(LanguageChangedNotifier)
export class App {
  constructor(notifier) {
    this.notifier = notifier;
  }
}

  1. t绑定中使用通知程序(不会影响t值转换器的行为):
  1. Use the notifier in your t bindings (it won't impact the behavior of the t value converter):

${'status_deceased' | t:notifier.signal}

  1. 更改语言环境时,请使用通知程序刷新绑定:

this.notifier.notify();

这篇关于在Aurelia中刷新i18n转换后的字符串插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆