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

Fix crash when system locale is complex #5158

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Locale;
import java.util.Locale.Category;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.Math.max;
import static org.terasology.engine.config.flexible.SettingArgument.constraint;
Expand Down Expand Up @@ -81,13 +83,29 @@

public final Setting<Locale> locale = setting(
type(Locale.class),
defaultValue(Locale.getDefault(Category.DISPLAY)),
defaultValue(getAdjustedLocale()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to only do this if the locale cannot be found, e.g. because it's a weird one?
This would allow to keep regional differences like en_US vs en_GB or en_NZ while fixing the macos issue.

name("${engine:menu#settings-language}"),
constraint(new LocaleConstraint(Locale.getAvailableLocales())) // TODO provide translate project's locales (Pirate lang don't works)

Check warning on line 88 in engine/src/main/java/org/terasology/engine/config/SystemConfig.java

View check run for this annotation

Terasology Jenkins.io / Open Tasks Scanner

TODO

NORMAL: provide translate project's locales (Pirate lang don't works)
);

@Override
public String getName() {
return "${engine:menu#system-settings-title}";
}

private static Locale getAdjustedLocale() {
Locale systemLocale = Locale.getDefault(Category.DISPLAY);

// Matches strings like xx_XX. e.g. en_US
final Pattern langRegionPattern = Pattern.compile("[a-z]{2}_[A-Z]{2}");

String input = systemLocale.toString();
Matcher matcher = langRegionPattern.matcher(input);

// If the locale is like that, convert it to just the language, e.g. en_US -> en
if (matcher.find()) {
systemLocale = Locale.forLanguageTag(systemLocale.getLanguage());
}
return systemLocale;
}
}
Loading