Skip to main content

Lightweight library for handling translations

Project description

Translations

Setup

pip install pytranslations

Usage

from pytranslations import Translations

translations = Translations()

First, you need to import the translations file

with open("translations.json", "r") as tr_file:
    translations.get_translations_from_json(tr_file.read())

Example translations file

translations.json

{
    "en": {
        "hello": {
            "phrase": "Hello {name}",
            "variables": ["name"]
        },

        "mynameis": {
            "phrase": "Hello {companion}! My name is {name}.",
            "variables": ["companion", "name"]
        }
    },

    "fr": {
        "hello": {
            "phrase": "Bonjour {name}",
            "variables": ["name"]
        }
    }
}

Then you can use t_lang() to translate phrase to the specified language. For example,

translations.t_lang("en", "mynameis", "John", "Bob")

will output Hello John! My name is Bob.

you can also set the set_default_language(). The default language is used when there is no translations for the specified one in t_lang(). Now, with the default language set to "en", let's try translating "mynameis" phrase to french.

translations.t_lang("fr", "mynameis", "John", "Bob")

This will output Hello John! My name is Bob., because there is no translation in the french dictionary for the phrase "mynameis".

You can also add users to track their specified language using add_user(user_id, language)

translations.add_user("user_1", "en")

Then you can call t_id() to translate the phrase for the specific user.

translations.t_id("user_1", "hello", "Bob")

will output Hello Bob.

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

pytranslations-0.0.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytranslations-0.0.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pytranslations-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f3b32c20a93282cd41a8923416820ad49ea0561deec45227093327d8e399eea1
MD5 98e85ca3c902a89cf86c7aba9c43f48f
BLAKE2b-256 259ca0aca4c02e276f17049755c18a379461dcc4f105695e116c64692ceaf633

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytranslations-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for pytranslations-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3689d902116abecd0d62e7b5c333283a9f4dc6a450713c663dfc04cd76f991a2
MD5 c79e09ce125d8e945b4affc0f223d66b
BLAKE2b-256 23e56a4818b61945e40f7d4f92af4d8aa358424cf88c3cdfa1551c3d7b816abf

See more details on using hashes here.

Supported by

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