Best practice javascript and multilanguage

后端 未结 9 1897
走了就别回头了
走了就别回头了 2020-11-29 17:37

what is the best practice for multilanguage website using DOM Manipulating with javascript? I build some dynamic parts of the website using javascript. My first thought was

9条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 17:57

    This way you can use one js code for multi language by multi word :

    var strings = new Object();
    
    if(navigator.browserLanguage){
      lang = navigator.browserLanguage;
    }else{
      lang = navigator.language;
    }
    
    lang = lang.substr(0,2).toLowerCase();
    
    
    
    if(lang=='fa'){/////////////////////////////Persian////////////////////////////////////////////////////
    
    
            strings["Contents"]                              = "فهرست";
            strings["Index"]                                 = "شاخص";
            strings["Search"]                                = "جستجو";
            strings["Bookmark"]                              = "ذخیره";
    
            strings["Loading the data for search..."]        = "در حال جسنجوی متن...";
            strings["Type in the word(s) to search for:"]    = "لغت مد نظر خود را اینجا تایپ کنید:";
            strings["Search title only"]                     = "جستجو بر اساس عنوان";
            strings["Search previous results"]               = "جستجو در نتایج قبلی";
            strings["Display"]                               = "نمایش";
            strings["No topics found!"]                      = "موردی یافت نشد!";
    
            strings["Type in the keyword to find:"]          = "کلیدواژه برای یافتن تایپ کنید";
    
            strings["Show all"]                              = "نمایش همه";
            strings["Hide all"]                              = "پنهان کردن";
            strings["Previous"]                              = "قبلی";
            strings["Next"]                                  = "بعدی";
    
            strings["Loading table of contents..."]          = "در حال بارگزاری جدول فهرست...";
    
            strings["Topics:"]                               = "عنوان ها";
            strings["Current topic:"]                        = "عنوان جاری:";
            strings["Remove"]                                = "پاک کردن";
            strings["Add"]                                   = "افزودن";
    
    }else{//////////////////////////////////////English///////////////////////////////////////////////////
    
    strings["Contents"]                              = "Contents";
    strings["Index"]                                 = "Index";
    strings["Search"]                                = "Search";
    strings["Bookmark"]                              = "Bookmark";
    
    strings["Loading the data for search..."]        = "Loading the data for search...";
    strings["Type in the word(s) to search for:"]    = "Type in the word(s) to search for:";
    strings["Search title only"]                     = "Search title only";
    strings["Search previous results"]               = "Search previous results";
    strings["Display"]                               = "Display";
    strings["No topics found!"]                      = "No topics found!";
    
    strings["Type in the keyword to find:"]          = "Type in the keyword to find:";
    
    strings["Show all"]                              = "Show all";
    strings["Hide all"]                              = "Hide all";
    strings["Previous"]                              = "Previous";
    strings["Next"]                                  = "Next";
    
    strings["Loading table of contents..."]          = "Loading table of contents...";
    
    strings["Topics:"]                               = "Topics:";
    strings["Current topic:"]                        = "Current topic:";
    strings["Remove"]                                = "Remove";
    strings["Add"]                                   = "Add";
    
    }
    

    you can add another lang in this code and set objects on your html code. I used Persian For Farsi language and English, you can use any type language just create copy of this part of code by If-Else statement.

提交回复
热议问题