I'm Korean and using django-all-auth in my Django Project.
I checked that there is Korean .po file in django-all-auth.
But all expressions are english, not Korean.
I just followed Installation part and Configuration part in doc.
And here is my settings.py
LANGUAGE_CODE = 'ko-kr'
TIME_ZONE = 'Asia/Seoul'
USE_I18N = True
USE_L10N = True
USE_TZ = True
Did I miss something?
You need to activate the internationalization middleware:
In your settings.py set:
MIDDLEWARE = {...,"django.middleware.locale.LocaleMiddleware",...}
As said in the Doc
Make sure you’ve activated translation for your project (the fastest
way is to check if MIDDLEWARE includes
django.middleware.locale.LocaleMiddleware). If you haven’t yet, see
How Django discovers language preference.
Since now your pages are configured to route to the desired internationalization you now need to provide localization yourself. They will not automatically be translated. You need to handle it yourself.
To do so you will have to mark the elements that need to be translated by using
from django.utils.translation import ugettext as _
Now mark the text that you need to translate by using ugettext as _in the following way:-
class PollPluginPublisher(CMSPluginBase):
model = PollPluginModel # model where plugin data are saved
module = _("Polls")
name = _("Poll Plugin") # name of the plugin in the interface
Now the elements are marked to be translated in our case ("Polls") and ("Poll Plugin").
After dealing with this you can run following command in your root directory:-
django-admin makemessages -l de
Replace the last "de" with your language locale name. What this command does is that it will create the po file which will just store the elements that need to be translated. Be sure that your LOCALE_PATH is set properly.
After this done you can use the following django-packages for translations:-
1) django-rosetta :- https://django-rosetta.readthedocs.io/en/latest/
2) django-modeltranslation:- http://django-modeltranslation.readthedocs.io/en/latest/
For further reference of LOCALIZATION you can view:-
https://docs.djangoproject.com/en/1.10/topics/i18n/translation/#localization-how-to-create-language-files
Related
We're developing an application which will serve multiple industries. I'm hoping to use django's i18n functionality to accomplish two things:
Translation to different languages (standard).
Translation to different industries (much less standard).
Lets say the application serves vets and car mechanics, you would end up with a matrix of options:
| English | French
----------------------------------
Vets | horse | cheval
----------------------------------
Car Mechanics | car | voiture
I guess I can setup the message files for different contexts pretty easily:
python manage.py makemessages -l fr_vet
etc...
Now how would I go about activating that translation?
I know the industry in middlewhere from the request, could I subclass django.middleware.locale.LocaleMiddleware and alter it, or do I need to subclass django.utils.translation and alter the activate function? Or something completely different?
Appologies if I've missed an existing explanation of how to do this - it was a classic case of "I'm sure the answer must exist but without knowing what it's called I can't google it".
I had the same situation once and I read almost all Django documentation about translations (it's really huge)... I would like to share with you some important links that can help:
Switch language in templates:
https://docs.djangoproject.com/en/dev/topics/i18n/translation/#switching-language-in-templates
This one, I think is the most important one for you, "How django discovers language..."
https://docs.djangoproject.com/en/dev/topics/i18n/translation/#how-django-discovers-language-preference
One of the things that I think you are missing are the languages in your settings file, for example:
LANGUAGES = (
('de', _('German')),
('en', _('English')),
)
Lara's answer was useful, but didn't explain it fully, I thought it worthwhile to add a full explanation.
All that's required is to add middleware which sets the translation code for the industry:
from django.middleware.locale import LocaleMiddleware
from django.utils import translation
class CustomLocaleMiddleware(LocaleMiddleware):
def process_request(self, request):
check_path = self.is_language_prefix_patterns_used()
language = translation.get_language_from_request(
request, check_path=check_path)
if hasattr(request, 'user_info'):
language = language[:2] + '-' + request.user_info['trans_code']
translation.activate(language)
request.LANGUAGE_CODE = translation.get_language()
(Note: user_info is added to request in middleware which is called before CustomLocaleMiddleware)
request.user_info['trans_code'] will be set to "ve" for vets and "cm" for car mechanics.
Then in settings:
MIDDLEWARE_CLASSES = (
...
'path.to.middleware.CustomAuthMiddleware', # this adds request.user_info
'path.to.middleware.CustomLocaleMiddleware',
...
)
LANGUAGE_CODE = 'en'
USE_I18N = True
USE_L10N = True
LANGUAGES = (
('en-ve', 'English Vets'),
('en-cm', 'English Car Mechanics'),
('fr-ve', 'French Vets'),
('fr-cm', 'French Car Mechanics'),
)
LOCALE_PATHS = (os.path.join(BASE_DIR,'locale'),)
You can create your .po files with
python manage.py makemessages -l en_VE -l en_CM -l fr_VE -l fr_CM
This has the advantage that it would default to the standard "de" translation if you had it but hadn't yet build "de-ve" or "de-cm".
I'm trying to use django-tinymce to edit fields in django admin.
I've the app installed in my virtualenv (django-tinymce==1.5.1b4). It's listed in my installed apps -
INSTALLED_APPS = (
#...
'tinymce',
#...
)
My settings includes the following
TINYMCE_DEFAULT_CONFIG = {
'theme': "advanced",
'theme_advanced_toolbar_location': "top",
'theme_advanced_buttons1': "bold,italic,underline,separator,"
"bullist,separator,outdent,indent,separator,undo,redo",
'theme_advanced_buttons2': "",
'theme_advanced_buttons3': "",
}
TINYMCE_SPELLCHECKER = True
TINYMCE_COMPRESSOR = True
And I've got the files available at /MEDIA_ROOT/js/tiny_mce (the default).
My models look like -
from tinymce import models as tinymce_models
class MyModel(models.Model)
post = tinymce_models.HTMLField()
When I go to the model admin page, the field appears as a normal text field and my browser tells me there's an error on the inline js script for the field. It says it doesn't recognise the variable tinyMCE. It doesn't look like the page is even trying to load the js file (I'm getting no 404's - I can't see any sign of anything being loaded).
I'm not sure what I'm missing..
Have You, Sir, done python manage.py collectstatic ?
What value variable in settings.py is in TINYMCE_JS_ROOT and TINYMCE_JS_URL
If variable TINYMCE_JS_URL is not set check if You, Sir, have file at /MEDIA_ROOT/js/tiny_mce/tiny_mce.js. If not, try to copy manually from django-tinymce's egg.
OK, looks like it might have been a bug in the django_tinymce code. I've reverted to 1.5.1b2 and everything works as expected. Guess I should look into filing a bug report.
I wanna get absolute path all installed_apps in "setting.py" .
app_list = [ app for app in Myproject.settings.INSTALLED_APPS ]
I list of application but how i can get absolute path all of them.
Thank you.
It's not really possible to do what you want. I mean you can get almost there, but there's no real functionality for this, and in particular, Python doesn't distinguish between classes, methods, and variables. They all count as attributes of the module.
First, INSTALLED_APPS is a tuple of strings. So, in order to get any useful information, you're going to have load them as modules:
for app in settings.INSTALLED_APPS:
module = __import__(app)
Then, once you have the actual module, you can call dir on it to get a list of its attributes:
for app in settings.INSTALLED_APPS:
module = __import__(app)
print dir(module)
And, that's about as far as you can go. It's going to output everything set in the module, not just classes. From here, about the only recourse I can think of to weed out everything but classes is to assume that naming conventions were followed and look for items that start with a capital letter. That's not exactly scientific, but that's all you got.
Today I solved my problem. I write this code and its just working. For limited name class I find name of class that inherent from "models.Model" . you can change it and enjoy that.also this code find files in depth 1 of modules. it can change.
app_list = [app for app in training.settings.INSTALLED_APPS if "task" in app]
for module in app_list:
module1 = __import__(module)
temp = module1.__path__
files_path = [temp[0] + os.sep + files_name for files_name in os.listdir(temp[0]) if
os.path.splitext(files_name)[1] == ".py"]
p = re.compile(r'class\s*\w*\s*\(models.Model\):')
for file in files_path:
infile = open(file)
text = infile.read()
all_class = p.findall(text)
print [class_name[6:][:-15] for class_name in all_class]
At first sorry for my English :).
LANGUAGE_CODE setting doesn't work correctly.
When I configured LANGUAGE_CODE="mn", but default language code is "en".
from django.utils.translation import get_language
print get_language()
>>> en
then I tried to configure LANGUAGES setting
LANGUAGES = (
("mn": "Mongolia"),
("en": "English"),
)
but still "en"
changed LANGUAGES setting
LANGUAGES = (
("mn": "Mongolia"),
("en-us": "English"),
)
now it is "mn"
but want above settings
LANGUAGE_CODE = "mn"
LANGUAGES = (
("mn": "Mongolia"),
("en": "English"),
)
It doesn't work correctly. Is it BUG ? or something else?
I also tried creating "mn", "en" locale.
Hope help me. Thanks.
If you haven't already, be sure to read this specific topic in the Django documentation:
How Django discovers language preference
https://docs.djangoproject.com/en/1.3/topics/i18n/deployment/#how-django-discovers-language-preference
We're not using Django 1.3 yet, but we are using Django with multiple languages.
The first item to check is to verify the Accept-Language HTTP header being sent by your browser. Use Fiddler or Charles Proxy or similar tool to verify. Sounds like your browser may be sending "en-us" as a language preference.
In your settings file, make sure USE_L10N and USE_I18N are set to True.
https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
https://docs.djangoproject.com/en/dev/ref/settings/#use-l10n
I already set USE_L10N = True in settings.py
But in following view:
from django.contrib.humanize.templatetags.humanize import intcomma
dev view_name(request):
output = intcomma(123456)
Output is always "123,456" for all locales.
Intcomma only respects the localization settings in Django 1.4 and higher.
In the meantime, remove intcomma and enable USE_THOUSAND_SEPARATOR.
Note that this enables thousand separators on all integers.
I think intcomma() does the same for all locales:
def intcomma(value):
"""
Converts an integer to a string containing commas every three digits.
For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
"""
orig = force_unicode(value)
new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', orig)
if orig == new:
return new
else:
return intcomma(new)
intcomma.is_safe = True
register.filter(intcomma)
You can modify this function and pass the separator as an argument.
If you are printing that in a template, you can set in settings.py:
USE_THOUSAND_SEPARATOR=True
THOUSAND_SEPARATOR='.'
NUMBER_GROUPING=3
making the 3 changes above, DECIMAL_SEPARATOR will automaticaly become ','. But you can also set it:
DECIMAL_SEPARATOR=','
this way you dont need humanize, but i guess this will affect all your applications under this settings.py file.
import locale
locale.format("%d", 123456, True)