Skip to content

Commit

Permalink
feat/test: add LocaleRule fixture (#1177)
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr authored Nov 7, 2024
1 parent 294cabf commit 31c3365
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/org/omegat/util/OStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
59 changes: 59 additions & 0 deletions test/fixtures/org/omegat/util/LocaleRule.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
**************************************************************************/

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);
}
}
};
}
}

0 comments on commit 31c3365

Please sign in to comment.