Skip to content

Commit d2dd28e

Browse files
tastybentoclaude
andcommitted
Accept underscore locale filenames by renaming to BCP-47 form
Files like pt_BR.yml were being silently ignored because Locale.forLanguageTag returns Locale.ROOT for underscore-separated tags. Instead, when a tag contains '_', replace it with '-', rename the file on disk to the corrected name, and load it normally. A warning is logged so server admins know the file was renamed. If a correctly-named file already exists the underscore file is skipped with a warning. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d3a04e2 commit d2dd28e

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/main/java/world/bentobox/bentobox/managers/LocalesManager.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,28 @@ public void loadLocalesFromFile(String localeFolder) {
272272
String tag = language.getName().substring(0, language.getName().length() - 4);
273273
Locale localeObject = Locale.forLanguageTag(tag);
274274

275-
// Skip files whose name does not parse to a real BCP-47 language tag.
276-
// e.g. "zh_CN.yml" (underscore) yields Locale.ROOT, which would otherwise show
277-
// up as a blank entry in the language selector panel.
275+
// If the tag uses underscores (e.g. pt_BR) it won't parse as a valid BCP-47 tag.
276+
// Silently fix it: rename the file to use '-' and load it under the corrected locale.
277+
if (localeObject.getLanguage().isEmpty() && tag.contains("_")) {
278+
String fixedTag = tag.replace('_', '-');
279+
File fixedFile = new File(language.getParentFile(), fixedTag + ".yml");
280+
if (!fixedFile.exists() && language.renameTo(fixedFile)) {
281+
plugin.logWarning("Locale file '" + localeFolder + "/" + language.getName()
282+
+ "' has been renamed to '" + fixedTag + ".yml' to conform to BCP-47 (use '-' not '_').");
283+
language = fixedFile;
284+
} else if (fixedFile.exists()) {
285+
plugin.logWarning("Locale file '" + localeFolder + "/" + language.getName()
286+
+ "' uses '_' instead of '-'; ignoring it because '" + fixedTag + ".yml' already exists.");
287+
continue;
288+
} else {
289+
plugin.logWarning("Locale file '" + localeFolder + "/" + language.getName()
290+
+ "' uses '_' instead of '-' and could not be renamed; loading it as '" + fixedTag + "'.");
291+
}
292+
tag = fixedTag;
293+
localeObject = Locale.forLanguageTag(tag);
294+
}
295+
296+
// Skip files that still don't parse to a real BCP-47 language tag after the fix attempt.
278297
if (localeObject.getLanguage().isEmpty()) {
279298
plugin.logWarning("Ignoring locale file '" + localeFolder + "/" + language.getName()
280299
+ "': '" + tag + "' is not a valid BCP-47 language tag (use '-' not '_').");

0 commit comments

Comments
 (0)