angular translate instant method does not work

前端 未结 2 1459
一个人的身影
一个人的身影 2021-01-14 16:13

We have a website with angular translate. It works perfectly. We have a variable that we want to fixate with a certain language key. Let\'s say the site\'s language has \'en

2条回答
  •  日久生厌
    2021-01-14 16:42

    Why don't you just embed the word in 'zh'. Don't use $translate there since you obviously don't want to translate that word.

    If there's another reason for this; I would suggest:

    // store the current language
    var currentLanguage = $translate.use();
    // change the language
    $translate.use("zh").then(function (translation) {
        // then translate here
        $log.debug($translate.instant('SOME_WORD'));
        // set the previous language back when you're fulfilled
        $translate.use(currentLanguage);
    });
    

    But since this is async, this may translate some other words to 'zh' in the meantime.

    A third way to achieve this would be setting the same translation value for SOME_WORD in each language file.

    And a forth way I could think of is translating SOME_WORD only in the 'en' file (no translation value in the 'zh' file) and using 'en' as the fallback language. Such as: $translate.fallbackLanguage('en')

提交回复
热议问题