1questionnow
1questionnow

Reputation: 11

Variable Language (Browser Language detection) - not working in safari

Ok here is the thing, i have a website and want it to check if someones browser language ist german and display them german texts if it is, if its NOT german the should always get a english version. A friend (who now has no more time) coded me this into the index:

<script>

    var texte = {
        'title': {
            'de':'eis',
            'en':'ice'},
        'subtitle': {
            'de':'Untertitel DE',
            'en':'Subhead Eng'},

        'email': {
            'target':'placeholder',
            'de':'Platzhalter DE',
            'en':'Email me when it is ready'},
        'submit': {
            'target':'value',
            'de': 'Lorem Ipsum',
            'en': 'send'
        },
        'share_txt': {
            'de': 'bitte teilen',
            'en': 'share...'
        }

    };

    (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s);
        js.id = id;
        js.src = "//c";
        fjs.parentNode.insertBefore(js, fjs);

    }(document, 'script', 'facebook-jssdk'));

    (function(d){
        var t = window.navigator.language!='de'?'en':'de';
        for(i in texte){
            var target = d.getElementById(i),
                    text = texte[i][t];
            if(texte[i].target){
                target[texte[i].target] = text;
            }else{
                target.innerText = text;
            }

        }

    })(document);
</script>

This works in Chrome but not in safari. Do you guys have any idea? Also i would think a easy workaround would be a detection and then sending the user to either a index_de.html or index_eng.html page. How would i implement this?

Upvotes: 1

Views: 504

Answers (1)

GolezTrol
GolezTrol

Reputation: 116140

Have you inspected the actual value of window.navigator.language?

If I understand the documentation correctly, the language is in RFC 4646 notation, so it could also have the notation de-DE. So you may want to inspect just the first two characters instead to get just the primary language without the subtags.

A small change would get you this:

var t = window.navigator.language.substr(0, 2)!='de'?'en':'de';

Upvotes: 2

Related Questions