Skip to content

Commit

Permalink
Merge pull request #62 from salesforce/fix-dictionary
Browse files Browse the repository at this point in the history
fix severe error for Haitian-Creole declension
  • Loading branch information
yoikawa authored Sep 17, 2024
2 parents 23f2eb2 + df5765e commit 91a742b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
30 changes: 24 additions & 6 deletions src/main/java/com/force/i18n/grammar/Adjective.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,6 +33,9 @@ public abstract class Adjective extends NounModifier {
@Override
public abstract Map<? extends AdjectiveForm, String> getAllValues();

/**
* @deprecated use {@link #Adjective(LanguageDeclention, String, LanguagePosition)}
*/
@Deprecated
protected Adjective(LanguageDeclension declension, String name) {
this(declension, name, declension.getDefaultAdjectivePosition());
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -118,7 +136,7 @@ public boolean defaultValidate(String name, Set<? extends AdjectiveForm> 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;
Expand Down Expand Up @@ -171,10 +189,10 @@ public boolean defaultValidate(String name, Set<? extends AdjectiveForm> 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");
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/main/java/com/force/i18n/grammar/ArticledDeclension.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -168,7 +169,10 @@ public Map<? extends NounForm, String> 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
Expand All @@ -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
}
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 91a742b

Please sign in to comment.