I finished a few days ago, creating a translation file for Django-LFS (Lightning Fast Shop) for my native language (pt_BR). Now that it's done, I need to install the ".po" file that I downloaded after finishing my translation in Transifex.
Ok, I downloaded the file, but now that I need to install it, I just can't figure how. I tried putting the file in 'lfs-installer' folder, using "bin/django compilemessages -a", tried the same thing but with the file in many different folders, but I just can't make my LFS use my translation file...
Does anyone know how to make a translation package work on lfs? Or am I doing something wrong?
Thanks
put you .po file in the path:
<your_django_project>/conf/locale/pt_BR/LC_MESSAGES/
and run
django-admin makemessages -a
If you do not already have one, create a folder called Locale. Then in your settings.py folder you must tell it where to find the locale paths. Something like this:
LOCALE_PATHS{
C:/sdalfjasd/dfalsdjkf/locale
}
Also make sure you have locale middleware in your middleware...
You can probably effectively track down that syntax and specifics, now that I pointed you in the right direction.
Once you have those things set up, you can run your makemessage -a command, which will create a folder in your locale folder for the -a you put in. Then you can browse to this, inside it there should be a .po file (there might not be). If there is not, just put your .po file you made in there.
Then browse to your project in your CMD, and run compilemessages -a. This should compile your .po files into .mo files, the files necessary for translations to work.
Hopefully I did not go crazy off-track...
I made it to work using the following settings:
import os
DIRNAME = os.path.dirname(__file__)
USE_I18N = True
USE_L10N = True
LANGUAGE_CODE = 'pt-br'
LANGUAGES = (
('pt-br', u"Português"),
)
LOCALE_PATHS = [
DIRNAME + '/locale',
]
Than create a locale folder aside of the settings.py folder and follow Django official instructions. The desired path for your django.po file is: locale/pt_BR/LC_MESSAGES/django.po. After that, use compilemessages tool and restart the server.
It should work.
Tip: django-lfs uses locale module to handle currency display, but there is a bug for locale module that makes it to show 1234,00 R$ instead of R$ 1234,00. If it bites you, put the following into your settings.py:
# Fix for LC_MONETARY bug: http://www.sourceware.org/bugzilla/show_bug.cgi?id=1294
import locale
locale._override_localeconv.update({'p_cs_precedes': 1, 'n_cs_precedes': 1})
Good luck.
Related
I'm using django and need to decouple setting data from source code,
because setting information must be hidden.
so tried python-decouple module.
I tried .ini and .env file both.
when using setting.ini file, I located it next to setting.py(same directory)
when using setting.env, located setting.py's parent derictory.
both occur error like this.
SECREAT_KEY not found. Declare it as envvar or define a default value.
setting.ini file
[settings]
SECRET_KEY=1234
setting.env file
SECRET_KEY=1234
source code in setting.py
from decouple import config
SECRET_KEY = config('SECRET_KEY')
I already installed python-decouple
pip install python-decouple
how can I fix it?
please help me
os = window
I figured it out.
reson was .ini file's name.
file name must be settings.ini and my file name was setting.ini
To be honest I don't see the need for a special module for this. You could just as well create a py file next to the settings.py file (I tend to name it something like local_settins.py), add your settings and secrets to that file, then just add from local_settings import * to settings.py. But I guess everyone has their own preference. :)
I'm starting on my first large django project and have realized that the default directory structure isn't going to work well.
I saw this question and answer and decided to implement something similar.
Large Django application layout
What do I need to do to make something like this work? How can I change where django looks for apps/modules?
Python works automatically with deep directory structures. That's porbably you didn't find any instructions on how to do it.Here are some instructions on how to get classes and models to work.
If you want to have a module in folder yourproject/apps/firstapp you can just add it to INSTALLED_APPS by adding line 'apps.firstapp',. You will have to add a __init__.py file to each of the directories so that they are recognized as python packages.
When you import classes you can simply use from yourproject.apps.firstapp.filename import yourclass.
You should also make sure all template directories are listed in TEMPLATE_DIRS.
I have two methods to handle this; one for production and one for development.
In development:
Append the path to your apps in your settings.py. In my case, I have my main project in ~/hg/django/project/ and my apps are in ~/hg/django/apps/. So I use:
if DEVEL:
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'apps'))
I also use the production method on my development box. This method has a knock on effect of letting me roll back to the most recent production version by simply commenting out the line path insertion in the settings.py.
In production:
I install the apps using distutils so that I can control deployment per server, and multiple projects running on each server can all access them. You can read about the distutils setup scripts here. Then, to install, you simply:
./setup.py install
if you add:
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
to your settings .py and the following to your manage.py:
sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))
sys.path.insert(0, join(settings.PROJECT_ROOT, "lib"))
then you can include them in your installed apps just as you would if they were in your project root:
INSTALLED_APPS = (
#local apps
'myapp',
#local libs
'piston_dev',
)
this allows you a bit more freedom to move apps around without having to redeclare imports and such.
Im trying to learn Django by looking at examples, but Im having a bit of a problem running the examples I find.
I downloaded 'cheeserator' from https://github.com/jacobian/cheeserater
and I tried running it with python manage.py runserver
but I get the following error -
Error: Can't find the file
'settings.py' in the directory
containing 'manage.py'. It appears
you've customized things. You'll have
to run django-admin.py, passing it
your settings module. (If the file
settings.py does indeed exist, it's
causing an ImportError somehow.)
What am I doing wrong?
You need to have a settings.py file.
As per the instructions in the link provided:
Then you'll want to create a settings.py file in this directory containing::
from settings_template import *
# Override any settings you like here.
Or if you don't want to override anything rename settings_template.py to settings.py
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to manage local vs production settings in Django?
I have managed to deploy successfully a Django project on Apache's Web Server with mod_wsgi.
I would like some recommendations on how to manage multiple settings.py files. Right now I have one for development and one totally different for production (regarding DB parameters, static content localization, and stuff like that). My settings.py file is versioned (don't know if this is a good practise) and I deploy it with something like:
$ hg archive myproject.tbz2
$ cd /path/of/apache/web/project/location
$ bzip2 -db /home/myself/myproject/myproject.tbz2 | tar -xvf -
It's working OK. But I find myself manipulating multiple settings.py files.
I guess my question is: what are the best practices when deploying DJANGO PROJECTS regarding multiple settings.py file versions?
I use a settings module that is not a single file:
settings/
__init__.py
_base.py
_servers.py
development.py
production.py
testing.py
The __init__.py file is simple:
from _servers import get_server_type
exec("from %s import *" % get_server_type())
The _base.py file contains all of the common settings across all server types.
The _servers.py file contains a function get_server_type() that uses socket.gethostname() to determine what type of server the current machine is: it returns development, production or testing.
Then the other files look a bit like (production.py):
DEBUG=False
TEMPLATE_DEBUG=False
from _base import *
In each of these files, I put in the settings that only apply to this server type.
The trick that seems to be the most common is to maintain both a settings.py and local_settings.py (one for each environment) file.
Environment agnostic settings go into settings.py and at the bottom of the file, you'll import from local_settings.py
try:
from local_settings import *
except ImportError:
pass
You can override any settings.py settings in the appropriate local_settings.py
django-admin.py / manage.py both accept a --settings=mysite.settings option. In development you could explicitly specify --settings=dev_settings. You can also set the DJANGO_SETTINGS_MODULE environment variable in your apache configuration.
Personally, I simply don't check in settings.py. Instead I check in multiple settings files (dev_settings, prod_settings, etc) and symbolically link them to settings.py as desired. This way if I simply checkout my application it won't be runnable until I think about which settings file is appropriate and actually put that settings file in place.
Another suggestion I've heard but I don't particularly like is having a settings.py that dynamically imports a dev_settings.py if it exists. While this may be more convenient I'd be concerned that it's harder to read settings.py and know what the settings will actually be without also looking for overriding values in a dev_settings.py file that may or may not exist.
My preferred way is to load a separate ini file using ConfigParser, based off a single setting or environment variable. Anyway in the django wiki there are many different options described: http://code.djangoproject.com/wiki/SplitSettings
I installed python separated from yum.
Now, I need to recompile the language pack for the OSQA system, but get this message:
Error: errors happened while running xgettext on __init__.py
xgettext: ./Django-1.2.3/tests/regressiontests/views/__init__.py:1: Unknown encoding "utf8". Proceeding with ASCII instead.
xgettext: Non-ASCII string at ./Django-1.2.3/tests/regressiontests/views/__init__.py:7.
Please specify the source encoding through --from-code or through a comment
as specified in http://www.python.org/peps/pep-0263.html.
I tried to set encode at utf-8 in the manage.py file but it didn't work.
Can someone help me to solve this issue?
I know this post is outdated but I had the same issue today, and it took me hours to find out why. Maybe people will be in the same case :
My virtualenv is in my django root directory :
Here is my project tree :
DjangoDirectory:
my_env
Django_App1
Django_App2
...
...
manage.py
When I launch command :
./manage.py makemessages -l fr
I get the same error :
Error: errors happened while running xgettext on __init__.py
...
In fact, I noticed that xgettext looked into ALL the files in my folder, as well as files in my_env.
So I found the -i flag which ignore files or folders during the makemessages process
So now, with the command below it works like a charm and I don't get the error anymore.
./manage.py makemessages -l fr -i my_env
Hope it will help
Actually yes, I've already had similar problems with makemessages, because on top of every source file I wrote "# coding: utf8". Even though it worked with source compilation, I've had to replace "utf8" with "utf-8" in every file.
If you're not used to makemessages, take care of gettext functions applied to format strings, you will need strings to contain named parameters when there is more than one placeholder.
"%s" is good
"%(max)s" is good too
"%(min)s %(max)s" too
"%s %s" is not ok.
Actually if you did all the configurations correctly in settings.py, views and templates and you installed gettext and all good, then you may want to check where your virtual environment is. For instance if it's inside your root folder in your project folder your structure I suppose is myapp_project/venv/
Also I'm assuming you've you created an empty folder called locale in your root myapp_project folder.
Try to run it like this if you're translating french for example:
and note: replace venv with whatever you named your virtual environment.
This is the short answer
django-admin.py makemessages -l fr -i venv
this above will get you the local/fr/LC_MESSAGES/django.po but you now have to run the second command to compile the .po file created by makemessages to a .mo file in the LC_MESSAGES folder
So, then run:
django-admin.py compilemessages
now this should get you the django.po file.
This is the long answer
If you configured your urls.py correctly with i18n_patterns, and you filled the translations of msgstr "" in your .po file and then run django-admin.py compilemessages again you can now run your server and go to your desired page 127.0.0.1:8000/fr/ and you should see your translations.
In case you are wondering what your settings.py file should look like (for french). It should look like this at least part of it.
from django.utils.translation import gettext_lazy as _
LANGUAGES = [
('fr', _('French')),
('en', _('English')),
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
Also in settings.py in the MIDDLEWARE list make sure you add the component. 'django.middleware.locale.LocaleMiddleware' and it's important where on the list you put that component as it executes from top to bottom. So put it under the sessions component.
For your main app (you may have a few or one) urls.py file make sure you import from django.conf.urls.i18n import i18n_patterns.
Next on the same urls.py file ensure that you actually add/or edit the url patterns alright. Here is an example of what it could look like:
urlpatterns += i18n_patterns (
path('', include('pages.urls')),
path('meals/', include('meals.urls')),
prefix_default_language=False
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You can play with this and obviously on the paths above it don't include the admin. So just play around but just giving you an idea of what it should look like in order for /fr/ url to work.
You will also notice above the line prefix_default_language=False this is to prevent the /en/ default pattern. In this example we are translating from English to French. So no need to have /en/ in the pattern.
Don't forget in your templates... yes all of them, it don't matter if it's parent template or not. You have to include {% load i18n %} for this to work.
Important to Note:
When you go back to your templates files and you start updating your text content and adding your transblocks etc. After you do this. Go back to your terminal and stop your server, and again if you have your venv folder in your root you must run these commands:
django-admin.py makemessages -l fr -i venv
django-admin.py compilemessages
Now you can open your django.po file and add your translations to the edited text content in your templates. If your venv folder ins't in your root folder. You can just run django-admin.py makemessages -l fr instead.
If you don't do this, you will end up doing things manually in your .po file. You don't wanna do that.
I hope this helped.
I've created a ticket for this at http://code.djangoproject.com/ticket/15980.
It appears to be a simple typo in the Django code, the problem being that python treats "utf8" as an alias for "utf-8", but xgettext does not. The problem still exists as of Django r16169 (05/06/11 12:49:06) in SVN.
EDIT: The issue has been fixed now in the Django source (as of May 2011).
Recently, I had the same issue, and I couldn't find a fitting solution online. After a few tries, I resolved the error in my case. While creating the trans tags, make sure you do this for each paragraph excluding the paragraph breaks.
Instead of:
<p>
{% trans "
Paragraph I
Paragraph II
" %}
</p>
This may solve the error:
<p>
{% trans "Paragraph I" %}
</p>
<p>
{% trans "Paragraph II" %}
</p>