Question

I am a beginner in programming and I have been able to add the linkedin share button to a test webpage.

I now want to change the language of the linkedin share button, so that it will change language when the user changes their language on the test web page.

Linkedin users a five character langauge code (ru_RU, en_US, ko_KR, etc). My test webpage uses a two character language code (ru, en, ko), so I have to write a js function to return the five character language code.

I am using django 1.4 and the "{{ user.get_profile.language_preference }}" returns the two character language code.

Here is what I attempted to "convert" the language code:

        <script type="text/javascript">
        function language_code_extended(){
            var language_code_value = "{{ user.get_profile.language_preference }}";
            if ( language_code_value == ru ) { 
                return "ru_RU"
            }
            else if ( language_code_value == "fr" ) { 
                return "fr_FR"
            }
            else if ( language_code_value == "ko" ) {  
                return "ko_KR"
            }
            else {
                return "en_US"
            }
        }
    </script>

Here is my linkedin share button code:

    <!-- LinkedIn Like Button code -->
    <script src="//platform.linkedin.com/in.js" type="text/javascript">
        //lang: ru_RU
        lang: language_code_extended()
    </script>

This does not work. Can someone point out what I have done wrong?

Thanks.

Was it helpful?

Solution 2

Solved this by using a different method - not JavaScript.

OTHER TIPS

Probably you need to quote the strings you're comparing to, for example:

language_code_value == ru

should be

language_code_value == "ru"

otherwise the comparison will be looking for a variable named ru to compare to.

There are a few additional issues to address in making this conversion:

  • Having an if condition test for every language you'll want to deal with is quickly going to become unwieldy and error prone.

  • This mapping is taking a language code ru and mapping to a code representing and language and a country ru_RU. This involves making an assumption that somebody speaking a certain language is in a certain country, which is not generally correct. Many languages are spoken in multiple countries with different conventions in each.

It might be worth your while to dig a little deeper to find a better way of doing this. This will likely result in less effort and a more correct solution.

I'm sorry, I don't know Django so I can't point you in the right direction, but you might want to explore whether it, or a third party package, can return you the correct IETF language tag directly.

Edit

You're also going to have problems with:

var language_code_value = "{{ user.get_profile.language_preference }}";

language_code_value is going to have the literal string value {{ user.get_profile.language_preference }} which is never going to compare equal to the two digit language codes you want to compare it to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top