Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions examples/python-basic/README.md
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/
Copy link

Copilot AI Nov 12, 2025

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.

Copilot uses AI. Check for mistakes.
├── 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.**
```
Binary file not shown.
9 changes: 9 additions & 0 deletions examples/python-basic/app.py
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: अलविदा
4 changes: 4 additions & 0 deletions examples/python-basic/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"greeting": "Hello",
"farewell": "Goodbye"
}
4 changes: 4 additions & 0 deletions examples/python-basic/translations/hi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"greeting": "नमस्ते",
"farewell": "अलविदा"
}
18 changes: 18 additions & 0 deletions examples/python-basic/translator.py
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
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The load_language method lacks error handling for missing translation files or invalid JSON. If a translation file doesn't exist or contains invalid JSON, the application will crash with an unhandled exception. Consider adding try-except blocks to handle FileNotFoundError and json.JSONDecodeError, providing a helpful error message or fallback behavior.

Copilot uses AI. Check for mistakes.

def t(self, key):
return self.translations.get(key, key)
Loading