From beabd5d65dfdf5a04b1a9843ff0cfb8e0d4b7c73 Mon Sep 17 00:00:00 2001 From: Sachin Mahor Date: Fri, 31 Oct 2025 11:55:14 +0530 Subject: [PATCH 1/3] docs(examples): add python integration example for Lingo.dev (#1453) --- examples/python-basic/README.md | 57 ++++++++++++++++++ .../__pycache__/translator.cpython-312.pyc | Bin 0 -> 1397 bytes examples/python-basic/app.py | 9 +++ examples/python-basic/translations/en.json | 4 ++ examples/python-basic/translations/hi.json | 4 ++ examples/python-basic/translator.py | 18 ++++++ 6 files changed, 92 insertions(+) create mode 100644 examples/python-basic/README.md create mode 100644 examples/python-basic/__pycache__/translator.cpython-312.pyc create mode 100644 examples/python-basic/app.py create mode 100644 examples/python-basic/translations/en.json create mode 100644 examples/python-basic/translations/hi.json create mode 100644 examples/python-basic/translator.py diff --git a/examples/python-basic/README.md b/examples/python-basic/README.md new file mode 100644 index 000000000..9f250be02 --- /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 + नमस्ते + अलविदाrom translator import Translator + + # 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 0000000000000000000000000000000000000000..1024e57c711b86808c129394981670cf03d15e48 GIT binary patch literal 1397 zcmZ8hL5S2?5Urp5XnO3PFsq`YyAqvdFoG?Dis&K;innGL^#%WXfI-?HP376{}iY7F=I=4Gi3oJaEFMEGEOV_)!?kQV=<= zO^P|E=~=4eu@}q;9i0k_5YzEY)BJk+^0|65jvDpA51UcN^={X_JI-u7@Zx&AliZBL zB#ABT;F0q;A`O=qI+_-My8AO1`L3glS~=V0H4yB7ZQxx@SL%!s?HQ7WsR9 za#b&`=m(bd1CLLA(9J$tLSr)tIN5 zkJ?^{3%Cng0?RMxZdu7KF6b_U^8bKH@);j1OfLtUz!Y>pK`*Ske_J`f>mhOwUq^Ni z){Kcq^Y`Z;rT5dvm!4OjRC`Hp=I!NP^__8kk*}2|{wTlL_n~ChAYLUeV zVRl(wi>n~0BP=CLi&UFlf|$Ze^>?omQ)3M?Az9KbZp%Y5fx7r0GVEt#_ljXI8|I2p zUN*`vuJ)$h8I}L^iWo Date: Tue, 18 Nov 2025 10:47:26 +0530 Subject: [PATCH 2/3] Update examples/python-basic/translator.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- examples/python-basic/translator.py | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/examples/python-basic/translator.py b/examples/python-basic/translator.py index 636a6e616..3d332a24f 100644 --- a/examples/python-basic/translator.py +++ b/examples/python-basic/translator.py @@ -2,17 +2,47 @@ 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 From eafb29509171ac3d451df40259e5b1074f0ddff4 Mon Sep 17 00:00:00 2001 From: sachinm121 <112053922+sachinm121@users.noreply.github.com> Date: Tue, 18 Nov 2025 10:47:50 +0530 Subject: [PATCH 3/3] Update examples/python-basic/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- examples/python-basic/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/python-basic/README.md b/examples/python-basic/README.md index 9f250be02..3e4e80c96 100644 --- a/examples/python-basic/README.md +++ b/examples/python-basic/README.md @@ -34,7 +34,7 @@ python-basic/ Hello Goodbye नमस्ते - अलविदाrom translator import Translator + अलविदा # Initialize with English tr = Translator(lang='en')