How to use Knockout observables in i18next?

后端 未结 4 478
抹茶落季
抹茶落季 2021-01-20 00:10

I\'m trying to somehow dynamically use i18next translations together with Knockout.js, but I cant figure out how.

Neither a custom Knockout binding or the i18next jQ

4条回答
  •  遇见更好的自我
    2021-01-20 00:16

    KO config:

    var language = ko.observable('');
    
    ko.i18n = function(key) {
      return ko.computed(function() {
        if (language() != null) {
          return i18n.t(key, {
            lng : language()
          });
        } else {
          return "";
        }
      }, key);
    };
    

    view-model:

    var labels = {      
    aboutUs: ko.i18n('app:labels.aboutUs'), 
    contactUsBtn: ko.i18n('app:labels.contactUsBtn') }
    

    view:

    
    

提交回复
热议问题