def icu_format_message(locale_id: str, message: str, arguments: _MessageArguments = {}) -> str: """Substitute arguments into ICU-style message. You can have variable substitution, plurals, selects and nested messages. Raises `ICUError` in case of incorrectly formatted message. The arguments must be a dict """ return MessageFormat(message, Locale.createFromName(locale_id)).format( list(arguments.keys()), [Formattable(x) for x in arguments.values()])
def format(string, values=None): """Translates a string with the given current locale""" ctx = _request_ctx_stack.top locale = get_locale() icu_msg = get_message(string) msg = MessageFormat(icu_msg, locale) if values is not None: keys = [] vals = [] for (k, v) in values.items(): keys.append(k) vals.append(Formattable(v)) return msg.format(keys, vals) return msg.format('')
def _format_message(self, message: str, arguments: _MessageArguments = {}, html_escape: bool = True) -> str: """Substitute arguments into ICU-style message. You can have variable substitution, plurals, selects and nested messages. Raises `ICUError` in case of incorrectly formatted message. The arguments must be a dict """ return MessageFormat(message, self.icu_locale).format( list(arguments.keys()), [ Formattable( escape(x) if html_escape and isinstance(x, str) else x) for x in arguments.values() ], )
def icu_format_html_message( locale_id: str, message: str, arguments: _MessageArguments = {}, tags: _TagMapping = {}, ) -> str: """Substitute arguments into ICU-style HTML message. You can have variable substitution, plurals, selects and nested messages. You can also replace HTML tag placeholders. Raises `ICUError` in case of incorrectly formatted message. """ return MessageFormat(restore_tags( message, tags), Locale.createFromName(locale_id)).format( list(arguments.keys()), [ Formattable(escape(x) if isinstance(x, str) else x) for x in arguments.values() ], )