Package: chalice-babel
Chalice BabelChalice-Babel is a localization package inspired by Flask-Babel for AWS Chalice framework which is uses with help babel, pytz packages. Functionalty is the same like Flask-Babel but this package has a extra feature. You can add your translation strings without touching .po translation files help of export and import feature.Installationpipinstallchalice_babelConfigurationFor the default configuration values you need to addbabel_configjson intochalicelib/__init__.pyfile like below example. If not specify Chalice-Babel will use default values below table.# __init__.pybabel_config={"BABEL_DEFAUL_LOCALE":"en","BABEL_DEFAUL_TIMEZONE":"UTC","BABEL_TRANSLATION_DIRECTORIES":["translations","locale"],"BABEL_DOMAIN":"messages","LANGUAGES":["en","tr"],}Chalice-Babel has the following configuration values that can be used to change some internal defaults:KeyDescriptionBABEL_DEFAUL_LOCALEThe default locale language when local selector function not used. Default value is"en"BABEL_DEFAUL_TIMEZONEThe default timezone when local timezone selector functiofunction used. Default value is"UTC"BABEL_TRANSLATION_DIRECTORIESList of strings for translations folder names. Default value is"transtalions"BABEL_DOMAINThe message domain used by the application. Defaults to"messages".LANGUAGESList of language strings you want to supportUsageTo use Chalice-Babel you need to give app you created toBabelobject. Jinja support comes defaultTrueif you don't want to use jinja support you need to add configre_jinja = False toBabelobject.fromchaliceimportChalicefromchalice_babelimportBabelapp=Chalice(app_name="chalice_babel")babel=Babel(app)@babel.localeselectordefget_locale():langs=babel.parse_language_header()returnbabel.best_match(langs)Chalice-Babel uses local selector function returned language code to make translations possible so this decorator needs to be defined. If you need to localize something about time zones additionaly you need to define timezone selector decorator as well.Format NumbersTo format numbers you can use theese functions:format_number()format_decimal()format_currency()format_percent()format_scientific()>>>fromchalice_babelimportformat_number
>>>returnformat_number(2022)2,022>>>fromchalice_babelimportformat_decimal
>>>returnformat_decimal(2.022)2,022>>>fromchalice_babelimportformat_currency
>>>returnformat_currency(2.022,"USD")$2,022.00>>>fromchalice_babelimportformat_percent
>>>returnformat_percent(20.22)2,022%>>>fromchalice_babelimportformat_scientific
>>>returnformat_scientific(20220000)2.022E10Formatting DatesTo format dates you can use below functions. All of them uses datetime objects as first parameter and a format strings as second parameter.format_datetime()format_date()format_time()format_timedelta()>>>fromchalice_babelimportformat_datetime
>>>fromdatetimeimportdatetime

>>>format_datetime(datetime(1987,3,5,17,12))Mar5,19875:12:00PM

>>>format_datetime(datetime(1987,3,5,17,12),'full')Thursday,March5,19875:12:00PMWorld(GMT)Time

>>>format_datetime(datetime(1987,3,5,17,12),'short')3/5/875:12PM

>>>format_datetime(datetime(1987,3,5,17,12),'dd mm yyy')05121987Translating ApplicationWhen it comes to translations this is why Chalice-Babel built. It is usesgettexttogether withPython-Babel. All you need to do importgettext()orngettext()functions then mark strings or text using one of this functions you  want to be translated.
To translate singular strings you can usegettext()and if strings have plural form you can usengettext().fromchalice_babelimportgettext,ngettextgettext(u'A simple string')gettext(u'Value:%(value)s',value=42)ngettext(u'%(num)sApple',u'%(num)sApples',number_of_apples)If you want to evaluate strings translation you can uselazy_gettext()function.After mark all strings you want to translate it is time to create special template file called.potthat contains all the translated strings.
Before the creating pot file you need to definebabel.cfgconfiguration file next to yourapp.py, which is necessary to  usepybabelcommands.# babel.cfg[python:**.py][jinja2:**/templates/**.html]extensions=jinja2.ext.autoescape,jinja2.ext.with_When you are ready it is time to extract translated strings to.potfile. You all need to do run the followingpybabelcommand.pybabelextract-Fbabel.cfg-omessages.pot.If you usedlazy_gettext()function in your application you need to exract this translation strings using thispybabelcommand.pybabelextract-Fbabel.cfg-klazy_gettext-omessages.pot.Above commands will generatemessages.potfile frombabel.cfgconfiguration file. Now it is time to initialize first translation language.First of all you should create"en"folder with first command below after that you are free to create other languages you want. This is nessary becauseexportandimportcommand will use this"en"folder as a default. This commands explained in the next section.pybabelinit-imessages.pot-dchalicelib/translations-lenpybabelinit-imessages.pot-dchalicelib/translations-lde-dtranslation folder which contains language folders. This folder should be located inchalicelibfolder. The reason is Chalice packaging this folder when you deploy you application. It you create this folder outside ofchalicelibfolder translation will not work.
If you chooce another name then thetranslationsyou should add this name toBABEL_TRANSLATION_DIRECTORIESlist in the configuration file.After you generate your language folders you can edittranslations/{language_code}/LC_MESSAGES/messages.pofiles to add your translation strings.When you done with your translations only you need to compile translations withpybabel compilecommand.pybabelcompile-dchalicelib/translationsWhen you changed marked strings or add new translation strings you need to updatemessages.potfile.pybabelupdate-imessages.pot-dchalicelib/translationsEnd of this operations some strings can markedfuzzyin.pofiles. When this happen you need to check and fix manually this strings.If you need more information for above commands and howBabelworks you can checkoutbabeldocumentationExport & ImportWhen you have a large application with support for many languages, it means that your application contains a lot of strings and text that needs to be translated, and at some point it becomes a pain to manage and replace all those translation files."export_strings"and"import_strings"commands makes this process easy to manage.ExportWith help ofchalice_babel export_stringscommand you can export all marked translation strings from.pofiles as a json file and you can easyly check which strings or texts translated correctly or not needs to be translated.Usage:chalice_babelexport_strings[options][args]Options:-l,--langsourcelanguageforexporttranslationstrings,default"en"-D,--domaindomainsofPOfiles,default"messages"-t,--translation_folderfoldernametobasedirectorycontainingthecatalogsunderchalicelibfolder.Default"translations"-o,--output_diroutputpathfortranslationstringsjsonfile.Defaultlocationiswhereapp.pyislocated-f,--filenametranslationstringsjsonfilename.Default"translation_strings"For example if you have"ja"and"de"languages you wanted to support. Export command will generate json file like below.{"currency":{"en":"","ja":"","de":""},"message":{"en":"","ja":"メッセージ","de":"Botschaft"},"Hello":{"en":"","ja":"こんにちわ","de":"Halo"}}ImportIf you generated your translation strings json file now you can edit this file to change or add translations. For example"currency"string didn't translated on above example. You can add translation strings for this key. After that when you runchalice_babel import_stringscommand this changes will implemented to corresponding.pofiles. End of this operation you should runpybabel compile.Usage:chalice_babelimport_strings[options][args]Options:-D,--domaindomainsofPOfiles,default"messages"-t,--translation_folderfoldernametobasedirectorycontainingthecatalogsunderchalicelibfolder.Default"translations"-o,--input_dirinputpathfortranslationstringsjsonfile.Defaultlocationiswhereapp.pyislocated-f,--filenametranslationstringsjsonfilename.Default"translation_strings"ContributingContributions are always welcome!If you one to help us you can open your pr and we can discuss your new feature.