Package: django-boot-siba
SibaSiba (coming fromSibawayh) is a Django translation tool that usesjsonfiles instead ofgettext (po, mo).Features:Support for nested keysReads current locale from Django translation moduleSimple parameter injection to valuesSupport for caching locales (loading into memory) to improve performance if neededPluralization: none, one, some, manyMultiple prefixes (you can have separate translation files for different contexts)Lazy loading translationsWhat it doesn't do:Right now there is no support to use this library in Django templates. Rest framework should be supported well.RequirementsTested with Python 3.9, but 3.6+ should be supported as well. Django >= 4.2.UsageYou can create files with format{prefix}.{locale}.jsonunder a directory of your choice so Siba scans them.
For exampleapplication.en.jsonshould containenlocale data for prefixapplication(default). You can change default prefix and directory of locales in settings (read below).Example json content:{"phrase":{"welcomeMessage":"Welcome to application, {username}"}}You can now read this translation using:fromsiba.translationimporttranslate,translate_lazytranslate("phrase.welcomeMessage")translate_lazy("phrase.welcomeMessage")You can directly pass a locale as well:fromsiba.translationimporttranslate,translate_lazytranslate("phrase.welcomeMessage",locale="de")translate_lazy("phrase.welcomeMessage",locale="de")Or change the prefix (reads from a different file):fromsiba.translationimporttranslate# looks in messages.de.json filetranslate("phrase.welcomeMessage",prefix="messages",locale="de")If you don't pass a locale it goes for active translation in Django.
So you don't have to worry if you already change active translation in your middlewares.fromsiba.translationimporttranslatefromdjango.utilsimporttranslationtranslation.activate("de")translate("phrase.welcomeMessage")# looks for de localeAnd you can inject parameter valuesfromsiba.translationimporttranslatetranslate("phrase.welcomeMessage",parameters={"username":"siba"})PluralizationSiba supports pluralization. To use it you need at least these two keys defined in your json object for a parent key you want to pluralize:one,many.Through the settings you can enable support forsome, and it supportsnoneby default too.Example:{"catCounter":{"none":"You have no cats! Start to pet one.","one":"You only have a single cat! Get more pets.","some":"Glad to see you have some cats.","many":"I've never seen so many cats bein pets of a single person!"}}Using key argumentp_countyou can determine which translation should be used.fromsiba.translationimporttranslatetranslate("catCounter")# You only have a single cat! Get more pets.translate("catCounter",p_count=0)# You have no cats! Start to pet one.translate("catCounter",p_count=1)# You only have a single cat! Get more pets.translate("catCounter",p_count=3)# Glad to see you have some cats.# Dynamical example:cats=[]translate("catCounter",p_count=len(cats))SettingsYou can change the settings by using code below in yoursettings.py.fromsibaimportset_settingset_setting(key,value)Setting keys are:error_on_unknown_key: expects(bool), determines if error should rise when a translation key is missing.error_on_missing_locale_file: expects(bool), determines if error should rise when a locale file is missing.key_split_delimiter: expects(str), delimiter for keys.locales_path: expects(str), directory to find locale files in .cache_locales: expects(bool), determines if locale data should be cached (loaded into memory).prefixes: expects(list[str]), list of supported locale file prefixes.default_prefix: expects(str), default prefix (currently set toapplication).locales: expects(list[str]), list of supported locales in the application.default_locale: expects(str), the default locale of the application (currently set toen).missing_parameter_handler: expects (function(key:str)), callable function to call when a parameter is missing. By default the parameter name is returned.pluralization: expects(dict), with keys below:some_enabled: expects(bool), determines if "some" should be supportedsome_limit: expects(int), determines value which plural will be known as "some" if count is between this number or 1.