How to use i18next? Problems with translations

前端 未结 2 1900
萌比男神i
萌比男神i 2020-12-29 05:45

I want to use a internationalization option at my jQuery Mobile and jQuery webside. I tried to generate an example with the documentation on http://i18next.com but it seems

2条回答
  •  旧巷少年郎
    2020-12-29 06:20

    Main problem is you can't call i18n.t("menu.surname", { defaultValue: "Name:"}); directly after initialization, as loading the resources from server is async, so basically you try to translate before i18next fetched the resources.

    Instead load it with callback:

    $(document).ready(function(){
      language_complete = navigator.language.split("-");
      language = (language_complete[0]);
      console.log("Sprache (root): %s", language);
    
      i18n.init({ lng: language, debug: true }, function() {
          // save to use translation function as resources are fetched
          $(".menu").i18n();
          $("headline").i18n();
      });
    });
    

    or use flag to load resources synchron.

    By the way: Your html code has one closing

    too many.

    The call to $("headline").i18n(); should be $("#headline").i18n();.

提交回复
热议问题