0

I have some code that I can not get my head around. The more I look at it the more confused I become.

There are two date values and a language code that is passed into a js function. Then there is a django collection (I think!) that interacts with the django language tag to assign the correct values.

I thought I had set this up correctly, but the code is not working and I cannot see the reason for it as my experience is not good enough to see where I have gone wrong.

The error occurs when I try to call the names.month (as shown on the last line), so I think I have made an error in the name_map code or in the assignment of the variables of lc and LANGUAGE_CODES.

The passed in values are:

date1: 10/2000;

date2: 12/2004;

dynamic_language_code: de;

Any suggestions would be great.

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.

var a = date1;
var b = date2;
var lc = dynamic_language_code;
var LANGUAGE_CODES = 'ar, zh-CN, zh-TW, en-GB, en, fr, fr-CA, de, it, pl, pt, pt-BR, ru, es-419, es';

var name_map = {
    {% for lc in LANGUAGE_CODES %}
        {{ lc }}: {
            month: "{% language lc %}{% trans 'month' %}{% endlanguage %}",
            months: "{% language lc %}{% trans 'months' %}{% endlanguage %}",
            year: "{% language lc %}{% trans 'year' %}{% endlanguage %}",
            years: "{% language lc %}{% trans 'years' %}{% endlanguage %}"
        } {% if not forloop.last %},{% endif %}
    {% endfor %}
}
names = name_map[lc];
if(names === undefined) { names = name_map['en']; }

....
time_span = total_months + " " + names.month;
....
user3354539
  • 1,245
  • 5
  • 21
  • 40
  • 1
    What does the code look like when rendered (ie in View Source)? Seems to me you have some confusion between server-side and client-side variables here: you define a JS variable `LANGUAGE_CODES`, but also try and iterate through something called `LANGUAGE_CODES` in a template tag. Is that the same thing, or are you also passing LANGUAGE_CODES into the template context? – Daniel Roseman Oct 15 '14 at 08:07
  • This is indeed a template used by django to generate (in this case) js code, the `{% ... %}` and `{{ ... }}` are expressions for that template language. What is the error? And can you give us the code that renders the template? – Dettorer Oct 15 '14 at 08:12
  • Just like @DanielRoseman mention that you have confused the server-side code with the client-side code. – Neo Ko Oct 15 '14 at 08:34

1 Answers1

1

You confused the server-side code with client-side code

For example,

the server-side code

from django.shortcut import render_to_response

name_map_handler(request, **kwargs):
    """
    some code to handler the ajax request
    """
    render_to_response('the_template_you_want_to_use.html', {'LANGEAGE_CODES': ['zh-hans', 'de'])

only the server-side variable that you decide to render can be use in the Django template. Just like the LANGAGE_CODES in my example.

Neo Ko
  • 1,365
  • 15
  • 25