Translated strings not showing up when i switch languages in Django site - django

I have a reasonably simple Django (1.1) site where i need some basic interface and other texts to be translated between two languages. I've created the po files using manage.py makemessages, translated them (using poedit), and compiled the mo files using manage.py compilemessages as outlined in the i18n docs for Django.
But the problem is; most strings still show up in the original language...
i checked that the po files actually contain all strings
i checked that the mo files were freshly generated after the last translation effort
the language does actually change when i switch using the getlang() method
a few strings -do- end up being translated when i switch
but most don't...
Not really sure where else to look... Is there any app that i can use to check whether the compiled mo files are valid & complete for instance? Could these strings be cached? (i'm not using any caching middleware)

Found it!! While pulling out hair trying to figure out what was causing my woes i commented out django.middleware.locale.LocaleMiddleware from my MIDDLEWARE_CLASSES and refreshed the page in an attempt to try everything. Obviously that just turned off translation all together but when i turned it back on again, all my fine translated strings were showing up as they should have been all along.
So i'm guessing something, somewhere get's compiled/cached when you turn on the locale middleware and the only way to refresh it is to turn it off and on. Restarting the server didn't help so this a bit counter intuitive, but who cares it works! :)

Related

Missing Django core translations

I am trying to translate django contrib strings which for my language are untranslated in the original .po file. When I add them to my own .po file and translate them, the system works fine. The problem comes every time I use the makemessages command, since the generator interprets that those text strings have been deleted and comments them in the .po file. Can I hand generate a file that contains the translations and is not affected by the makemessages command? On the other hand, is it possible to tell him not to comment on the deleted lines?
The most effective would be probably to contribute directly to the Django project to provide those translations so they are included in the next version!
See "Localizing Django" for details.

Extract comment written in Chinese and translate them into English using some script

I have a C++ project in which comments of source code are in Chinese language, now I want to convert them into English.
I tried to solve using google translator but got an Issue: Whole CPP files or header didn't get converted, also I have found the name of the struct, class etc gets changed. Sometimes code also gets modified.
Note: Each .cpp or .h file is less than 1000 lines of code.But there are multiple C++ projects each having around 10 files. Thus I have around 50 files for which I need to translate Chinese text to English.
Well, what did you expect? Google Translate doesn't know what a CPP file is and how to treat it. You'll have to write your own program that extracts comments from them (not that hard), runs just those through Google Translate, and then puts them back in.
Mind you, if there is commented out code, or the comments reference variable names, those will get translated too. Detecting and handling these cases is a lot harder already.
Extracting comments is a lexical issue, and mostly a quite simple one.
In a few hours, you could write (e.g. with flex) some simple command line program extracting them. And a good editor (such as GNU emacs) could even be configured to run that filter on selected code chunks.
(handling a few corner cases, such as raw string literals, might be slightly more difficult, but these don't happen often and you might handle them manually)
BTW, if you are assigned to work on that code, you'll need to understand it, and that takes much more time than copy&pasting or editing each comments manually.
At last, I am not sure of the quality of automatic translation of code comments. You might be disappointed. Also, the code names (of functions, of classes, of variables, etc...) matter a lot more.
Perhaps adding your comments in English could be wiser.
Don't forget to use some version control system. You really need one (e.g. git)
(I am not convinced that extracting comments for automatic translation would help your work)
First separate both comment and code part in different file using python script as below,
import sys
file=sys.argv[1]
f=open(file,"r")
lines=f.readlines()
f.close()
comment=open("comment.txt","w+")
code=open("code.txt","w+")
for l in lines:
if "//" in l:
comment.write(l)
code.write("\n")
else:
code.write(l)
comment.write("\n")
comment.close()
code.close()
Now translate comment.txt with google translator and then use
paste code.txt comment_en > source
where comment_en is translated comment in english.

How to translate certain string from lib?

django-registration is missing some translations for german.
See github Search for "". Translations are were but "broken".
I don't want to fork or change localization files localy.
Is it possible to provide translation for some strings in my app/project?
You just mention the 2 methods that are candidates for the translation of a string. If you do not want to fork the project nor change the localized files then I believe there is no other way of translating it.
Last resort method: Make an identical file of django-registration that includes the to-be-translated string and add translations there.
IMO the only way is to fork the project, run ./manage.py makemessages and voila! Translations are there. Another thing you can do is to try to contribute to this package by fork it first and then make a pull request! That's the beauty of open sourcing!

Best practice for localizing long text in Django?

I just got done reading the Django docs on internationalization and something is bugging me.
It seems like the default language strings are themselves used as the keys for retrieving translations. That's perfectly fine for short text, but for paragraphs, it seems like poor design. Now, whenever I change the English (default) text, my keys change and I need to manually update my .po file?
It seems like using keys like "INTRO_TEXT" and retrieving the default language from it's own .po file is the right approach. How have others approached this problem and what has worked well for you?
Yes, you will have to manually update the PO files, but most of the times it will be limited to remove the fuzzy marks on out-of-date translations (gettext marks translations as fuzzy when the original version is modified).
I personally prefer this approach as it keeps the text content in the source code which makes it much more readable (especially for HTML). I also find it hard to find good and concise string identifiers, poorly named identifiers are headache prone.
Django-rosetta will be of great help if you do not want to edit PO files by hand or want to delegate translations to non developers.

Fastest way to mark strings to be translated in a Django project?

What would be the fastest way to mark (I mean marking them with the appropiate {% trans "" %} tag) all the strings (a lot) of the templates in a Django project that hasn't been i18d yet?
AFAIK, there is nothing faster than using PyCharm as said here. Is that right?
Check out https://github.com/rory/django-template-i18n-lint.
I used it as part of a 'code quality' unit test suite to ensure we don't leave strings untagged for i18n in a particular project (and also to help spot which strings needed fixing up when first starting the retrospective i18n process). There's also a -r option when you run it that should automatically make the changes.
However, I didn't use that auto-fix option -- I wanted more control over things (eg blocktrans vs trans etc), so I just kept running my tests and fixing up the various places where the linter found missing i18n markup. I also did something similar for gettext imports in Python files, and string markup in Python files, as well as named variable placeholders in strings in Python files, to make translations less likely to mess up word orders.
(The project that I retrospectively i18ned was three years old and pretty large as a result - it took five weeks to get it all straight. I hope yours takes less time)
PS: template-i18n-lint also has a sibling: https://github.com/rory/python-pylint-i18n