From 9c580b61a2b210e2d46b0fbf26643cdab5d8d5f3 Mon Sep 17 00:00:00 2001 From: ialokim <ialokim@mailbox.org> Date: Mon, 29 Nov 2021 16:41:00 +0100 Subject: [PATCH] add (graceful) error handling to i18n methods --- .../utils/InternationalizationUtils.kt | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/org/briarproject/briar/desktop/utils/InternationalizationUtils.kt b/src/main/kotlin/org/briarproject/briar/desktop/utils/InternationalizationUtils.kt index f2928561d8..b50699c108 100644 --- a/src/main/kotlin/org/briarproject/briar/desktop/utils/InternationalizationUtils.kt +++ b/src/main/kotlin/org/briarproject/briar/desktop/utils/InternationalizationUtils.kt @@ -1,7 +1,9 @@ package org.briarproject.briar.desktop.utils import com.ibm.icu.text.MessageFormat +import mu.KotlinLogging import java.util.Locale +import java.util.MissingResourceException import java.util.ResourceBundle /** @@ -12,30 +14,44 @@ import java.util.ResourceBundle */ object InternationalizationUtils { + private val LOG = KotlinLogging.logger {} + /** * Returns the translated text of the string identified with `key` */ - fun i18n(key: String): String { - val resourceBundle = createResourceBundle() - return resourceBundle.getString(key) - } + fun i18n(key: String): String = + try { + val resourceBundle = createResourceBundle() + resourceBundle.getString(key) + } catch (e: MissingResourceException) { + LOG.warn { "Missing string resource for key '$key'" } + "" + } /** * Returns the translated text of a string with plurals identified with `key` */ - fun i18nP(key: String, amount: Int): String { - val pattern: String = i18n(key) - val messageFormat = MessageFormat(pattern, Locale.getDefault()) - return messageFormat.format(arrayOf(amount)) - } + fun i18nP(key: String, amount: Int): String = + try { + val pattern: String = i18n(key) + val messageFormat = MessageFormat(pattern, Locale.getDefault()) + messageFormat.format(arrayOf(amount)) + } catch (e: IllegalArgumentException) { + LOG.warn { "Pattern does not match arguments for resource '$key' and locale '${Locale.getDefault()}" } + "" + } /** * Returns the translated text of a formatted string with */ - fun i18nF(key: String, vararg params: Any): String { - val pattern: String = i18n(key) - return java.text.MessageFormat.format(pattern, *params) - } + fun i18nF(key: String, vararg params: Any): String = + try { + val pattern: String = i18n(key) + java.text.MessageFormat.format(pattern, *params) + } catch (e: IllegalArgumentException) { + LOG.warn { "Pattern does not match arguments for resource '$key'" } + "" + } /** * Returns the resource bundle used for i18n at Briar Desktop -- GitLab