-
Notifications
You must be signed in to change notification settings - Fork 663
docs(examples): add python integration example for Lingo.dev (#1453) #1462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
||
sachinm121 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # 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.** | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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: अलविदा |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "greeting": "Hello", | ||
| "farewell": "Goodbye" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "greeting": "नमस्ते", | ||
| "farewell": "अलविदा" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import json | ||
| import os | ||
|
|
||
| class Translator: | ||
| def __init__(self, lang='en', directory='translations'): | ||
| self.directory = directory | ||
| self.lang = lang | ||
| self.translations = {} | ||
| self.load_language(lang) | ||
|
|
||
| def load_language(self, lang): | ||
| 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 | ||
|
Comment on lines
26
to
36
|
||
|
|
||
| def t(self, key): | ||
sachinm121 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return self.translations.get(key, key) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The folder structure should be formatted as a proper markdown code block for better readability. Wrap lines 6-12 with triple backticks to ensure proper rendering and preserve the tree structure formatting.