diff --git a/docs/python/faq.md b/docs/python/faq.md index f0f1f85..8ecd6a5 100644 --- a/docs/python/faq.md +++ b/docs/python/faq.md @@ -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 diff --git a/python-bible/Readme.md b/python-bible/Readme.md new file mode 100644 index 0000000..221fcfc --- /dev/null +++ b/python-bible/Readme.md @@ -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 + + + diff --git a/python-bible/localize/app.po b/python-bible/localize/app.po new file mode 100644 index 0000000..39b89a9 --- /dev/null +++ b/python-bible/localize/app.po @@ -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 , 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 \n" +"Language-Team: LANGUAGE \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 " \ No newline at end of file diff --git a/python-bible/localize/fr/LC_MESSAGES/app.mo b/python-bible/localize/fr/LC_MESSAGES/app.mo new file mode 100644 index 0000000..1e0a159 Binary files /dev/null and b/python-bible/localize/fr/LC_MESSAGES/app.mo differ diff --git a/python-bible/localize/fr/LC_MESSAGES/app.po b/python-bible/localize/fr/LC_MESSAGES/app.po new file mode 100644 index 0000000..6dd38b0 --- /dev/null +++ b/python-bible/localize/fr/LC_MESSAGES/app.po @@ -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 \n" +"Language-Team: LANGUAGE \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" \ No newline at end of file diff --git a/python-bible/recursion.py b/python-bible/recursion.py new file mode 100644 index 0000000..e8fddae --- /dev/null +++ b/python-bible/recursion.py @@ -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)) \ No newline at end of file diff --git a/python-bible/test_localize.py b/python-bible/test_localize.py new file mode 100644 index 0000000..6ca9e41 --- /dev/null +++ b/python-bible/test_localize.py @@ -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.')) \ No newline at end of file