Skip to content
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
22 changes: 19 additions & 3 deletions core/src/main/java/org/apache/hop/i18n/GlobalMessageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,23 @@ public static String calculateString(
final boolean fallbackOnRoot) {

final Set<Locale> activeLocales = getActiveLocales();
boolean foundEmpty = false;
for (final Locale locale : activeLocales) {
final String string =
calculateString(
pkgNames, locale, key, parameters, resourceClass, bundleName, fallbackOnRoot);
if (string != null && string.isEmpty()) {
// An empty value is an untranslated key (Weblate writes those): try the next locale.
foundEmpty = true;
continue;
}
if (!isMissingKey(string)) {
return string;
}
}
if (foundEmpty) {
return "";
}
if (logNotFoundError) {
final StringBuilder msg = new StringBuilder();
msg.append("Message not found in the preferred and failover locale: key=[")
Expand All @@ -290,15 +299,22 @@ private static String calculateString(
final Class<?> resourceClass,
final String bundleName,
final boolean fallbackOnRoot) {
String empty = null;
for (final String packageName : pkgNames) {
try {
return calculateString(
packageName, locale, key, parameters, resourceClass, bundleName, fallbackOnRoot);
final String string =
calculateString(
packageName, locale, key, parameters, resourceClass, bundleName, fallbackOnRoot);
if (!string.isEmpty()) {
return string;
}
// Untranslated in this package: another package may still have a translation.
empty = string;
} catch (final MissingResourceException e) {
continue;
}
}
return null;
return empty;
}

static String calculateString(
Expand Down
41 changes: 41 additions & 0 deletions core/src/test/java/org/apache/hop/i18n/GlobalMessageUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,45 @@ void calculateString() {
GlobalMessages.PKG,
GlobalMessages.BUNDLE_NAME));
}

@Test
void emptyTranslationFallsBackToTheNextLocale() {
// messages_fr_FR has "emptyKey=", the way Weblate writes an untranslated key
LanguageChoice.getInstance().setDefaultLocale(Locale.FRANCE);
assertEquals(
"Empty in French foo",
GlobalMessageUtil.calculateString(
new String[] {GlobalMessages.SYSTEM_BUNDLE_PACKAGE},
"emptyKey",
new String[] {"foo"},
GlobalMessages.PKG,
GlobalMessages.BUNDLE_NAME));
}

@Test
void emptyTranslationFallsBackToTheNextPackageFirst() {
// empty in i18n/messages_fr_FR, translated in foo/messages_fr
LanguageChoice.getInstance().setDefaultLocale(Locale.FRANCE);
assertEquals(
"Traduit dans foo foo",
GlobalMessageUtil.calculateString(
new String[] {GlobalMessages.SYSTEM_BUNDLE_PACKAGE, "org.apache.hop.foo"},
"emptyInFrenchOnly",
new String[] {"foo"},
GlobalMessages.PKG,
GlobalMessages.BUNDLE_NAME));
}

@Test
void keyThatIsEmptyEverywhereStaysEmpty() {
LanguageChoice.getInstance().setDefaultLocale(Locale.FRANCE);
assertEquals(
"",
GlobalMessageUtil.calculateString(
new String[] {GlobalMessages.SYSTEM_BUNDLE_PACKAGE},
"alwaysEmpty",
new String[] {},
GlobalMessages.PKG,
GlobalMessages.BUNDLE_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
#

someKey=Une certaine valeur {0}
emptyInFrenchOnly=Traduit dans foo {0}
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
#

someKey=Some Value {0}
emptyKey=Empty in French {0}
alwaysEmpty=
emptyInFrenchOnly=Only in English {0}
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
#

someKey=Une certaine valeur {0}
emptyKey=
emptyInFrenchOnly=
Loading