django language tag

51 views
Skip to first unread message

6233114 6233114

unread,
Oct 10, 2014, 7:42:55 PM10/10/14
to django...@googlegroups.com
I have some javascript that displays the timespan between two dates according to a language code. For example in en / English: (3 years, 2 months) , in ru / Russian (3 лет, 2 месяцев) in de / German: (3 Jahre, 2 Monate) , etc.

The code is working ok but will be difficult to maintain, so I have tried to pass in a dynamic variable to change the language display but it does not work, and I am not sure why and I am unsure how to get it working, despite trying for several days now.

Here is the relevant section of the working code, that will be difficult to maintain if more language codes are added to the site:

function dateCalculation(date1, date2, dynamic_language_code) {
    
    //this function will accept two dates (format: mm/yyyy) and calculate the difference between the 2 dates
    //and display the difference as x months or x years, x months.
        
    //arabic
    if (dynamic_language_code == 'ar'){
    
        var_month = "{% language 'ar' %}{% trans 'month' %}{% endlanguage %}";
        var_months = "{% language 'ar' %}{% trans 'months' %}{% endlanguage %}";
        var_year = "{% language 'ar' %}{% trans 'year' %}{% endlanguage %}";
        var_years = "{% language 'ar' %}{% trans 'years' %}{% endlanguage %}";
        
    } else if (dynamic_language_code == 'bg'){ //bulgaian
        
        var_month = "{% language 'bg' %}{% trans 'month' %}{% endlanguage %}";
        var_months = "{% language 'bg' %}{% trans 'months' %}{% endlanguage %}";
        var_year = "{% language 'bg' %}{% trans 'year' %}{% endlanguage %}";
        var_years = "{% language 'bg' %}{% trans 'years' %}{% endlanguage %}";
        
    } else if (dynamic_language_code == 'zh-CN'){ //chinese (simplified)
        
        var_month = "{% language 'zh' %}{% trans 'month' %}{% endlanguage %}";
        var_months = "{% language 'zh' %}{% trans 'months' %}{% endlanguage %}";
        var_year = "{% language 'zh' %}{% trans 'year' %}{% endlanguage %}";
        var_years = "{% language 'zh' %}{% trans 'years' %}{% endlanguage %}";
        
    }
    ......
    more conditions & remainder of code not important for this question.
    ......
    

The above variables take the form of:

date1: 01/2009
date2: 03/2012
dynamic_language_code: tr (a two letter language code)

Here is the code with the passed in variables that I cannot get to work:

        var_month = "{% language dynamic_language_code %}{% trans 'month' %}{% endlanguage %}";
        var_months = "{% language dynamic_language_code %}{% trans 'months' %}{% endlanguage %}";
        var_year = "{% language dynamic_language_code %}{% trans 'year' %}{% endlanguage %}";
        var_years = "{% language dynamic_language_code %}{% trans 'years' %}{% endlanguage %}";

How would I get the django language tag to work if I am passing in the dynamic_language_code variable as show immediately above? 

I think the error is to do with the django language tag not recognising the dynamic_langauge_code when it is not in quotes (''), but I could be wrong.

Collin Anderson

unread,
Oct 14, 2014, 11:48:50 AM10/14/14
to django...@googlegroups.com
Hello,

What does the error say?

Collin

6233114 6233114

unread,
Oct 14, 2014, 5:20:19 PM10/14/14
to django...@googlegroups.com
Hello,

There is no error. The translation simply reverts to the default language, which is English.
Reply all
Reply to author
Forward
0 new messages