MT-PY-django-I18n


NameMT-PY-django-I18n JSON
Version 0.0.3 PyPI version JSON
download
home_page
SummaryA package to implementing i18n in django
upload_time2023-09-25 11:39:37
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

* [Viraj B. Poojari](virajp.in@mouritech.com)

## Installation

* To install the package, use the following command:

```cmd
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
```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)

```
2. Create a JSON file with translation data in the following format:

```json
{
  "fr": {
    "Hello": "Bonjour",
    "Goodbye": "Au revoir"
  },
  "de": {
    "Hello": "Hallo",
    "Goodbye": "Auf Wiedersehen"
  },
  "es": {
    "Hello": "Hola",
    "Goodbye": "Adiós"
  }
}
```

3. Import and use the create_files() function from the translation_file_generator module:
```py
    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
```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 
```html
    <p>{{example}}</p>
    <p>{% trans %}</p>
```
* For localization
```html
                    {% 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

```py
 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
```py
 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.

```py
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

```py
 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
```py
 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
```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
# 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)
``` 

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "MT-PY-django-I18n",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "",
    "author_email": "Viraj <virajpoojari0905@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/a0/0d/1517aa495ece075de15704f61b812eaf2ecfaba79a319b21a6c1b9989f1a/MT_PY_django_I18n-0.0.3.tar.gz",
    "platform": null,
    "description": "# i18n engine for django-framework\r\n###  internationalization and localization in django\r\n\r\nThis Python package provides functionality to generate translation files for Django projects based on JSON file data.\r\n\r\n## Author\r\n\r\n* [Viraj B. Poojari](virajp.in@mouritech.com)\r\n\r\n## Installation\r\n\r\n* To install the package, use the following command:\r\n\r\n```cmd\r\npip install mt_django_i18n\r\n```\r\n## Setup\r\nBefore using library, you need setup some things in your django project, please follow this steps\r\n\r\n1. Setup all required variables in your settings.py\r\n```py\r\nfrom django.utils.translation import gettext_lazy as _\r\n# Mention the lanuage with respective language code which you will be using in your project\r\nLANGUAGES = [\r\n    ('en', _('English')),\r\n    ('fr', _('French')),\r\n   ('es', _('Spanish')),\r\n   ('de',_('German')),\r\n   ('en-IN',_('English (India)'))\r\n    # Add more languages as needed\r\n\r\n]\r\n\r\nLANGUAGE_CODE = 'en-in' # default =  'en'\r\n\r\nLOCALE_PATHS = [\r\n    os.path.join(BASE_DIR, 'locale'),  # Optional, default path './locales' folder\r\n]\r\n\r\nTIME_ZONE = 'UTC'\r\n\r\nUSE_I18N = True\r\n\r\nUSE_L10N=True\r\n\r\nLANGUAGES_BIDI = [\r\n    'he', 'ar',  # Add right-to-left languages if required\r\n]\r\n\r\n# Below mention localization of number system according to current language code of project\r\n\r\nTHOUSAND_SEPARATOR=','\r\nDECIMAL_SEPARATOR='.'\r\nUSE_THOUSAND_SEPARATOR=True\r\nNUMBER_GROUPING=(3,2,0)\r\n\r\n```\r\n2. Create a JSON file with translation data in the following format:\r\n\r\n```json\r\n{\r\n  \"fr\": {\r\n    \"Hello\": \"Bonjour\",\r\n    \"Goodbye\": \"Au revoir\"\r\n  },\r\n  \"de\": {\r\n    \"Hello\": \"Hallo\",\r\n    \"Goodbye\": \"Auf Wiedersehen\"\r\n  },\r\n  \"es\": {\r\n    \"Hello\": \"Hola\",\r\n    \"Goodbye\": \"Adi\u00f3s\"\r\n  }\r\n}\r\n```\r\n\r\n3. Import and use the create_files() function from the translation_file_generator module:\r\n```py\r\n    from mt_django_i18n.json_to_po import create_translator_files\r\n    from django.utils.translation import gettext as _\r\n\r\n    create_files(json_file_path,project_name,target_lang)\r\n\r\n```\r\n1. _**json_file_path** = This parameter represent file path for translation_\r\n2. _**project_name** = Name of project_ \r\n3. _**target_lang** = Lagunages which language file should be converted in binary format file i.e .mo files_\r\n\r\n## Example\r\n### views.py\r\n```py\r\nfrom mt_i18n.json_to_po import create_files\r\nimport os\r\nfrom django.utils.translation import gettext as _\r\n# Create your views here.\r\n\r\n\r\ndef blog(request):\r\n    'hello':_('Hello'), # this mark to value stored in _('') are to translated \r\n    if not (os.path.exists(\"locale\") and os.path.isdir(\"locale\")):\r\n        \r\n        os.mkdir('locale')\r\n        create_files(\"translator.json\",\"blog\",['fr', 'de', 'es'] )\r\n    \r\n```\r\n\r\n## Templates - HTML pages\r\n* The static text to be translated should be marked throught {% trans Hello world! %} or via variable format\r\nas mention below \r\n```html\r\n    <p>{{example}}</p>\r\n    <p>{% trans %}</p>\r\n```\r\n* For localization\r\n```html\r\n                    {% load l10n %}\r\n                    {% localize on %}\r\n                    <tr>\r\n                      <td><h6 class=\"text-muted\">Category</h6></td>\r\n                      <td> <h6 class=\"fw-bold\">\r\n                        {{my_currency|localize}}\r\n                      </h6></td>\r\n                    </tr>\r\n\r\n                    <tr>\r\n                      <td><h6 class=\"text-muted\">Number</h6></td>\r\n                      <td> <h6 class=\"fw-bold\">{{my_number|localize}}\r\n                      </h6></td>\r\n                    </tr>\r\n\r\n                    <tr>\r\n                      <td><h6 class=\"text-muted\">Date</h6></td>\r\n                      <td> <h6 class=\"fw-bold\">\r\n                        {{my_date}}\r\n                      </h6></td>\r\n                    </tr>\r\n                  </tbody>\r\n                </table>\r\n                {% endlocalize %}\r\n```\r\n## #currency_format\r\n* This function help to convert the number into currency format by passing number and language code as shown in below syntax\r\n\r\n```py\r\n from mt_django_i18n.json_to_po import currency_format\r\n my_currency= currency_format(currency,language='en-in') \r\n```\r\n* Where _currency_ parameter is for passing number/currency and _language_ parameter for language code.\r\n\r\n### Example\r\n```py\r\n from mt_django_i18n.json_to_po import currency_format\r\n my_currency = currency_format(currency=1234567.89,language=request.LANGUAGE_CODE) \r\n```\r\n**Note**\r\nUse _translation_ package to current activate language of project.\r\n\r\n```py\r\nfrom django.utils import translation\r\ntranslation.activate(language)\r\n```\r\n\r\n## #number_format\r\n* This function help to convert the number into number format by passing number and language code as shown in below syntax\r\n\r\n```py\r\n from mt_django_i18n.json_to_po import number_format\r\n my_number= number_format(number,lang='en-in')\r\n```\r\n* Where _number_ parameter is for pass number and _lang_ parameter is for language code.\r\n\r\n## Example\r\n```py\r\n from mt_django_i18n.json_to_po import number_format \r\n from decimal import *\r\n  my_number=number_format(Decimal(1000000.50) ,lang=request.LANGUAGE_CODE)\r\n```\r\n## urls.py\r\n```py\r\nfrom django.conf.urls.i18n import i18n_patterns\r\nurlpatterns+=i18n_patterns(\r\n  \r\n    path('',blog,name=\"blog\"),\r\n    # path('language_select/',language_select,name=\"language_select\")\r\n)\r\n```\r\n\r\n```note\r\n# Note: activate the language code of project every time there is\r\nchange in language so that translation perform better. \r\n\r\nfrom django.utils import translation.\r\ntranslation.activate(language)\r\n``` \r\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A package to implementing i18n in django",
    "version": "0.0.3",
    "project_urls": {
        "Bug Tracker": "https://github.com/pypa/sampleproject/issues",
        "Homepage": "https://github.com/pypa/sampleproject"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0be2374a827a2f74710c68d9e7a20fe9837d3128fb8c635241cc754212457505",
                "md5": "5b5da0534413de0fd23589cd18cce5b3",
                "sha256": "9b04d29e3d041c5f65b9526a456273c62acecba97d3610cdb237bafa2f2b012d"
            },
            "downloads": -1,
            "filename": "MT_PY_django_I18n-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5b5da0534413de0fd23589cd18cce5b3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 6843,
            "upload_time": "2023-09-25T11:39:34",
            "upload_time_iso_8601": "2023-09-25T11:39:34.817557Z",
            "url": "https://files.pythonhosted.org/packages/0b/e2/374a827a2f74710c68d9e7a20fe9837d3128fb8c635241cc754212457505/MT_PY_django_I18n-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a00d1517aa495ece075de15704f61b812eaf2ecfaba79a319b21a6c1b9989f1a",
                "md5": "70e0ed99fc70ac65670c61eab3366afe",
                "sha256": "19eb08e3d8986a2d813081e095ca35b33ef222100ba68a491aed3f11d52e70dc"
            },
            "downloads": -1,
            "filename": "MT_PY_django_I18n-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "70e0ed99fc70ac65670c61eab3366afe",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 6040,
            "upload_time": "2023-09-25T11:39:37",
            "upload_time_iso_8601": "2023-09-25T11:39:37.534599Z",
            "url": "https://files.pythonhosted.org/packages/a0/0d/1517aa495ece075de15704f61b812eaf2ecfaba79a319b21a6c1b9989f1a/MT_PY_django_I18n-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-25 11:39:37",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pypa",
    "github_project": "sampleproject",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "mt-py-django-i18n"
}
        
Elapsed time: 0.29380s