From 31c3365b1746d6703ea68923fb2509d57c2f83da Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Thu, 7 Nov 2024 11:37:06 +0900 Subject: [PATCH] feat/test: add LocaleRule fixture (#1177) Signed-off-by: Hiroshi Miura --- src/org/omegat/util/OStrings.java | 13 +++- test/fixtures/org/omegat/util/LocaleRule.java | 59 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/org/omegat/util/LocaleRule.java diff --git a/src/org/omegat/util/OStrings.java b/src/org/omegat/util/OStrings.java index 6e7b3a375f..a3cfd4595f 100644 --- a/src/org/omegat/util/OStrings.java +++ b/src/org/omegat/util/OStrings.java @@ -28,6 +28,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.Locale; import java.util.PropertyResourceBundle; import java.util.ResourceBundle; import java.util.function.Function; @@ -71,8 +72,10 @@ private OStrings() { IS_BETA = !b.getString("beta").isEmpty(); } + private static final String BASENAME = "org/omegat/Bundle"; + /** Resource bundle that contains all the strings */ - private static ResourceBundle bundle = ResourceBundle.getBundle("org/omegat/Bundle"); + private static ResourceBundle bundle = ResourceBundle.getBundle(BASENAME); /** * Returns resource bundle. @@ -81,6 +84,14 @@ public static ResourceBundle getResourceBundle() { return bundle; } + /** + * Loads resources with the specified locale. + * @param locale Locale to load. + */ + public static void loadBundle(Locale locale) { + bundle = ResourceBundle.getBundle(BASENAME, locale); + } + /** * Loads resources from the specified file. If the file cannot be loaded, * resources are reverted to the default locale. Useful when testing diff --git a/test/fixtures/org/omegat/util/LocaleRule.java b/test/fixtures/org/omegat/util/LocaleRule.java new file mode 100644 index 0000000000..8107feacd1 --- /dev/null +++ b/test/fixtures/org/omegat/util/LocaleRule.java @@ -0,0 +1,59 @@ +/************************************************************************** + OmegaT - Computer Assisted Translation (CAT) tool + with fuzzy matching, translation memory, keyword search, + glossaries, and translation leveraging into updated projects. + + Copyright (C) 2024 Hiroshi Miura + Home page: https://www.omegat.org/ + Support center: https://omegat.org/support + + This file is part of OmegaT. + + OmegaT is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OmegaT is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + **************************************************************************/ + +package org.omegat.util; + +import java.util.Locale; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +public class LocaleRule implements TestRule { + private final Locale testLocale; + private Locale originalLocale; + + public LocaleRule(Locale locale) { + this.testLocale = locale; + } + + @Override + public Statement apply(Statement base, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + originalLocale = Locale.getDefault(); + Locale.setDefault(testLocale); + OStrings.loadBundle(testLocale); + try { + base.evaluate(); + } finally { + Locale.setDefault(originalLocale); + OStrings.loadBundle(originalLocale); + } + } + }; + } +}