I have my translation strings only in templates (stored in the project_dir/Templates), I tried running the $ django-admin.py createmessages -l ru both in the project root directory and in the app directories that use templates with trans. strings. It created folders locale/ru/LC_MESSAGES but the folders were empty. I tried to add the django.po files manually (with the syntax mentioned in the l10n docs). And ran the createmessages -a and compilemessages commands. It created the .mo files, but the translations didn't appear in the browser.
As I created the .po files manually I had no lines starting with #. What should I write there?
My template files are in different folder than the .py files for apps. Should I put some extra links to them?
did you try :
python manage.py makemessages -a
from project root and app ?
this should create a .po that you have to edit.
be sure to remove 'fuzzy' stuff everywhere.
then :
python manage.py compilemessages
You need to restart the server
For newer versions of Django (e.g. 3.2):
in the root directory create folder "locale"
run command django-admin makemessages -l ru
update your language files (located in the locale folder)
run django-admin compilemessages
Configure the LOCALE_PATHS in settings.py, otherwise you won't see the translations:
LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')]
LANGUAGE_CODE = 'ru'
To fix empty po files:
Make sure you did the needed changes for the templates as mentioned in the documentation. Please make sure that you are checking the correct documentation version for your project.
You can add a locale directory in your templates directory and add its path to the LOCALE_PATHS list. (Optional, but helpful to make sure that the template directory is included in step 4)
Go to the project_dir (you should run the next command in a parent directory of the files to be translated)
Run the command django-admin makemessages -l ru
Related
I'm getting an odd error when I make the i18n files of a django project:
(venv) user#machine:~/path/to/repo$ django-admin makemessages -l es
It creates fake .py files for every .txt files:
For example, requirements/base.txt
Django==1.10.6
django-environ==0.4.1
djangorestframework==3.6
psycopg2==2.7
djangorestframework-jwt==1.9.0
Markdown==2.6.8
unipath==1.1
It generates a requirements.base.txt.py with 'XXXXXX' in it:
XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXX
XXXXXXXXXXXX
But it also creates the right .po files into /locale
Could you please point me in the right direction? Because I'm lost. Thank you!
For everyone that is having the same problem, this can also occur if you haven't created a locale/ folder within each app of your Django project, as it's the default folder if none specified in your settings.
After creating it, my problem went away.
More info in the documentation
The answer is here : Permission denied in Django makemessages
To make a long story short: makemessages misbehaves because it doesn't know your project's settings. Actually this should be reported as a defect (if it hasn't already) since most other commands that needs the settings to properly work detect the fact and do raise an ImproperlyConfigured error with an explicit message.
When I run manage.py makemessages I found some messages which were in the .po file like this:
msgid "Example"
msgstr "Example"
Transformed to this, after I ran the command:
#~ msgid "Example"
#~ msgstr "Example"
What does #~ means? Since the translation of those messages doesn't work anymore, I suppose it is a comment.
What can I do to prevent Django commenting out (or "#~ing") pre-existing messages in the translation file?
Django will comment-out all messages that are no longer in your code. It won't remove them, so you won't lose it, but that way this messages won't end up in compiled .mo file.
I was facing a similar problem with 3rd party apps. makemessages did not include them in the .po file and when adding them manually makemessages was commenting them out next time.
In my case I had the virtual env symlinked into the project folder. To make makemessages see those 3rd party apps I had to add -s
./manage.py makemessages -a -s -l de -i faker -i openid -i gunicorn
At the same time I want to exclude some apps from translations with -i
I have custom locale path in my settings
PROJECT_ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
LOCALE_PATHS = (
os.path.join(PROJECT_ROOT,'templates','v1','locale'),
)
but when i'm trying to create .po files, i've get error:
$ python manage.py makemessages --locale=ru
CommandError: This script should be run from the Django Git tree or your project or app tree. If you did indeed run it from the Git checkout or your project or application, maybe you are just missing the conf/locale (in the django tree) or locale (for project and application) directory? It is not created automatically, you have to create it by hand if you want to enable i18n for your project or application.
Why django doesn't want to use LOCALE_PATHS?
Absolute paths are:
PROJECT_ROOT = '/home/creotiv/ENVS/project_env/project/project'
LOCALE_PATHS = ('/home/creotiv/ENVS/project_env/project/project/templates/v1/locale')
PS: Also i added translation to main app and django doesnt see it either.
First things first; You need to give correct path of the manage.py to the python.
$ python /path/to/the/manage.py makemessages --locale=ru
Or go to directory which is included manage.py. After that run that command you've wrote in your question.
$ python manage.py makemessages --locale=ru
Next; Did you create ./templates/v1/locale directories under the project home?
According to error you may check existence of the ./templates/v1/locale' folder.
Also; You may need to append LANGUAGES setting in to project settings.py file:
LANGUAGES = (
('ru', _('Russian')),
('en', _('English')), # (Optional)
)
Appendix:
If you are developing under virtual environment, do not forget to enable virtual environment first.
Maybe you have to go inside the app or project directory. Try this:
$ ls # should return also the manage.py
$ cd myproject
$ ls # should return also the settings.py
$ python ../manage.py makemessages --locale=ru
Just updated to django 1.7 and problem gone.
I don't understand why this doesn't find the command:
khandelwal#simba:/opt/code/cdcschool$ python schoolcommand/manage.py createcampaign
Unknown command: 'createcampaign'
Type 'manage.py help' for usage.
When this works fine:
khandelwal#simba:/opt/code/cdcschool$ cd schoolcommand/
khandelwal#simba:/opt/code/cdcschool/schoolcommand$ python manage.py createcampaign
Error: Provide: <type start_date end_date>
This is where my command is located:
$ ls schoolcommand/management/
commands __init__.py __init__.pyc
$ ls schoolcommand/management/commands/
campaignmanager.py createcampaign.py __init__.py
campaignmanager.pyc createcampaign.pyc __init__.pyc
How do I fix it so that I can do:
khandelwal#simba:/opt/code/cdcschool$ python schoolcommand/manage.py createcampaign
Here are the values of my PYTHONPATH, DJANGO_SETTINGS_MODULE and the one place my settings.py is located.
khandelwal#simba:/opt/code/cdcschool$ ls
Procfile README requirements.txt schoolcommand
khandelwal#simba:/opt/code/cdcschool$ echo $PYTHONPATH
khandelwal#simba:/opt/code/cdcschool$ echo $DJANGO_SETTINGS_MODULE
khandelwal#simba:/opt/code/cdcschool$ find . -name settings.py
./schoolcommand/settings.py
khandelwal#simba:/opt/code/cdcschool$
When you use Django, there are two important rules.
First.
You have a settings.py file which must be used by the web server and all the manage.py commands. All of them.
The default place to look for the settings.py file is the current working directory. You can change this with the PYTHONPATH and the DJANGO_SETTINGS_MODULE environment variable.
The manage.py is created for you in the same directory as the settings.py.
You can use django-admin.py --settings=some.module if you don't want to use manage.py.
Second.
The manage.py commands do not have any "path" to them. They're all just one-word commands, no matter where they happen to live in your application tree.
You never do this: python schoolcommand/manage.py createcampaign unless (somehow) your settings.py is not in the same directory as your manage.py.
You normally do this:
cd /path/to/your/settings
python manage.py createcampaign
If your settings is in code/schoolcommand that means that your web site and all your commands will operate in that directory.
print :
D:\zjm_code\register2>python D:\Python25\Lib\site-packages\django\bin\django-adm
in.py makemessages -l cn
Error: This script should be run from the Django SVN tree or your project or app
tree. If you did indeed run it from the SVN checkout or your project or applica
tion, maybe you are just missing the conf/locale (in the django tree) or locale
(for project and application) directory? It is not created automatically, you ha
ve to create it by hand if you want to enable i18n for your project or applicati
on.
2.i made a locale directory ,and
D:\zjm_code\register2>python D:\Python25\Lib\site-packages\django\bin\django-adm
in.py makemessages -l cn
processing language cn
Error: errors happened while running xgettext on __init__.py
'xgettext' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\Python25\lib\site-packages\django\core\management\base.py:234: RuntimeWarning
: tp_compare didn't return -1 or -2 for exception
sys.exit(1)
3.
ok
http://hi.baidu.com/zjm1126/blog/item/f28e09deced15353ccbf1a82.html
is register2 your project or app tree?
did you make directory register2\\locale?