Using `ugettext` in a definition of a Django field - django

In my models.py file I define a field on a model like this:
description = models.CharField(
max_length=40,
default=_('Bla bla bla'),
)
Now, _ is django.utils.translation.ugettext, and I want to use as a default value the Hebrew translation of 'Bla bla bla', which is 'בלה בלה בלה'. (The website is in Hebrew only.) The string is properly translated in the messages file. But when I run the Django admin and create a new object, I see the English 'bla bla bla' on the field. I assume that English is the active language when compiling the models.py module. How can I solve this and make it Hebrew?
I know that one solution would be to forego ugettext and just write Hebrew inside the Python module, but I prefer to avoid that in order to prevent encoding hell.

Try ugettext_lazy
https://docs.djangoproject.com/en/1.3/topics/i18n/internationalization/#lazy-translation
Always use lazy translations in Django
models.

ugettext merely marks strings as translatable. It doesn't do any translation for you, per se. You have to create language files (.mo and .po) and then set the LANGUAGE_CODE setting to match. Read the Django documentation on Internationalization and Localization for more in depth info.

Related

How can I use Django's translation strings with Wagtailtrans (Add-on for supporting multi language Wagtail sites)

So I have a website which is using the Wagtailtrans extension for Wagtail. I basically enables multi language by duplicating the page tree. So the url gets fixed with the language-code at the start.
I can translate all my content which I define through my models perfectly fine. Here's an example of how that works:
class ServicesPage(MetadataPageMixin, TranslatablePage):
description = models.CharField(max_length=255, blank=True,)
content_panels = Page.content_panels + [
FieldPanel('description', classname="full")
]
Instead of Page you define it as TranslatablePage in your model. All is working fine, however I still need some additional strings which I don't define in my models to be translated. I just use Django's translation feature with {% load i18n %} and then the strings wrapped inside {% trans "String" %}.
So far so good, I defined my two languages in Wagtail admin (Wagtail trans creates an option for that) which in this case is English and Dutch. I set English as the main language so the strings are in english.
I use ./manage.py makemessages and it creates a .po file for me, with all the tagged strings in there. At last I use ./manage.py compilemessages.
But translated strings are not showing up when I switch to Dutch language, it still displays the english strings. So I suspect it needs some additional tweaking to work with Wagtailtrans, but I can't seem to figure out how this set-up should be. Can anyone help me out?

Variable translation in django

I want to translate in any language django permission names. To do this i extend django.contrib.auth.models.Permission model in django like this :
class ExtendedPermission(Permission):
translation_name = models.CharField(_('translation_name'),max_length=255)
Then i want to update this table in management commands. First 'name' field taken from Permission model, than it must be translated into another language for instance tr to use in ExtendedPermission model as translation_name field.This function in management/commands:
def try_translate(self,permission):
translation.activate(settings.LANGUAGE_CODE)
translation.activate('tr')
translated_str =translation.ugettext(permission)
translation.deactivate()
return translated_str
When run this command the variable does not appear in .po file.But if i give string that i want to translate like this :
ugettext("name")
msgid = "name" appears in .po file and i can edit msgstr but i can not get the translated msgstr from .mo file with using ugettext.
I add 'django.middleware.locale.LocaleMiddleware', to settings file.I also run commands makemessages and compilemessages.
As I understand it, the Django i18n support works off static data, which you pre-generate running makemessages. It looks for _(), {% trains %}, etc, which require static strings.
If permissions is not know statically ( e.g. Without reading a database ), then it cannot process it during makemessages.

Django translation: add custom translation

I have an application where users can fill a text field.
I would like "try" to translate it if the string that the user entered is in the .po translation files.
So in one of my detail views, I did something like:
class InterrogationDetailView(generic.DetailView):
model = Interrogation
def get_context_data(self, **kwargs):
context = super(InterrogationDetailView, self)\
.get_context_data(**kwargs)
if self.object is not None:
context[u'translated_word'] = {
u'description': _(self.object.description),
}
return context
That's nice, it seems to work. So it searches in the .po files. So I'd like to add sentences, or words on my own in those .po files. When I try to add one translation that is not in my source files, when I call makemessages I get them commented like:
#~ msgid "I'm a test"
#~ msgstr "Godsmack - Cryin' like a b"
How to solve this? And if I'm not doing this the right way (I've read a lot about django translation), what is the way to go?
Django documentation mentions that makemessages is not capable of extracting translations for computed values, as happens in your example.
In order to have translations for strings that you retrieve from somewhere else you have to have them in your code as string literals. This can be achieved by the way #psychok7 suggests, creating a separate .py listing all those strings.
One way to automate this could be to write your custom django-admin command that will retrieve strings to be translated from database and put them into some file, locatable by makemessages, e.g. .txt with translation tags.

how to Write in Django SlugField with hebrew

i need help how to write Hebrew in SlugField in django models
now i can write only English
something like: hello-world
but i what to write in SlugField = "שלום-עולם"
and the url be like:
www.somsite.com/blog/שלום-עולם
10X
The whole idea of the slugfield is that it converts a given string to only url-safe characters. Hebrew is unicode, which means that it generally is unsafe to use in the url. If you want to override this, just use your article title instead of a slugfield.

Django multilingual - how to preview content from any language?

I'm using django-multilingual for a Django based website. When I define the __unicode__ function for a model to return this way:
def __unicode__(self):
return unicode(self.title)
However my default language is English and I have some items that are inserted in Dutch only. When I preview the full list, I get "None" as a title.
Is there an easy way to try to get the English title and then check for a title in any other language just for preview?
Iterating over all translations can be easily done like this:
>>> translations = [getattr(obj, "name_" + lang[0].replace("-","_")) for lang in
settings.LANGUAGES]
Where obj is the model object, and lang will represent a tuple ('bg', 'Bulgarian') from your settings file.
lang[0].replace("-","_") is required, in case that you have languages like "uk-gb", because those values are placed into name_uk_gb
Well, assuming your model stores the Dutch version in an attribute called dutch_title, you could do this.
def __unicode__(self):
# If we've got an English title, use that
if self.title:
return self.title
# Otherwise, default to the Dutch title
return self.dutch_title
Without knowing what you mean by "some items are inserted in Dutch only", it's a bit difficult to answer your question more helpfully.
Depending on the app you are using (for example - django-multilingual), you can use:
<td>{{ object.name_en|escape }}</td>
(example from: http://code.google.com/p/django-multilingual/source/browse/trunk/testproject/templates/articles/category_list.html)
As i remember there were some other forks of this app, that used to change that behavior like this:
Objec title is: {{object.en.name}}
In case you use other app, you can always run the manage.py shell and test with dir(MyModel), or dir(MyModel.fields) to check what fields are defined there :)
In case there are magic getters for those field names and you cant see anything interesting in dir(...), you can refer to the code of your prefered l18n app and see what going under the hood :)