A global mobile app stores message templates for different locales. Implement a formatter that selects the best available localized template and replaces placeholders with provided values.
Write a function format_message(translations, locale, key, params) that returns the formatted string for a message key.
translations: a dictionary where each locale maps to another dictionary of message keys to template stringslocale: a locale string such as "en-US" or "fr-CA"key: the message key to renderparams: a dictionary of placeholder names to replacement valuesen for en-US)enkey if no template existsPlaceholders use {name} syntax. Replace only placeholders present in params; leave unknown placeholders unchanged.
Example 1
translations = {"en": {"greet": "Hello, {name}!"}, "fr": {"greet": "Bonjour, {name}!"}}, locale = "fr-CA", key = "greet", params = {"name": "Ava"}"Bonjour, Ava!"fr-CA is missing, so the function falls back to base language fr.Example 2
translations = {"en": {"items": "{count} items"}}, locale = "de-DE", key = "items", params = {"count": 3}"3 items"de-DE nor de exists, so the function falls back to en.1 <= number of locales <= 10^31 <= number of keys across all locales <= 10^40 <= number of params <= 50