def text_direction(language_code: str) -> str: babel_language_code = lang_int2babel(language_code) try: locale = babel.Locale.parse(babel_language_code) except babel.UnknownLocaleError: print(f'Unrecognized Babel language code {babel_language_code} ' f'for interface language code {language_code}') return 'auto' else: return locale.text_direction
def test_message_syntax_valid_bulk_not_allowed(language_code: str, gender: str): if 'bulk_not_allowed' in translations.translations[language_code]: message = translations.translations[language_code]['bulk_not_allowed'] formatters.I18nFormatter( locale_identifier=lang_int2babel(language_code), get_gender=lambda value: gender, ).format( message, user='******', )
def test_message_syntax_valid_duplicates_instructions(language_code: str, number: int): if 'duplicates_instructions' in translations.translations[language_code]: message = translations.translations[language_code][ 'duplicates_instructions'] formatters.I18nFormatter( locale_identifier=lang_int2babel(language_code), get_gender=unused, ).format( message, lexemes=number, )
def test_message_syntax_valid_edit_unmatched_warning(language_code: str, number: int): if 'edit_unmatched_warning' in translations.translations[language_code]: message = translations.translations[language_code][ 'edit_unmatched_warning'] formatters.I18nFormatter( locale_identifier=lang_int2babel(language_code), get_gender=unused, ).format( message, forms=number, )
def test_message_syntax_valid_description_with_forms_and_senses( language_code: str, number: int): if 'description_with_forms_and_senses' in translations.translations[ language_code]: message = translations.translations[language_code][ 'description_with_forms_and_senses'] formatters.I18nFormatter( locale_identifier=lang_int2babel(language_code), get_gender=unused, ).format( message, description='description', forms=number, senses=number, )
def test_message_syntax_valid_edit_form_list_item(language_code: str, list: builtins.list[str], number: int): if 'edit_form_list_item' in translations.translations[language_code]: message = translations.translations[language_code][ 'edit_form_list_item'] formatters.I18nFormatter( locale_identifier=lang_int2babel(language_code), get_gender=unused, ).format( message, form_link='', grammatical_feature_labels=list, statements=number, )
def mw2py(mw: str, language: str, variables: list[str], lists: set[str]) -> str: locale = babel.Locale(lang_int2babel(language)) def replace_plural(match: re.Match) -> str: nonlocal locale, variables number = int(match[1]) variable = variables[number - 1] args = match[2].split('|') plurals = [] tag_args = [] for arg in args: key, _, text = arg.partition('=') if key.isnumeric(): plurals.append(arg) else: tag_args.append(arg) tags = [ tag for tag in ['zero', 'one', 'two', 'few', 'many'] if tag in locale.plural_form.tags ] tags = tags[:len(tag_args) - 1] + ['other'] for tag, tag_arg in zip(tags, tag_args): plurals.append(f'{tag}={tag_arg}') return '{' + variable + '!p:' + ':'.join(plurals) + '}' py = re.sub(r'\{\{PLURAL:\$([1-9][0-9]*)\|([^}]*)\}\}', replace_plural, mw) def replace_gender(match: re.Match) -> str: nonlocal variables number = int(match[1]) variable = variables[number - 1] args = match[2].split('|') genders = [] for gender, arg in zip(['m', 'f', 'n'], args): genders.append(f'{gender}={arg}') return '{' + variable + '!g:' + ':'.join(genders) + '}' py = re.sub(r'\{\{GENDER:\$([1-9][0-9]*)\|([^}]*)\}\}', replace_gender, py) def replace_hyperlink(match: re.Match) -> str: nonlocal variables number = int(match[1]) variable = variables[number - 1] inner_html = match[2] assert '{' not in inner_html and '}' not in inner_html return '{' + variable + '!h:' + inner_html + '}' py = re.sub(r'\[\$([1-9][0-9]*) ([^]]*)\]', replace_hyperlink, py) def replace_unconverted(match: re.Match) -> str: nonlocal variables number = int(match[1]) variable = variables[number - 1] if variable in lists: return '{' + variable + '!l}' else: return '{' + variable + '}' py = re.sub(r'\$([1-9][0-9]*)', replace_unconverted, py) return py
def message_with_kwargs(message_code: str, **kwargs) -> flask.Markup: template, language = message_with_language(message_code) message = I18nFormatter(locale_identifier=lang_int2babel(language), get_gender=get_gender).format(template, **kwargs) message = cast(flask.Markup, message) # I18nFormatter returns Markup given Markup return add_lang_if_needed(message, language)