From df5765e9c52b589e111ab0a53659bc984680b337 Mon Sep 17 00:00:00 2001 From: Yoshiyuki Oikawa Date: Tue, 17 Sep 2024 03:34:18 +0000 Subject: [PATCH] fix severe error for Haitian-Creole declention --- .../com/force/i18n/grammar/Adjective.java | 30 +++++++++++++++---- .../i18n/grammar/ArticledDeclension.java | 16 +++++----- .../i18n/grammar/impl/SimpleDeclension.java | 5 ++-- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/force/i18n/grammar/Adjective.java b/src/main/java/com/force/i18n/grammar/Adjective.java index fc787f0..40fdb2d 100644 --- a/src/main/java/com/force/i18n/grammar/Adjective.java +++ b/src/main/java/com/force/i18n/grammar/Adjective.java @@ -13,6 +13,7 @@ import java.util.Set; import java.util.logging.Logger; +import com.force.i18n.commons.text.TextUtil; import com.google.common.collect.ImmutableSet; /** * An adjective or other noun modifier as stored in a LanguageDictionary. @@ -32,6 +33,9 @@ public abstract class Adjective extends NounModifier { @Override public abstract Map getAllValues(); + /** + * @deprecated use {@link #Adjective(LanguageDeclention, String, LanguagePosition)} + */ @Deprecated protected Adjective(LanguageDeclension declension, String name) { this(declension, name, declension.getDefaultAdjectivePosition()); @@ -53,11 +57,25 @@ public final String getString(ModifierForm form) { return getString((AdjectiveForm)form); } + private String getDefaultValue(boolean doFallback) { + String defaultValue = getString(getDeclension().getAdjectiveForm(getDeclension().getDefaultStartsWith(), + getDeclension().getDefaultGender(), LanguageNumber.SINGULAR, getDeclension().getDefaultCase(), + getDeclension().getDefaultArticle(), getDeclension().getDefaultPossessive())); + if (defaultValue != null || !doFallback) return defaultValue; + + // not perfect, but trying to return something instead of null + for (String val : this.getAllValues().values()) { + if (!TextUtil.isNullEmptyOrWhitespace(val)) { + // error should be already logged by defaultValidate + return val; + } + } + return null; // something went wrong + } + @Override public String getDefaultValue() { - return getString(getDeclension().getAdjectiveForm(getDeclension().getDefaultStartsWith(), - getDeclension().getDefaultGender(), LanguageNumber.SINGULAR, - getDeclension().getDefaultCase(), getDeclension().getDefaultArticle(), getDeclension().getDefaultPossessive())); + return getDefaultValue(true); } /** @@ -118,7 +136,7 @@ public boolean defaultValidate(String name, Set require for (AdjectiveForm form : getDeclension().getAdjectiveForms()) { if (getString(form) == null) { if (requiredForms.contains(form)) { - logger.fine("###\tError: The adjective " + name + " is missing required " + form + " form"); + logger.fine(() -> "###\tError: The adjective " + name + " is missing required " + form + " form"); // TODO: uncomment the return false below once we actually handle validation // Presently, the return value is simply ignored // return false; @@ -171,10 +189,10 @@ public boolean defaultValidate(String name, Set require // so default to the absolute default value s = getDefaultValue(); if (s == null) { - logger.fine("###\tError: The adjective " + name + " has no " + form + " form and no default could be found"); + logger.fine(() -> "###\tError: The adjective " + name + " has no " + form + " form and no default could be found"); return false; } else { - logger.fine("###\tError: The adjective " + name + " has no obvious default for " + form + "form"); + logger.fine(() -> "###\tError: The adjective " + name + " has no obvious default for " + form + "form"); } } diff --git a/src/main/java/com/force/i18n/grammar/ArticledDeclension.java b/src/main/java/com/force/i18n/grammar/ArticledDeclension.java index ab6312d..6499f1c 100644 --- a/src/main/java/com/force/i18n/grammar/ArticledDeclension.java +++ b/src/main/java/com/force/i18n/grammar/ArticledDeclension.java @@ -134,7 +134,8 @@ protected void setString(ArticleForm form, String value) { @Override public boolean validate(String name) { if (this.value == null) { - logger.info("###\tError: The article " + name + " has no form"); + HumanLanguage language = this.getDeclension().getLanguage(); + logger.info(() -> "###\tError: " + language + "The article " + name + " has no form"); return false; } return true; @@ -168,7 +169,10 @@ public Map getAllDefinedValues() { @Override public String getDefaultString(boolean isPlural) { - return isPlural && plural != null ? plural : singular; + if (isPlural && plural != null) return plural; + + // there's a corner case that dictionary has only plural form. try to return non-null + return singular != null ? singular : plural; } @Override @@ -187,14 +191,12 @@ protected void setString(String value, NounForm form) { value = intern(value); if (form.getNumber().isPlural()) { this.plural = value; - if (value != null && value.equals(this.singular)) - { + if (value != null && value.equals(this.singular)) { this.singular = value; // Keep one reference for serialization } } else { this.singular = value; - if (value != null && value.equals(this.plural)) - { + if (value != null && value.equals(this.plural)) { this.plural = value; // Keep one reference for serialization } } @@ -203,7 +205,7 @@ protected void setString(String value, NounForm form) { @Override protected boolean validateValues(String name, LanguageCase _case) { if (this.singular == null) { - logger.info("###\tError: The noun " + name + " has no singular form"); + logger.info(() -> "###\tError: The noun " + name + " has no singular form"); return false; } return true; diff --git a/src/main/java/com/force/i18n/grammar/impl/SimpleDeclension.java b/src/main/java/com/force/i18n/grammar/impl/SimpleDeclension.java index 021bc75..38b0d9b 100644 --- a/src/main/java/com/force/i18n/grammar/impl/SimpleDeclension.java +++ b/src/main/java/com/force/i18n/grammar/impl/SimpleDeclension.java @@ -90,8 +90,9 @@ protected void setString(String value, NounForm form) { protected boolean validateValues(String name, LanguageCase _case) { ///CLOVER:OFF if (this.value == null) { - // this is wrong. the noun should have singular form - logger.severe("###\tError: The noun " + name + " has no value"); + // this is wrong. the noun should have singular form + HumanLanguage language = this.getDeclension().getLanguage(); + logger.severe(() -> "###\tError: " + language.getLocaleString() + "The noun " + name + " has no value"); return false; } ///CLOVER:ON