• Stars
    star
    201
  • Rank 193,480 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy to use i18n library for Python

python-i18n Build Status Coverage Status Code Climate

This library provides i18n functionality for Python 3 out of the box. The usage is mostly based on Rails i18n library.

Installation

Just run

pip install python-i18n

If you want to use YAML to store your translations, use

pip install python-i18n[YAML]

Usage

Basic usage

The simplest, though not very useful usage would be

import i18n
i18n.add_translation('foo', 'bar')
i18n.t('foo') # bar

Using translation files

YAML and JSON formats are supported to store translations. With the default configuration, if you have the following foo.en.yml file

en:
  hi: Hello world !

in /path/to/translations folder, you simply need to add the folder to the translations path.

import i18n
i18n.load_path.append('/path/to/translations')
i18n.t('foo.hi') # Hello world !

Please note that YAML format is used as default file format if you have yaml module installed. If both yaml and json modules available and you want to use JSON to store translations, explicitly specify that: i18n.set('file_format', 'json')

Memoization

Setting the configuration value enable_memoization in the settings dir will load the files from disk the first time they are loaded and then store their content in memory. On the next use the file content will be provided from memory and not loaded from disk, preventing disk access. While this can be useful in some contexts, keep in mind there is no current way of issuing a command to the reloader to re-read the files from disk, so if you are updating your translation file without restarting the interpreter do not use this option.

Namespaces

File namespaces

In the above example, the translation key is foo.hi and not just hi. This is because the translation filename format is by default {namespace}.{locale}.{format}, so the {namespace} part of the file is used as translation.

To remove {namespace} from filename format please change the filename_format configuration.

i18n.set('filename_format', '{locale}.{format}')

Directory namespaces

If your files are in subfolders, the foldernames are also used as namespaces, so for example if your translation root path is /path/to/translations and you have the file /path/to/translations/my/app/name/foo.en.yml, the translation namespace for the file will be my.app.name and the file keys will therefore be accessible from my.app.name.foo.my_key.

Functionalities

Placeholder

You can of course use placeholders in your translations. With the default configuration, the placeholders are used by inserting %{placeholder_name} in the ntranslation string. Here is a sample usage.

i18n.add_translation('hi', 'Hello %{name} !')
i18n.t('hi', name='Bob') # Hello Bob !

Pluralization

Pluralization is based on Rail i18n module. By passing a count variable to your translation, it will be pluralized. The translation value should be a dictionnary with at least the keys one and many. You can add a zero or few key when needed, if it is not present many will be used instead. Here is a sample usage.

i18n.add_translation('mail_number', {
    'zero': 'You do not have any mail.',
    'one': 'You have a new mail.',
    'few': 'You only have %{count} mails.',
    'many': 'You have %{count} new mails.'
})
i18n.t('mail_number', count=0) # You do not have any mail.
i18n.t('mail_number', count=1) # You have a new mail.
i18n.t('mail_number', count=3) # You only have 3 new mails.
i18n.t('mail_number', count=12) # You have 12 new mails.

Fallback

You can set a fallback which will be used when the key is not found in the default locale.

i18n.set('locale', 'jp')
i18n.set('fallback', 'en')
i18n.add_translation('foo', 'bar', locale='en')
i18n.t('foo') # bar

Skip locale from root

Sometimes i18n structure file came from another project or not contains root element with locale eg. en name.

{
    "foo": "FooBar"
}

However we would like to use this i18n .json file in our Python sub-project or micro service as base file for translations. python-i18n has special configuration tha is skipping locale eg. en root data element from the file.

i18n.set('skip_locale_root_data', True)

More Repositories

1

atomic-chrome

Edit Chrome textareas in Atom
JavaScript
916
star
2

fundle

A minimalist package manager for fish shell
Shell
363
star
3

opencov

Open source code coverage history webapp
Elixir
312
star
4

fish-ssh-agent

Shell
288
star
5

ex_cli

User friendly CLI apps for Elixir
Elixir
215
star
6

structomap

Easily and dynamically generate maps from Go static structures
Go
141
star
7

pushex

Push notifications for Elixir
Elixir
104
star
8

elixir-browser

Browser detection for Elixir
Elixir
95
star
9

atomic-chrome-atom

Edit Chrome textareas in Atom
CoffeeScript
93
star
10

elixir-temp

Temporary files and directories for Elixir
Elixir
74
star
11

plug-navigation-history

Elixir plug to keep navigation history
Elixir
69
star
12

