I'm trying to do Internationalization using Django. I've followed through docs and able to set it up right.
My questions here are
i) What are all the language supported by django?
ii) I've created '.po', '.mo' message files for the languages 'de' , 'de-at' but when trying to access those resources from API I'm only able to get resources from 'de' for both the cases. Does Django overrides 'de-at' by 'de' message file resources. Why is it so?
I've created message files using below commands by executing in project directory.
django-admin.py makemessages -l de
After running above command. I've added resources for both the languages corresponding in their '.po' file
django-admin.py compilemessages
Any help would be appreciated.
de-at is not supported by Django. So by default Django takes de message files.
Django documentation says :
In each of these places, the language preference is expected to be in the standard language format, as a string. For example, Brazilian Portuguese is pt-br.
If a base language is available but the sublanguage specified is not, Django uses the base language. For example, if a user specifies de-at (Austrian German) but Django only has de available, Django uses de.
Hence It returns messages inside a file 'de'
If the question is actual this might help you.
I had task to translate website both to de and de-at.
Django supports de-at language.
List of supported languages:
https://msdn.microsoft.com/en-us/library/ms533052(v=vs.85).aspx
Or you can find on git language_info, I saw list of supported languages there too.
If you created in languages de-at - dash is separator and folder in locale named de_AT ( makemessages de_AT) it is enough. Django defines them as different languages. Problem may be in form you are using on front-end side. In my case form that I got from django documentation crashes on sublanguages so try to change your front-end form, you can find them in another stackoverflow questions.
Related
I work on a project where we want to have multilingual site. We start with two languages defined in settings.py
LANGUAGES = (
("en-us", _("United States")),
("cs", _("Czech Republic")),
)
I am not the programmer doing the work but if I understood correctly all we need is to be able to add - for example - French language for the whole website but not via setting.py but Django admin web interface.
LANGUAGES = (
("en-us", _("United States")),
("cs", _("Czech Republic")),
("fr", _("French")),
)
We are using rosetta for translating in Django admin. So I want to use Django admin to add new laguage so it appears in rosetta interface.
Could someone tell me how we can control ( add or remove or disable ) languages from Django admin?
I checked these but did not find the answer
Adding new site language in Django admin
How to manage system languages from django admin site?
https://djangowaves.com/tutorial/multiple-languages-in-Django/
Add translation for model field using django rosetta
Adding languages dynamically through Django Admin
The short answer is that you can't do that.
The settings.py of a Django project is not designed, and not recommended to be modified by the web application.(It can introduce a security breach.)
So I recommend to change LANGUAGES manually, or to enable all languages supported by Django by removing LANGUAGES key. Of course, don't forget to generate message files with the makemessages command.
If you really want such a dynamic feature, your best bet will be to implement it on your own by modifying the Django Rosetta source code.(Define a preference item for supported languages on a DB model, and filter languages by its value.)
Before I ask my question I need to give some context:
I wrote a simple python script that read linux's syslog file and search for certain strings. I have other similar scripts like these (scripts that do file system stuff, scripts that interact with other servers and so on). Most of these scripts write simple write stuff to stdout.
I would like to port these scripts to a web-server so I could simple browser to https://server/syslog and get the same output that I would get by running the script on the command line interface.
According with my research Django seems to be a great choice. I followed some Django tutorials and I was capable of developing some basic django web apps.
My question is: Since django does not have a "controller" where should I place the scripts code? My best bet in the view, but according with djangos documentation it does not make sence.
Extracted from django doc: In our interpretation of MVC, the “view” describes the data that gets presented to the user. It’s not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. It’s a subtle distinction.
The description of MVC is not so important. The typical use of django is for database backed web applications. And this describes a design pattern or paradigm for that. It's completely possible to use django in other ways as well.
If you want to build a django app that is a web interface for your existing scripts, you might not even need the django ORM at all. In any case, you can put as much or as little logic in your view as you want. Your use case might just not fit neatly into the MVC or MVT paradigm. Django views are just python functions (or classes, but Django class based views are more tightly coupled with the ORM).
I would recommend:
leaving your scripts largely as they are, but wrap the parts you want to reuse as
functions. You can keep them functional as standalone scripts with an
if __name__=='__main__':
block to call the functions.
Import the functions to views.py - it doesn't matter where they are as long as your server will always be able to find them. I put mine right in the app directory.
Call the function(s) in your view(s), and return the text to a HttpResponse object which you return from the view. (I think this is more direct than creating a template and a context and calling render, but its not what I usually do so there may be some issues?)
Thats bit old code - but you will get enough idea to start - check https://github.com/alex2/django_logtail (Django_LogTail)
Is there some way to enable/disable or add/delete system languages from django admin interface? Since Django says:
"It reads metadata in your model to provide a powerful and production-ready interface that content producers can immediately use to start adding content to the site."
And django book tells us:
"This is a Web-based interface, limited to trusted site administrators, that enables the adding, editing and deletion of site content."
I assume that main point is the power of manage content site. Then if my language setting enables content in some language in my site, why does django not allows me to modify it? (add/delete language to site).
I would have something like this:
Do you mean to translate the Admin interface itself? If so, this might help. Do you expect to have translation files in DB, in the admin/i18n? I don't think this is not how Django works, it works with .po/.mo files.
The internationalization documentation is really good, maybe a bit too big to digest when you just start, but Django has several switches to control what you want.
A good place to understand what switch to use for your need (at least I found it very interesting) is the implementation notes paragraph in there, and how Django discovers translations which gives a HLD of the logic.
Sorry if my answer look off-topic, but as Lara, I feel like I don't completely understand your question.
I was wondering, if is there any way to add localization to django-rest-framework api. Because I want to change default message errors.
This is not supported well in Django REST framework 3.0, and it's not supported at all in versions before that.
Most of the strings that DRF uses to display things such as error notices are translatable, and some of them match the Django translations, but for the most part you need to manually find out the translation strings and do the translations on your own. The other option is to override the exception handler and translate within there.
With that said, Django REST framework 3.1 provides a guide on translating and is hooked up to Transifex. If you want your translation included by default in 3.1 (and other future releases), help us and recommend translations on Transifex.
I'm translating my website into the 24 languages of the European Union. These include the "Malti" language, that is not listed in django default supported languages.
I would like to know if there is a way to add a custom language to django so it can work with the native i18n url function.
Thanks!
I found the solution, you have to manually add a folder in /django/conf/locale/ with the language extension you want. Actually you can just copy past the en (english) folder and name it after the missing language (in my case mt).
In this folder you can also edit the file formats.py to localize the dates, numbers etc.
Restart django and your language will be natively supported.