I18n

2,046 views
1,923 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,046
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • I18n

    1. 1. i18n & L10n Thierry Sans
    2. 2. Internationalization (i18n)” Internationalization is the process of designing a softwareapplication so that it can be adapted to various languages andregions without engineering changes. “ Wikipedia➡ Make an application language agnostic
    3. 3. Localization (L10n)” Localization is the process of adapting internationalizedsoftware for a specific region or language by adding locale-specific components and translating text. “ Wikipedia➡ Adapting an application for a specific language (aka locale)
    4. 4. It is is not only about language translation • Number format • Date/Time • Sort orders • Units and conversion • Currency • Paper size
    5. 5. How does it work
    6. 6. Configure your locale preference
    7. 7. GET index.html
    8. 8. GET index.html
    9. 9. GET index.htmlGoogle Français
    10. 10. GET index.htmlGoogle Français
    11. 11. What are the difference locales? http://www.i18nguy.com/unicode/language-identifiers.html
    12. 12. Step 1 - Configure Django
    13. 13. settings.py# Language code for this installation. All choices can be found here:# http://www.i18nguy.com/unicode/language-identifiers.htmlLANGUAGE_CODE = en-us# If you set this to False, Django will make some optimizations so as# not to load the internationalization machinery.USE_I18N = True# If you set this to False, Django will not format dates, numbers and# calendars according to the current localeUSE_L10N = True
    14. 14. Detects your locale from the HTTP request settings.pyMIDDLEWARE_CLASSES = ( django.middleware.locale.LocaleMiddleware, django.middleware.common.CommonMiddleware, django.contrib.sessions.middleware.SessionMiddleware, django.middleware.csrf.CsrfViewMiddleware, django.contrib.auth.middleware.AuthenticationMiddleware, django.contrib.messages.middleware.MessageMiddleware,)
    15. 15. settings.pyINSTALLED_APPS = ( django.contrib.auth, django.contrib.contenttypes, django.contrib.sessions, django.contrib.sites, django.contrib.messages, django.contrib.staticfiles, WebDirectory, HelloLocale, # Uncomment the next line to enable the admin: django.contrib.admin, # Uncomment the next line to enable admin documentation: # django.contrib.admindocs,)
    16. 16. Step 2 - Marking
    17. 17. Mark translation messages from a template HelloLocale/templates/HelloLocale/index.html{% load i18n %}<html><head> <title>{% trans "Hello World!" %}</title> ...</head><body> <form action=sayhello/ method=post> {% csrf_token %} <label>{% trans "Your name" %}</label> <input type="text" name="name"/> </form> <input type="button" value="{% trans "send" %}" onclick=" ...<body></html>
    18. 18. Mark translation messages from views.py HelloLocale/views.py from django.utils.translation import ugettext as _ def sayHello(request): name = request.POST[name] output = _(Hello %(name)s!) % {name : name} return HttpResponse(strip_tags(output))
    19. 19. Step 3 - Creating a locale
    20. 20. Create a locale• Create a locale directory in the app•$ cd HelloLocale The locale identifier$ mkdir Locale$ django-admin.py makemessages -l fr➡ Generates a .po file
    21. 21. Define the translations HelloLocale/locale/fr/LC_MESSAGES/Django.po#: views.py:20#, python-formatmsgid "Hello %(name)s!"msgstr "Bonjour %(name)s!"#: templates/HelloLocale/index.html:4msgid "Hello World!"msgstr "Bonjour tout le monde!"#: templates/HelloLocale/index.html:11msgid "Your name"msgstr "Votre nom"#: templates/HelloLocale/index.html:13msgid "send"msgstr "envoyer"
    22. 22. Compile locales$ django-admin.py compilemessages➡ Creates a .mo file
    23. 23. Update locales$ django-admin.py makemessages -a$ django-admin.py compilemessages
    24. 24. Custom locale
    25. 25. Custom locale from django.utils.translation import ugettext as _ def sayHello(request): language = en-us translation.activate(language) request.LANGUAGE_CODE = translation.get_language() name = request.POST[name] output = _(Hello %(name)s!) % {name : name} return HttpResponse(strip_tags(output))
    26. 26. Idea• The language preference is stored in the user’s profile• When the user is authenticated, store the language in a cookie or a session

    ×