Skip to main content

A package to implementing i18n in django

Project description

i18n engine for django-framework

internationalization and localization in django

This Python package provides functionality to generate translation files for Django projects based on JSON file data.

Author

Installation

  • To install the package, use the following command:
pip install mt_django_i18n

Setup

Before using library, you need setup some things in your django project, please follow this steps

  1. Setup all required variables in your settings.py
from django.utils.translation import gettext_lazy as _
# Mention the lanuage with respective language code which you will be using in your project
LANGUAGES = [
    ('en', _('English')),
    ('fr', _('French')),
   ('es', _('Spanish')),
   ('de',_('German')),
   ('en-IN',_('English (India)'))
    # Add more languages as needed

]

LANGUAGE_CODE = 'en-in' # default =  'en'

LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale'),  # Optional, default path './locales' folder
]

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N=True

LANGUAGES_BIDI = [
    'he', 'ar',  # Add right-to-left languages if required
]

# Below mention localization of number system according to current language code of project

THOUSAND_SEPARATOR=','
DECIMAL_SEPARATOR='.'
USE_THOUSAND_SEPARATOR=True
NUMBER_GROUPING=(3,2,0)
  1. Create a JSON file with translation data in the following format:
{
  "fr": {
    "Hello": "Bonjour",
    "Goodbye": "Au revoir"
  },
  "de": {
    "Hello": "Hallo",
    "Goodbye": "Auf Wiedersehen"
  },
  "es": {
    "Hello": "Hola",
    "Goodbye": "Adiós"
  }
}
  1. Import and use the create_files() function from the translation_file_generator module:
    from mt_django_i18n.json_to_po import create_translator_files
    from django.utils.translation import gettext as _

    create_files(json_file_path,project_name,target_lang)
  1. json_file_path = This parameter represent file path for translation
  2. project_name = Name of project
  3. target_lang = Lagunages which language file should be converted in binary format file i.e .mo files

Example

views.py

from mt_i18n.json_to_po import create_files
import os
from django.utils.translation import gettext as _
# Create your views here.


def blog(request):
    'hello':_('Hello'), # this mark to value stored in _('') are to translated 
    if not (os.path.exists("locale") and os.path.isdir("locale")):
        
        os.mkdir('locale')
        create_files("translator.json","blog",['fr', 'de', 'es'] )
    

Templates - HTML pages

  • The static text to be translated should be marked throught {% trans Hello world! %} or via variable format as mention below
    <p>{{example}}</p>
    <p>{% trans %}</p>
  • For localization
                    {% load l10n %}
                    {% localize on %}
                    <tr>
                      <td><h6 class="text-muted">Category</h6></td>
                      <td> <h6 class="fw-bold">
                        {{my_currency|localize}}
                      </h6></td>
                    </tr>

                    <tr>
                      <td><h6 class="text-muted">Number</h6></td>
                      <td> <h6 class="fw-bold">{{my_number|localize}}
                      </h6></td>
                    </tr>

                    <tr>
                      <td><h6 class="text-muted">Date</h6></td>
                      <td> <h6 class="fw-bold">
                        {{my_date}}
                      </h6></td>
                    </tr>
                  </tbody>
                </table>
                {% endlocalize %}

#currency_format

  • This function help to convert the number into currency format by passing number and language code as shown in below syntax
 from mt_django_i18n.json_to_po import currency_format
 my_currency= currency_format(currency,language='en-in') 
  • Where currency parameter is for passing number/currency and language parameter for language code.

Example

 from mt_django_i18n.json_to_po import currency_format
 my_currency = currency_format(currency=1234567.89,language=request.LANGUAGE_CODE) 

Note Use translation package to current activate language of project.

from django.utils import translation
translation.activate(language)

#number_format

  • This function help to convert the number into number format by passing number and language code as shown in below syntax
 from mt_django_i18n.json_to_po import number_format
 my_number= number_format(number,lang='en-in')
  • Where number parameter is for pass number and lang parameter is for language code.

Example

 from mt_django_i18n.json_to_po import number_format 
 from decimal import *
  my_number=number_format(Decimal(1000000.50) ,lang=request.LANGUAGE_CODE)

urls.py

from django.conf.urls.i18n import i18n_patterns
urlpatterns+=i18n_patterns(
  
    path('',blog,name="blog"),
    # path('language_select/',language_select,name="language_select")
)
# Note: activate the language code of project every time there is
change in language so that translation perform better. 

from django.utils import translation.
translation.activate(language)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

MT_PY_django_I18n-0.0.2.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

MT_PY_django_I18n-0.0.2-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file MT_PY_django_I18n-0.0.2.tar.gz.

File metadata

  • Download URL: MT_PY_django_I18n-0.0.2.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for MT_PY_django_I18n-0.0.2.tar.gz
Algorithm Hash digest
SHA256 337243bd80a897c758ac705879d29dcf2917fad9e0c5c8d856cfd3a5976ab4de
MD5 8ae2b7148688d3e4b509d88c8824cc32
BLAKE2b-256 f6dd59945b0df74b4f9685f10f66379821fb552520c7d1ffa1c2b8c44b8cb272

See more details on using hashes here.

File details

Details for the file MT_PY_django_I18n-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for MT_PY_django_I18n-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2990de25184fe274bb8e53a414a891b557c5647cf457d3e49411c012c7ca30d9
MD5 0d4b8bdcc6283cc6215dfdc50f3bfd90
BLAKE2b-256 dbca124047d0a3ea159270b06869db62d819ba76bdfca88b2c084c479fba39ad

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page