I understand that Django renders the templates at the server side. The browser receives HTML content, which can then be manipulated using JavaScript.
I have some JavaScript code to manipulate the Django language tag that will be difficult to maintain in its current form, so I am trying to come with a better way to code it, but I cannot think of such a way and need some help.
Here is my code:
The dynamic_language_code is a two letter code ie: en, fr, bg, ru, es, etc.
function dateCalculation(dynamic_language_code) {
var c = dynamic_language_code;
//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 %}";
//bulgarian.
} else if (dynamic_language_code == 'bg'){
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 %}";
}
........
many more else if conditions
........
//default value of English.
} else {
var_month = "{% language 'en' %}{% trans 'month' %}{% endlanguage %}";
var_months = "{% language 'en' %}{% trans 'months' %}{% endlanguage %}";
var_year = "{% language 'en' %}{% trans 'year' %}{% endlanguage %}";
var_years = "{% language 'en' %}{% trans 'years' %}{% endlanguage %}";
}
}
The code should dynamically change the language of the month, years according to the passed in dynamic_language_code value. There really should be no need for an if else statement, just the assignment of the var_month, var_months, var_year and var_years values using the passed in dynamic_language_code value.
How do I structure the code above to get rid of the if else condition and still return the correct language versions using the passed in dynamic_language_code?
EDIT
I want to get rid of the if else conditions and just assign the variables to the language code using the dynamic language tag. Not sure how I can do this or even if this can be done. For example:
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 %}";