Reputation: 3599
While trying to implement a translated version of a website, I came across the following problem:
Let say I have a model defined like that:
class Model(models.model)
name=models.CharField(max_length=10)
...
To create some data (which are later displayed on a web page), I use a special view that includes lines like:
Model.objects.create(name = "testName",...)
I tried to implement the translation with the following code:
Model.objects.create(name = _("testName"),...)
If I import ugettext
as _
, it just put the translated value of "testName" in the database.
If I import ugettext_lazy
as _
, I have an InterfaceError
(more precisely Error binding parameter X - probably unsupported type
.
I was wondering if such an initialization is possible of if I have to find some workarounds.
Thanks in advance.
Upvotes: 0
Views: 1027
Reputation: 3599
Just adding that in case someone need it.
It's the best way I found to implement Florian solution and to avoid the need to add/remove the _
function each time.
You'll define a dummy function in the file containing the strings:
def _(input):
return input
This way, your strings are going to be stored in the original language, but makemessages
is still going to process them like a string that needs translation, thus putting them in the .po
files. And you'll just have to use {% trans %}
block in html.
Upvotes: 0
Reputation: 543
Just store the english version in the db and only call ugettext/_lazy during output. This obviously only works as long as translations for those strings exist, otherwise it will display the english version either way…
Upvotes: 2
Reputation: 23871
ugettext
and ugettext_lazy
are used for static I18N translations. They are applied on translatable text in code to collect the text to .po
files and finally in .mo
files.
Normally for dynamic text, you need to create translation schema in DB through Django ORM and to edit translations in views such as Django Admin. Then in your view, show correct version of translations according to the requested language.
You could 'store' a ugettext_lazy
__proxy__
to DB by pickling it, and could unpickle and apply unicode
on it to get the translation. However, IMO, its meaningless to store a literal from code to DB.
If you want po-based translation solution which is managed through Web interface, ref https://github.com/mbi/django-rosetta
Upvotes: 1