Re: Internationalization: trans with object property
On Wed, Oct 24, 2012 at 2:16 PM, Ma Ba <m.balzke@northbit.com> wrote:
> Hi,
> I have a question about the internationalization feature ...
>
> I have a country model:
>
> class Country(models.Model):
> nameKey = models.CharField(max_length=50)
> shortNameKey = models.CharField(max_length=30)
>
> The properties 'nameKey' and 'shortNameKey' contain resource keys like
> '_country_af' or '_country_af_short'. I need this to show the country names
> depending on the current locale.
>
> The problem was/is to add the resource keys to the .po file, so I found a
> solution in a forum:
> http://stackoverflow.com/questions/7625991/how-to-properly-add-entries-for-computed-values-to-the-django-internationalizati
>
> For example, I added the following to a model file:
>
> def dummy_for_makemessages():
> #countries
> pgettext('forecast type', '_country_af')
> pgettext('forecast type', '_country_af_short')
>
> Runing makemessages this generates the following entries in the .po file:
>
> msgctxt "forecast type"
> msgid "_country_af"
> msgstr "Afghanistan"
>
> msgctxt "forecast type"
> msgid "_country_af_short"
> msgstr "Afghanistan"
>
> For testing purposes I tried to iterate over all countries in my database in
> a template and to print out the translated names:
>
> {% for country in countries %}
> {% trans country.nameKey %}
> {% endfor %}
>
> Unfortunately, it only prints out the following (the resource keys instead
> of the translated names):
>
> _country_af
> _country_ai
> _country_qa
> _country_ke
> _country_kg
> _country_ki
> _country_cc
> _country_co
> ...
>
> I found out that the 'msgctxt' part in the .po file causes the missing
> translation. I removed it for testing purposes and this time it worked fine.
>
> So, my question is, how can I translate resource keys that are not defined
> in the template itself but in an object property instead? Is there a way to
> generate the resource keys without the 'msgctxt' part? I tried NONE or an
> empty string but this generates a commented out resource key in the .po file
> (please see the link to the other forum above).
> Does anybody have an idea how to solve this problem?
>
> Thanks for your help in advance!!
>
You did compile the po file catalogues to mo files, right? Otherwise,
gettext will simply return the msgid. msgctxt should be irrelevant.
Cheers
Tom
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home