diff --git a/examples/python-basic/README.md b/examples/python-basic/README.md new file mode 100644 index 000000000..3e4e80c96 --- /dev/null +++ b/examples/python-basic/README.md @@ -0,0 +1,57 @@ +# Python Basic Integration Example for Lingo.dev + +This example demonstrates how to use translation JSON files exported from Lingo.dev in a simple Python backend or CLI application. + +## Folder Structure +python-basic/ +├── translations/ +│ ├── en.json +│ └── hi.json +├── translator.py +├── app.py +└── README.md + + +## Features + +- Loads translations from JSON files +- Accesses translations for a given key +- Easily switches languages at runtime + +## How to Use + +1. **Translation Files:** + Put your exported translation files (e.g., `en.json`, `hi.json`) inside the `translations/` directory. + +2. **Run the Demo:** + In your terminal, navigate to this folder and run: + ```bash + python app.py + ``` + + You should see the following output: + + Hello + Goodbye + नमस्ते + अलविदा + + # Initialize with English + tr = Translator(lang='en') + print(tr.t('greeting')) # Output: Hello + + # Switch to Hindi + tr.load_language('hi') + print(tr.t('greeting')) # Output: नमस्ते + ``` + +## Files + +- `translator.py`: Contains a simple `Translator` class for loading and accessing translations. +- `app.py`: Demo script showing how to use the `Translator` class. +- `translations/`: Folder for your translation JSON files (`en.json`, `hi.json`, etc.). + +--- + +**You can now use these files as a starting point for integrating Lingo.dev translations in any Python backend or CLI application.** +``` diff --git a/examples/python-basic/__pycache__/translator.cpython-312.pyc b/examples/python-basic/__pycache__/translator.cpython-312.pyc new file mode 100644 index 000000000..1024e57c7 Binary files /dev/null and b/examples/python-basic/__pycache__/translator.cpython-312.pyc differ diff --git a/examples/python-basic/app.py b/examples/python-basic/app.py new file mode 100644 index 000000000..304af9736 --- /dev/null +++ b/examples/python-basic/app.py @@ -0,0 +1,9 @@ +from translator import Translator + +tr = Translator(lang='en') +print(tr.t('greeting')) # Output: Hello +print(tr.t('farewell')) # Output: Goodbye + +tr.load_language('hi') +print(tr.t('greeting')) # Output: नमस्ते +print(tr.t('farewell')) # Output: अलविदा \ No newline at end of file diff --git a/examples/python-basic/translations/en.json b/examples/python-basic/translations/en.json new file mode 100644 index 000000000..d7f4572b4 --- /dev/null +++ b/examples/python-basic/translations/en.json @@ -0,0 +1,4 @@ +{ + "greeting": "Hello", + "farewell": "Goodbye" + } \ No newline at end of file diff --git a/examples/python-basic/translations/hi.json b/examples/python-basic/translations/hi.json new file mode 100644 index 000000000..8250d0448 --- /dev/null +++ b/examples/python-basic/translations/hi.json @@ -0,0 +1,4 @@ +{ + "greeting": "नमस्ते", + "farewell": "अलविदा" + } \ No newline at end of file diff --git a/examples/python-basic/translator.py b/examples/python-basic/translator.py new file mode 100644 index 000000000..3d332a24f --- /dev/null +++ b/examples/python-basic/translator.py @@ -0,0 +1,48 @@ +import json +import os + +class Translator: + """ + A simple translation class that loads language files and provides translation for keys. + + Attributes: + directory (str): The directory where translation files are stored. + lang (str): The current language code. + translations (dict): The loaded translations for the current language. + """ + def __init__(self, lang='en', directory='translations'): + """ + Initialize the Translator. + + Args: + lang (str): The language code to use (default is 'en'). + directory (str): The directory containing translation files (default is 'translations'). + """ + self.directory = directory + self.lang = lang + self.translations = {} + self.load_language(lang) + + def load_language(self, lang): + """ + Load translations for the specified language from a JSON file. + + Args: + lang (str): The language code to load. + """ + path = os.path.join(self.directory, f"{lang}.json") + with open(path, encoding='utf-8') as file: + self.translations = json.load(file) + self.lang = lang + + def t(self, key): + """ + Translate a key using the loaded translations. + + Args: + key (str): The key to translate. + + Returns: + str: The translated string if found, otherwise the key itself. + """ + return self.translations.get(key, key) \ No newline at end of file