elixir-git-cli

A simple interface to Git CLI for Elixir
Elixir
68
star
13

phoenix-active-link

Elixir/Phoenix view helper to manage "active" state of a link
Elixir
63
star
14

elixir-web-push-encryption

Elixir implementation of Web Push Payload encryption.
Elixir
54
star
15

eclair

A Solidity interpreter
Rust
47
star
16

bigcode-tools

Set of tools to help working with "Big Code"
Python
43
star
17

rust-simple-nn

Simple neural network implementation in Rust
Rust
33
star
18

seedex

Seed data generation for Ecto
Elixir
29
star
19

securerandom

Go
25
star
20

evm-analyzer

Code for Smart Contract Vulnerabilities: Vulnerable Does Not Imply Exploited
OCaml
22
star
21

fish-fastdir

Fast directory navigation for fish
Shell
19
star
22

asdf-exec

Native command to run asdf shims
Go
16
star
23

suplearn-clone-detection

Cross language clone detection using supervised learning
Python
16
star
24

node-git-cli

Simple CLI like git interface for Node.
CoffeeScript
16
star
25

defi-plf-analysis

Code and data for the paper: DeFi Protocols for Loanable Funds: Interest Rates, Liquidity and Market Efficiency
Jupyter Notebook
14
star
26

scalog

Datalog implementation in Scala.
Scala
12
star
27

atom-align-regexp

Regexp based alignment for Atom
JavaScript
11
star
28

koa-police

Policy based authentication library for Koa
JavaScript
9
star
29

fontawesome-icons-list

Fontawesome icons list
JavaScript
9
star
30

blockchain-analyzer

Tool to fetch and analyze blockchain transactions
Go
9
star
31

fish-kubectl

Shell
8
star
32

oh-my-fish-core

oh-my-fish core library extracted
Shell
7
star
33

sigma-bower

Bower repository for SigmaJS
JavaScript
6
star
34

dotfiles

TeX
5
star
35

ecto-secure-password

has_secure_password for Ecto models
Elixir
5
star
36

ocaml-monads

OCaml
5
star
37

fish-completion-helpers

Fish helper functions to simplify completions
Shell
5
star
38

go-sqs-client

Go
4
star
39

wedding

Simple webapp to manage guests and invitations for my wedding with Ai.
Ruby
4
star
40

ethereum-tools

Python
4
star
41

simple-bitcoin-parser

Simple Python Bitcoin parser/formatter implementation
Python
4
star
42

flashcards-cli

Text-based flashcards
OCaml
3
star
43

phd-thesis

TeX
3
star
44

deepsentence

和文文書からの要点抽出
Python
3
star
45

scala-simple-compiler

Scala
3
star
46

code-battle

HTML
3
star
47

fish-nvm

nvm wrapper for fish
Shell
3
star
48

fish-asdf

fish plugin for asdf vm
Shell
2
star
49

projare

Sample app using Elixir, Phoenix and RiotJS
JavaScript
2
star
50

sublime-file-commands

Commands to work with files in Sublime Text
Python
2
star
51

advent-2022

Rust
2
star
52

elixir-logger-sample

Elixir
2
star
53

meditable

WIP: markdown <-> html
JavaScript
2
star
54

easter-egg-hunt

Ruby
1
star
55

websocket-rails-bower

1
star
56

master-thesis

A study of machine learning approaches to cross-language code clone detection
TeX
1
star
57

advent-2020

Haskell
1
star
58

advent-2023

F#
1
star
59

vim-config

Vim Script
1
star
60

go-ethereum-customized

Fork of the official geth client
Go
1
star
61

word-frequency-analyzer

Simple word frequency analyzer with JavaFX rendering.
Scala
1
star
62

bachelor-thesis

Repository for my bachelor thesis
TeX
1
star
63

js-easy-params

Decorator for JS functions with optional arguments
JavaScript
1
star
64

scala-x-server

System to run an X server in a web browser
Scala
1
star
65

jsurl_ex

Elixir
1
star
66

tokyo-ex-1-slides

HTML
1
star
67

erlang-sqs-client

Erlang
1
star
68

revealjs-jade-starter

HTML
1
star
69

sublime-rails-status-snippets

ST2/3 snippets for HTTP codes symbols in RoR
Python
1
star
70

ansible-exrm

1
star
71

koa-police-jwt

JWT strategy for koa-police
JavaScript
1
star
72

django-twitter-auth-sample

Python
1
star
73

aleth

C++
1
star