Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcodeforce committed Mar 28, 2024
2 parents a0ba8aa + 6100392 commit 7a12aa8
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/python/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ import logging

Start python with the `--log=INFO` to set the logging level.

## How to get localization in python message

How to make a string translated in another language using GNU gettext.

See the code test_localize.py

* install poedit
* upadte PATH to point to `C:\Program Files (x86)\Poedit\GettextTools\bin`
* get the localizable string from the python program
* xgettext -d app -o localize/app.pot test_localize.py
* then create localize/fr/LC_MESSAGES/app.po from the created app.po under localize
* Compile the po to mo: msgfmt -o localize/fr/LC_MESSAGES/app.mo localize/fr/LC_MESSAGES/app
* export LANG=fr
* python test_localize.py


## Reading Files

### Read json file
Expand Down
18 changes: 18 additions & 0 deletions python-bible/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

## The localization examples

How to make a string translated in another language using GNU gettext.

See the code test_localize.py

* install poedit
* upadte PATH to point to `C:\Program Files (x86)\Poedit\GettextTools\bin`
* get the localizable string from the python program
* xgettext -d app -o localize/app.pot test_localize.py
* then create localize/fr/LC_MESSAGES/app.po from the created app.po under localize
* Compile the po to mo: msgfmt -o localize/fr/LC_MESSAGES/app.mo localize/fr/LC_MESSAGES/app
* export LANG=fr
* python test_localize.py



26 changes: 26 additions & 0 deletions python-bible/localize/app.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-25 21:09-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: test_localize.py:6
msgid "This is a translatable string."
msgstr ""

#: test_localize.py:8
msgid "Your name."
msgstr "
Binary file added python-bible/localize/fr/LC_MESSAGES/app.mo
Binary file not shown.
19 changes: 19 additions & 0 deletions python-bible/localize/fr/LC_MESSAGES/app.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-25 21:09-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"


msgid "This is a translatable string."
msgstr "Ceci est une chaine de charactere traduisable"

msgid "Your name."
msgstr "Votre nom"
19 changes: 19 additions & 0 deletions python-bible/recursion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import math

def exponential(base,exp):
if base == 0:
return 0
if exp == 0:
return 1
else:
if exp > 0:
return base * exponential(base,exp-1)
else:
return 1/base * exponential(base,exp+1)


if __name__ == "__main__":
print(exponential(0,2))
print(exponential(2,3))
assert exponential(2,-3) == math.pow(2,-3)
print(exponential(5,-5))
11 changes: 11 additions & 0 deletions python-bible/test_localize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import gettext


localizator = gettext.translation('app', localedir='localize', languages=['fr'])
localizator.install()

_ = localizator.gettext
# ...
print(_('This is a translatable string.'))

print(_('Your name.'))

0 comments on commit 7a12aa8

Please sign in to comment.