def tweak_message(message): """We piggyback on jinja2's babel_extract() (really, Babel's extract_* functions) but they don't support some things we need so this function will tweak the message. Specifically: 1) We strip whitespace from the msgid. Jinja2 will only strip whitespace from the ends of a string so linebreaks show up in your .po files still. 2) Babel doesn't support context (msgctxt). We hack that in ourselves here. """ if isinstance(message, str): message = strip_whitespace(message) elif isinstance(message, tuple): # A tuple of 2 has context, 3 is plural, 4 is plural with context if len(message) == 2: message = add_context(message[1], message[0]) elif len(message) == 3: if all(isinstance(x, str) for x in message[:2]): singular, plural, num = message message = (strip_whitespace(singular), strip_whitespace(plural), num) elif len(message) == 4: singular, plural, num, ctxt = message message = (add_context(ctxt, strip_whitespace(singular)), add_context(ctxt, strip_whitespace(plural)), num) return message
def tweak_message(message): """We piggyback on jinja2's babel_extract() (really, Babel's extract_* functions) but they don't support some things we need so this function will tweak the message. Specifically: 1) We strip whitespace from the msgid. Jinja2 will only strip whitespace from the ends of a string so linebreaks show up in your .po files still. 2) Babel doesn't support context (msgctxt). We hack that in ourselves here. """ if isinstance(message, basestring): message = strip_whitespace(message) elif isinstance(message, tuple): # A tuple of 2 has context, 3 is plural, 4 is plural with context if len(message) == 2: message = add_context(message[1], message[0]) elif len(message) == 3: if all(isinstance(x, basestring) for x in message[:2]): singular, plural, num = message message = (strip_whitespace(singular), strip_whitespace(plural), num) elif len(message) == 4: singular, plural, num, ctxt = message message = (add_context(ctxt, strip_whitespace(singular)), add_context(ctxt, strip_whitespace(plural)), num) return message
def translate(text, files): """Search a list of .lang files for a translation""" lang = fix_case(translation.get_language()) # don't attempt to translate the default language. if lang == settings.LANGUAGE_CODE: return Markup(text) tweaked_text = strip_whitespace(text) for file_ in files: key = "dotlang-%s-%s" % (lang, file_) rel_path = os.path.join('locale', lang, '%s.lang' % file_) trans = cache.get(key) if trans is None: path = os.path.join(settings.ROOT, rel_path) trans = parse(path) cache.set(key, trans, settings.DOTLANG_CACHE) if tweaked_text in trans: original = FORMAT_IDENTIFIER_RE.findall(text) translated = FORMAT_IDENTIFIER_RE.findall(trans[tweaked_text]) if set(original) != set(translated): explanation = ('The translation has a different set of ' 'replaced text (aka %s)') message = '%s\n\n%s\n%s' % (explanation, text, trans[tweaked_text]) mail_error(rel_path, message) return Markup(text) return Markup(trans[tweaked_text]) return Markup(text)
def translate(text, files): """Search a list of .lang files for a translation""" lang = translation.get_language(True) # don't attempt to translate the default language. if lang == settings.LANGUAGE_CODE: return Markup(text) tweaked_text = strip_whitespace(text) for file_ in files: key = "dotlang-%s-%s" % (lang, file_) path = str(settings.LOCALES_PATH / lang / f'{file_}.lang') trans = cache.get(key) if trans is None: trans = parse(path) cache.set(key, trans) if tweaked_text in trans: original = FORMAT_IDENTIFIER_RE.findall(text) translated = FORMAT_IDENTIFIER_RE.findall(trans[tweaked_text]) if set(original) != set(translated): explanation = ('The translation has a different set of ' 'replaced text (aka %s)') message = '%s\n\n%s\n%s' % (explanation, text, trans[tweaked_text]) mail_error(path, message) return Markup(text) return Markup(trans[tweaked_text]) return Markup(text)
def get_lang_ids(self, template_str): found = [] found.extend((m.start(), strip_whitespace(m['string']), m['args']) for m in GETTEXT_RE.finditer(template_str)) found.extend((m.start(), trans_to_lang(m['string']), m['args']) for m in TRANS_BLOCK_RE.finditer(template_str)) found.sort(key=lambda t: t[0]) return {string: args for pos, string, args in found}
def trans_to_lang(string): string = strip_whitespace(string) return TRANS_PLACEABLE_RE.sub( lambda m: '%({})s'.format(m.group('var')), string )
def _parse_block(self, parser, allow_pluralize): ref, buffer = super()._parse_block(parser, allow_pluralize) return ref, strip_whitespace(buffer)
def _parse_block(self, parser, allow_pluralize): ref, buffer = super(I18nExtension, self)._parse_block(parser, allow_pluralize) return ref, strip_whitespace(buffer)