Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore/test: add LocaleRule fixture #1177

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
};
}
}
Loading