From 610039298b5381dd230850fa972edd74a613a52f Mon Sep 17 00:00:00 2001 From: jbcodeforce Date: Mon, 25 Mar 2024 21:38:12 -0700 Subject: [PATCH] add localization example --- docs/python/faq.md | 16 ++++++++++++ python-bible/Readme.md | 18 ++++++++++++++ python-bible/localize/app.po | 26 ++++++++++++++++++++ python-bible/localize/fr/LC_MESSAGES/app.mo | Bin 0 -> 475 bytes python-bible/localize/fr/LC_MESSAGES/app.po | 19 ++++++++++++++ python-bible/recursion.py | 19 ++++++++++++++ python-bible/test_localize.py | 11 +++++++++ 7 files changed, 109 insertions(+) create mode 100644 python-bible/Readme.md create mode 100644 python-bible/localize/app.po create mode 100644 python-bible/localize/fr/LC_MESSAGES/app.mo create mode 100644 python-bible/localize/fr/LC_MESSAGES/app.po create mode 100644 python-bible/recursion.py create mode 100644 python-bible/test_localize.py 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 0000000000000000000000000000000000000000..1e0a1590e156d82eaaf8a700add318363a9e98f5 GIT binary patch literal 475 zcmYL_!A=`75Qg2B9=b;^Tu?Rl$_q#w(2`bJ5(6uCv(aV~DV%V2hs9N5N4B?u8+Xn; z0Z-Hy=(8|Jl`@*o|HzuLf6c%By;nx@g*YTmiLXRXgt#NV6OxFCXX4-!V=ttfc%%9o z>0eUHk9SDe=Wv9mACv=13EoQW*3!$>8o_z1bQiOw=`Cov!I(|0`HhX|S<4q_oiaLs zX*#+|uLLYaHOtCLRN>ZG&kNV7mS6OpRcKByhG;K4K$m6rl6aYYelBr2VL@Y&=>Fn{9jL!eZINc)HPM!`=b\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