Skip to main content

A simple, thread-safe Python module for easy localization (multilingual support)

Project description

Simple Localize

A simple and efficient Python module to easily add localization (multilingual support) to your projects.

Installation

pip install simple-localize

Quick Start

import simple_localize

simple_localize.init_localizer('your_translations.json')
print(simple_localize.get_text('your_text_key'))  # Auto-detects your language

Features

  • 🌍 Auto-detects your system language
  • 📁 JSON-based translations (easy to edit)
  • 🔄 Switch languages on the fly (per-thread safe)
  • 🎯 Text formatting with variables
  • ⚠️ Missing key warnings with helpful suggestions
  • Zero dependencies - uses only Python standard library
  • 🔒 Thread-safe - safe to use in multi-threaded apps (Flask, FastAPI…)

Basic Examples

# Get text in your system language
message = simple_localize.get_text('welcome_message')

# Get text in specific language
french_msg = simple_localize.get_text('welcome_message', 'FR')

# Change current language
simple_localize.set_language('ES')

# Use variables in text
greeting = simple_localize.get_text('greeting', name="Alice")

API Reference

Function Description Example
init_localizer(file, lang) Initialize with JSON file init_localizer('my_translations.json')
get_text(key, lang, **kwargs) Get translated text get_text('hello', name="John")
set_language(lang) Change language for the current thread set_language('FR')
get_current_language() Get current thread's language code Returns 'EN'
get_available_languages() List all available languages Returns ['EN', 'FR', 'ES']
add_translations(key, dict) Add/update a key at runtime (thread-safe) add_translations('hello', {'EN': 'Hi'})

Create Your Translation File

Create a JSON file with any name you like (my_texts.json, lang.json, app_translations.json, etc.):

{
  "app_title": {
    "EN": "My Application",
    "FR": "Mon Application",
    "ES": "Mi Aplicación"
  },
  "login_button": {
    "EN": "Login",
    "FR": "Connexion",
    "ES": "Iniciar sesión"
  },
  "welcome_user": {
    "EN": "Welcome {username}!",
    "FR": "Bienvenue {username} !",
    "ES": "¡Bienvenido {username}!"
  }
}

Then use it in your code:

import simple_localize

simple_localize.init_localizer('my_app_texts.json')
print(simple_localize.get_text('app_title'))
print(simple_localize.get_text('welcome_user', username="John"))

Thread Safety

set_language() applies per thread — each thread can have its own active language without affecting others. This makes it safe to use in web servers where each request runs in its own thread:

# Thread A (e.g. a French user's request)
simple_localize.set_language('FR')
simple_localize.get_text('welcome')  # → French
simple_localize.get_text('goodbye')  # → French too

# Thread B (e.g. a Spanish user's request), simultaneously
simple_localize.set_language('ES')
simple_localize.get_text('welcome')  # → Spanish, unaffected by Thread A

add_translations() is also thread-safe for concurrent writes.

Supported Languages

Add any languages you need! Just use standard language codes like: EN, FR, ES, DE, IT, etc.

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

simple_localize-1.0.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

simple_localize-1.0.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file simple_localize-1.0.0.tar.gz.

File metadata

  • Download URL: simple_localize-1.0.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for simple_localize-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d879eea27e7973347e8854b42f94444fb0fadb747088beedb7d2cba6706d7e60
MD5 b4ba49adbba51c6d28e22adddfb1f54d
BLAKE2b-256 dbce4370cfe8610d5cc211c118099d7e18602c7c7cc9a6ca837e500112dc2473

See more details on using hashes here.

File details

Details for the file simple_localize-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_localize-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a91c4b60d2a493d5b505f2aa73a983e3e49f89e34d1df2e2ccecb2c9ee0fa0ec
MD5 edfa8798446dc921c3e44c94ab538c86
BLAKE2b-256 e15d0e30845762cb7d05937d37dc727e7acd9a39e3c841ddf939f50a53516722

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