Bilingual site with JS

3

I needed to put a bilingual website and I found this script below, which by the way helped a lot, but it always shows the two versions "BR" "ENG" before "hiding" one of them. Even altering the order of events he continues to show both languages. I'm letting something go, but I do not understand. Does anyone have a light?

This is the code:

<script type="text/javascript">
        function createCookie(name, value, days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            }
            else
                expires = "";
            document.cookie = name + "=" + value + expires + "; path=/";
        }
        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) === ' ')
                    c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) === 0)
                    return c.substring(nameEQ.length, c.length);
            }
            return null;
        }
        function language(lang_on, lang_off) {
            createCookie("langue_on", lang_on, 365);
            createCookie("langue_off", lang_off, 365);
            for (var i = 0; i < document.getElementsByTagName("div").length; i++) {
                if (document.getElementsByTagName("div")[i].lang === lang_on) {
                    document.getElementsByTagName("div")[i].style.display = "block";
                }
                if (document.getElementsByTagName("div")[i].lang === lang_off) {
                    document.getElementsByTagName("div")[i].style.display = "none";
                }
            }
        }
        function startlanguage() {
            var notdefined;
            var lang_on = readCookie("langue_on");
            var lang_off = readCookie("langue_off");
            if (lang_on === notdefined) {
                lang_on = "pt";
            }
            if (lang_off === notdefined) {
                lang_off = "en";
            }
            language(lang_on, lang_off);
        }
        window.onload = function () {
            startlanguage();
        };
    </script>

I call the function like this:

<div lang="en">ENGLISH!</div>
<div lang="pt">PORTUGA!</div>
    
asked by anonymous 19.02.2015 / 19:14

2 answers

1

I created a function for you that is: link

Instead of using cookie I used localStorage, you can change the default language value in line 2 of the javascript code ....

    
20.02.2015 / 04:05
0
  

Bilingual site with JS?

Just a comment, the title of the question was perfect but its description induces the answers to correct the posted code, and not to propose the correct solution that would be to adopt an established standard.

Responding, I believe the ideal in your case is to use the L10n standard, and there are well-established JavaScript libraries that support L10n. For example, l10n.js (which is used to locate FirefoxOS).

This way you do not have to break your head by creating a new structure for each language, you only need to create a JSON property file with the strings keys and their respective value in each supported language.

    
21.04.2015 / 16:20