Use two letter language codes with country codes for dialects

This commit is contained in:
Vankka 2023-08-26 15:47:58 +03:00
parent 1c24055108
commit f8c323e7de
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
6 changed files with 16 additions and 19 deletions

View File

@ -20,7 +20,7 @@ jobs:
pull_request_title: "New Crowdin translations" pull_request_title: "New Crowdin translations"
pull_request_body: "" pull_request_body: ""
source: "source.yaml" source: "source.yaml"
translation: "/%original_path%/src/main/resources/translations/%three_letters_code%.yaml" translation: "/%original_path%/src/main/resources/translations/%two_letters_code%.yaml"
project_id: ${{ secrets.CROWDIN_PROJECT_ID }} project_id: ${{ secrets.CROWDIN_PROJECT_ID }}
token: ${{ secrets.CROWDIN_PERSONAL_TOKEN }} token: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
env: env:

View File

@ -28,7 +28,7 @@ jobs:
download_translations: false download_translations: false
localization_branch_name: i18n localization_branch_name: i18n
source: "source.yaml" source: "source.yaml"
translation: "/%original_path%/src/main/resources/translations/%three_letters_code%.yaml" translation: "/%original_path%/src/main/resources/translations/%two_letters_code%.yaml"
project_id: ${{ secrets.CROWDIN_PROJECT_ID }} project_id: ${{ secrets.CROWDIN_PROJECT_ID }}
token: ${{ secrets.CROWDIN_PERSONAL_TOKEN }} token: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
env: env:

View File

@ -458,11 +458,7 @@ public abstract class AbstractDiscordSRV<
if (config != null) { if (config != null) {
String defaultLanguage = config.messages.defaultLanguage; String defaultLanguage = config.messages.defaultLanguage;
if (StringUtils.isNotBlank(defaultLanguage)) { if (StringUtils.isNotBlank(defaultLanguage)) {
for (Locale locale : Locale.getAvailableLocales()) { return Locale.forLanguageTag(defaultLanguage);
if (locale.getISO3Language().equals(defaultLanguage)) {
return locale;
}
}
} }
} }

View File

@ -17,7 +17,6 @@ public abstract class MessagesConfigManager<C extends MessagesConfig> {
private final Map<Locale, MessagesConfigSingleManager<C>> configs = new LinkedHashMap<>(); private final Map<Locale, MessagesConfigSingleManager<C>> configs = new LinkedHashMap<>();
private final DiscordSRV discordSRV; private final DiscordSRV discordSRV;
private final Logger logger; private final Logger logger;
private boolean multi;
public MessagesConfigManager(DiscordSRV discordSRV) { public MessagesConfigManager(DiscordSRV discordSRV) {
this.discordSRV = discordSRV; this.discordSRV = discordSRV;
@ -45,10 +44,7 @@ public abstract class MessagesConfigManager<C extends MessagesConfig> {
throw new ConfigException("MainConfig not available"); throw new ConfigException("MainConfig not available");
} }
boolean multiple = config.messages.multiple; if (config.messages.multiple) {
Locale defaultLocale = discordSRV.defaultLocale();
if (multiple) {
try { try {
Path messagesDirectory = directory(); Path messagesDirectory = directory();
if (!Files.exists(messagesDirectory)) { if (!Files.exists(messagesDirectory)) {
@ -83,7 +79,8 @@ public abstract class MessagesConfigManager<C extends MessagesConfig> {
throw new ConfigException("Failed to initialize messages configs", t); throw new ConfigException("Failed to initialize messages configs", t);
} }
} else { } else {
configs.put(Locale.US, new MessagesConfigSingleManager<>(discordSRV, this, Locale.US, false)); Locale defaultLocale = discordSRV.defaultLocale();
configs.put(defaultLocale, new MessagesConfigSingleManager<>(discordSRV, this, defaultLocale, false));
} }
for (Map.Entry<Locale, MessagesConfigSingleManager<C>> entry : configs.entrySet()) { for (Map.Entry<Locale, MessagesConfigSingleManager<C>> entry : configs.entrySet()) {

View File

@ -105,11 +105,14 @@ public abstract class TranslatedConfigManager<T extends Config, LT extends Abstr
} }
private ConfigurationNode getTranslationRoot() throws ConfigurateException { private ConfigurationNode getTranslationRoot() throws ConfigurateException {
String languageCode = locale().getISO3Language(); String languageCode = locale().getLanguage();
String countryCode = locale().getCountry();
ClassLoader classLoader = discordSRV.getClass().getClassLoader(); ClassLoader classLoader = discordSRV.getClass().getClassLoader();
URL resourceURL = classLoader.getResource("translations/" + languageCode + ".yaml");
URL resourceURL = classLoader.getResource("translations/" + languageCode + "_" + countryCode + ".yaml");
if (resourceURL == null) { if (resourceURL == null) {
resourceURL = classLoader.getResource("translations/eng.yaml"); resourceURL = classLoader.getResource("translations/" + languageCode + ".yaml");
} }
if (resourceURL == null) { if (resourceURL == null) {
return null; return null;

View File

@ -6,10 +6,11 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
@ConfigSerializable @ConfigSerializable
public class MessagesMainConfig { public class MessagesMainConfig {
@Comment("The 3 letter ISO 639-2 code for the default language, if left blank the system default will be used") @Comment("The language code for the default language, if left blank the system default will be used.\n"
public String defaultLanguage = "eng"; + "This should be in the ISO 639-1 format or ISO 639-1 (for example \"en\"), a underscore and a ISO 3166-1 country code to specify dialect (for example \"pt_BR\")")
public String defaultLanguage = "en";
@Comment("If there should be multiple messages files, one for every language") @Comment("If there should be a messages file per language (based on the player's or user's language), otherwise using the default")
public boolean multiple = false; public boolean multiple = false;
@Comment("If all languages provided with DiscordSRV should be loaded into the messages directory, only functions when \"multiple\" is set to true") @Comment("If all languages provided with DiscordSRV should be loaded into the messages directory, only functions when \"multiple\" is set to true")