Skip to main content

A basic dynamic class to support I18N translation from JSON

Project description

dynamic-i18n

PyPI version

Introduction

dynamic-i18n is a python package that helps to flatten complex JSON files into a simple and accessible format. It also provides functionality to modify the text of specific properties and get an object that only contains properties of a specified language.

Installation

To install dynamic-i18n, run the following command:

pip install dynamic-i18n

Usage

To use dynamic-i18n, you need to pass either a file name or a dictionary to the I18NFlatten class. You can also specify a language prefix to limit the properties to a specific language.

import json

from dynamic_i18n.flatten import I18NFlatten



# Using a file

json_file = 'file.json'

i18n = I18NFlatten(json_file=json_file)



# Using a dictionary

json_dict = {...}

i18n = I18NFlatten(json_dict=json_dict)

The flattened properties can be accessed as attributes of the I18NFlatten object.

property_value = i18n.property_name

The modify_text method can be used to modify the text of a specified property by replacing placeholders with the provided parameters.

modified_text = i18n.modify_text('property_name', param1='value1', param2='value2')

The get_language_object method can be used to get a new I18NFlatten object that only contains properties of a specified language.

language_object = i18n.get_language_object('en')

Practical Example

from dynamic_i18n.flatten import I18NFlatten



data_json = {

    'i18n': {

        'welcome_message': {

            'en_us': "Hello World {name}",

            'pt_br': "Olá Mundo",

            'es': "Hola mundo"

        },

        'other_message': {

            'en_us': "{programming_language} programming language",

            'pt_br': "{programming_language} linguagem de programação",

            'es': "{programming_language} lenguaje de programación"

        }

    }

}



data = I18NFlatten(json_dict=data_json)

print(data._fields)

# {'i18n_welcome_message_en_us': 'Hello World {name}', 'i18n_welcome_message_pt_br': 'Olá Mundo', 'i18n_welcome_message_es': 'Hola mundo', 'i18n_other_message_en_us': '{programming_language} programming language', 'i18n_other_message_pt_br': '{programming_language} linguagem de programação', 'i18n_other_message_es': '{programming_language} lenguaje de programación'}



print(data.modify_text('i18n_welcome_message_en_us', name='Jeferson Peter'))

# IN: 'Hello World {name}'

# OUT: 'Hello World Jeferson Peter'



print(data.modify_text('i18n_other_message_pt_br', programming_language='Python'))

# IN: '{programming_language} linguagem de programação'

# OUT: 'Python linguagem de programação'



data_en = data.get_language_object(language_prefix='en_us')

print(data_en._fields)

# {'i18n_welcome_message_en_us': 'Hello World {name}', 'i18n_other_message_en_us': '{programming_language} programming language'}

Conclusion

dynamic-i18n provides a simple and convenient way to access and manipulate complex JSON files for internationalization purposes. Give it a try and see how it can help you in your next project.

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

dynamic_i18n-0.0.7.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

dynamic_i18n-0.0.7-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file dynamic_i18n-0.0.7.tar.gz.

File metadata

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

File hashes

Hashes for dynamic_i18n-0.0.7.tar.gz
Algorithm Hash digest
SHA256 48d0bdd5c3235fccaca7a0a9d4b07d319df5b32f45cd58229d46564ee3ba111e
MD5 d8303c66d8b887b7fb360fe192835740
BLAKE2b-256 d5303d7ec2907093a2b352813cca5d3abb23b342704718c31098fd9630706d2b

See more details on using hashes here.

File details

Details for the file dynamic_i18n-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for dynamic_i18n-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a693237fdde6072e57cfc24f4f23c34e883577bec8155c8cfd3ff7df287af328
MD5 1b9101d6d5552ec86cb2e71b3913a8c8
BLAKE2b-256 1cf4239ed13241156d2c2fa786654d4c945a545ef30ce81543c8a8daa12e512d

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