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 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
import org.terasology.engine.config.flexible.constraints.LocaleConstraint;
import org.terasology.engine.config.flexible.constraints.NumberRangeConstraint;

import java.util.Arrays;
import java.util.Locale;
import java.util.Locale.Category;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.Math.max;
import static org.terasology.engine.config.flexible.SettingArgument.constraint;
Expand Down Expand Up @@ -81,13 +86,34 @@ public class SystemConfig extends AutoConfig {

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)
constraint(new LocaleConstraint(
// Locale.getAvailableLocales() + non-standard "pr" (pirate) tag
Stream.concat(Arrays.stream(Locale.getAvailableLocales()), Stream.of(Locale.forLanguageTag("pr")))
.collect(Collectors.toSet())))
);

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

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

// Matches unusual locales on Mac OS created from the user's language and location, e.g. en_UA
if (!Arrays.asList(Locale.getAvailableLocales()).contains(systemLocale)) {
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_UA -> en
if (matcher.find()) {
systemLocale = Locale.forLanguageTag(systemLocale.getLanguage());
}
}
return systemLocale;
}
}
Loading