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 ajs
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.
score:0
Solved this by using a different method - not JavaScript.
score:2
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 countryru_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.
Credit To: stackoverflow.com
Related Query
- linkedin language code change
- Django url change language code
- Change some default words for a specific language code in django
- Django: How to get language code in template?
- Django and Celery - re-loading code into Celery after a change
- How to change language from Django URL?
- Issue trying to change language from Django template
- Django: i18n - change language
- Set or change the default language dynamically according to the user - Django
- Code change monitoring malfunctioning with Django 1.7 on mod-wsgi
- Django: Can't change default language
- Is there any way to use django shell without restarting when change code
- When to use the locale name and when the language code in Django?
- Django language change ignored, remains default
- Getting the two letter language code of the client
- How do I change a language of a particular page in Django website
- When using i18n_patterns, how to reverse url without language code
- How to convert language code to locale name?
- Do I need to change my normal Django code when introducing Django Channels?
- Django, Nginx, FastCGI caching problem when I change code
- Django: change app language programmatically (without /i18n/setlang/ view)
- Change language and its url in django
- Where does django read request language code and sneak it into DecimalField output?
- Python - Extract Main Language Code from HTTP / Browser
- Swap main code language with translation
- Django throwing ValueError: too many values to unpack (expected 2) with no change to the code
- how to add change language dropdown to Django(1.10) admin?
- What is the best way to change the language files in Django
- Is there a way to change language in django-mdeditor
- change the language attrib in HTML tag based on user's language Django Wagtail
More Query from same tag
- How to use one to many models in Django rest framework
- Error on importing passenger_wsgi.py file
- Django ModelForm with ManyToManyField showing all possible objects
- Technology stack for building a web application
- How to show different inlines depending of current object field value
- How to write Python Django queries, and import them?
- django select rows by max value of field
- Django+MongoDb connection error
- Writing a manager to filter query set results
- What can I do if celery sometimes forgets task?
- Is there a shorter way to check for a value in M2M in a Django template?
- Limits of django-haystack with Solr 4.0
- Using Django's ORM in a Celery Task
- Using UTF-8 encoded JSON fixture file in Django
- Sort a python Django query with zero always first, then positives, then negatives