2015-04-08 68 views
3

有沒有辦法觸發某個類的所有值轉換器重新評估?觸發值轉換器重新評估

我在問這個,因爲我目前正在努力弄清楚本地化的一個很好的方法,特別是有一個翻譯字符串的字典。

實施例是如下:

值轉換器:

export class TValueConverter { 
    static inject() { return [Dictionary] } 

    dictionary: Dictionary; 

    constructor(dictionary: Dictionary) { 
    this.dictionary = dictionary; 
    } 

    toView(key: string, replacements: any): string { 
    return this.dictionary.translate(key, replacements); 
    } 
} 

視圖:

<template> 
    <p>${"hello_world" | t: {"name": "Some User"} }</p> 
</template> 

這隨後將被轉化爲

<p> 
Hello Some User! 
</p> 

但是,用戶可以改變目前的語言,當他們這樣做時,我想重新評估這種使用新語言的價值轉換。

我的實現靈感來自https://github.com/zewa666/aurelia-i18next,但他們的解決方案是向值轉換器中添加「:currentLanguage」以引入依賴項,這種方法可行,但感覺重複,因爲我必須將字典注入每個視圖模型以使「當前語言」變量可用於視圖。

我可以從TValueConverter類發出信號,它對dictionary.lang有依賴關係,或者使用事件觸發它嗎? https://github.com/aurelia/templating-binding:

+1

信號在釋放後接下來的到來,盯緊這個問題確實解決/問題/ 25 –

回答