From 24a151f45b5104471a1a07e2a5aa72066cba3be9 Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 May 2018 13:44:35 +0200 Subject: Internationalization and localization of strings --- jfr_playoff/i18n.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 jfr_playoff/i18n.py (limited to 'jfr_playoff/i18n.py') diff --git a/jfr_playoff/i18n.py b/jfr_playoff/i18n.py new file mode 100644 index 0000000..0caa107 --- /dev/null +++ b/jfr_playoff/i18n.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- + +import re + +PLAYOFF_I18N_DEFAULTS = { + 'SCORE': 'wynik', + 'FINAL_STANDINGS': 'klasyfikacja końcowa', + 'STANDINGS_PLACE': 'miejsce', + 'STANDINGS_TEAM': 'drużyna', + 'STANDINGS_CAPTIONS': 'legenda', + 'FOOTER_GENERATED': 'strona wygenerowana', + 'SWISS_DEFAULT_LABEL': 'Turniej o %d. miejsce' +} + +class PlayoffI18N(object): + + def __init__(self, settings): + self.settings = settings + self.string_match = re.compile(r'{{(.*)}}') + + def localize(self, string): + return re.sub( + self.string_match, + lambda x: self.__get_translation(x.group(1)), + string) + + def __get_translation(self, string): + for dictionary in [self.settings, PLAYOFF_I18N_DEFAULTS]: + if string in dictionary: + return dictionary[string].decode('utf8') + return string -- cgit v1.2.3 From eaa2912f4dc571cb92fb68a6b0f53cc5fbff2670 Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 May 2018 13:55:11 +0200 Subject: If i18n string is not found, pass-thru the original braces --- jfr_playoff/i18n.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'jfr_playoff/i18n.py') diff --git a/jfr_playoff/i18n.py b/jfr_playoff/i18n.py index 0caa107..e22a563 100644 --- a/jfr_playoff/i18n.py +++ b/jfr_playoff/i18n.py @@ -28,4 +28,4 @@ class PlayoffI18N(object): for dictionary in [self.settings, PLAYOFF_I18N_DEFAULTS]: if string in dictionary: return dictionary[string].decode('utf8') - return string + return '{{%s}}' % (string) -- cgit v1.2.3 From b662da599a1cf673790648f5c0c7b6962e529bfa Mon Sep 17 00:00:00 2001 From: emkael Date: Tue, 1 May 2018 14:11:38 +0200 Subject: Debug info for i18n --- jfr_playoff/i18n.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'jfr_playoff/i18n.py') diff --git a/jfr_playoff/i18n.py b/jfr_playoff/i18n.py index e22a563..c4134a9 100644 --- a/jfr_playoff/i18n.py +++ b/jfr_playoff/i18n.py @@ -2,6 +2,8 @@ import re +from jfr_playoff.logger import PlayoffLogger + PLAYOFF_I18N_DEFAULTS = { 'SCORE': 'wynik', 'FINAL_STANDINGS': 'klasyfikacja końcowa', @@ -27,5 +29,10 @@ class PlayoffI18N(object): def __get_translation(self, string): for dictionary in [self.settings, PLAYOFF_I18N_DEFAULTS]: if string in dictionary: - return dictionary[string].decode('utf8') + translation = dictionary[string].decode('utf8') + PlayoffLogger.get('i18n').info( + 'translation for %s: %s', string, translation) + return translation + PlayoffLogger.get('i18n').info( + 'translation for %s not found', string) return '{{%s}}' % (string) -- cgit v1.2.3