Translation assistant for Python
pip install --user pylanguage
from pylanguage.translator import Translator as T
Suppose your project directory is of following structure:
myproject
|
---- src/
| |
| --- __init__.py
| --- myfile.py # Uses pylanguage
|
---- translations
|
---- base.json # base translations
|
---- en_US.json # English US translations
from pylanguage.translator import Translator as T
Here, myfile.py
uses the translations. So, under the import statement and before any strings, create the translations object.
Here using en_US
as an example,
import pathlib
from pylanguage.translator import Translator as T
proj_dir = str(pathlib.Path(__file__).parent.resolve())
translation_dir = str(pathlib.Path().joinpath(proj_dir, 'translations'))
p = T(base_file = 'base.json', lang_code = 'en_US', lang_dir = translation_dir)
Now, use the get()
method of the Translator
object.
Complete code of the previous example:
import pathlib
from pylanguage.translator import Translator as T
proj_dir = str(pathlib.Path(__file__).parent.resolve())
translation_dir = str(pathlib.Path().joinpath(proj_dir, 'translations'))
p = T(base_file = 'base.json', lang_code = 'en_US', lang_dir = translation_dir)
print(p.get("Hello World"))
Both base file and language specific files use the following spec. Do note however that the base file will have same keys and values.
For the example above, both base.json
and en_US.json
will have the following content:
{
"Hello World": "Hello World"
}
See LICENSE file.
Tl;dr: This project uses MIT License.
See Documentation