com.ibm.icu.text.PluralRules Java Examples

The following examples show how to use com.ibm.icu.text.PluralRules. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PluralRulesTest.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testPluralRules() throws Exception {
    PluralRules rules = PluralRules.forLocale(language.getLocale());
    StringBuilder function = new StringBuilder();
    function.append("var select = ");
    function.append(PluralRulesJsImpl.getSelectFunction(language.getLocale()));
    function.append(";");
       ScriptEngine engine = JsTestUtils.getScriptEngine();
       engine.eval(function.toString());
       String[] result = (String[]) engine.eval("var result = [];  for (var i = -0; i < 200; i+=0.5) {result.push(select(i));}; Java.to(result,'java.lang.String[]');");
       List<String> expected = new ArrayList<>(result.length);
       for (double i = 0; i < 200; i+= 0.5) {
           expected.add(rules.select(i));
       }
       //for (String keyword : rules.getKeywords()) {
       //    System.out.println(language.getLocale().getLanguage() + " " + keyword + " " + rules.getRules(keyword));
       //}
       Assert.assertArrayEquals(expected.toArray(new String[expected.size()]), result);
}
 
Example #2
Source File: PluralRulesObject.java    From es6draft with MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")
com.ibm.icu.text.PluralRules.FixedDecimal toFixedDecimal(double n) {
    NumberFormat nf = getNumberFormat();

    StringBuffer sb = new StringBuffer();
    FieldPosition fp = new FieldPosition(DecimalFormat.FRACTION_FIELD);
    nf.format(n, sb, fp);

    int v = fp.getEndIndex() - fp.getBeginIndex();
    long f = 0;
    if (v > 0) {
        ParsePosition pp = new ParsePosition(fp.getBeginIndex());
        f = nf.parse(sb.toString(), pp).longValue();
    }
    return new com.ibm.icu.text.PluralRules.FixedDecimal(n, v, f);
}
 
Example #3
Source File: PluralRulesLoader.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the plural rules for the the locale. If we don't have data,
 * com.ibm.icu.text.PluralRules.DEFAULT is returned.
 */
public PluralRules forLocale(ULocale locale, PluralRules.PluralType type) {
    String rulesId = getRulesIdForLocale(locale, type);
    if (rulesId == null || rulesId.trim().length() == 0) {
        return PluralRules.DEFAULT;
    }
    PluralRules rules = getRulesForRulesId(rulesId);
    if (rules == null) {
        rules = PluralRules.DEFAULT;
    }
    return rules;
}
 
Example #4
Source File: DefaultLanguagePluralRulesImpl.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private PluralCategory fromString(String string) {
	switch (string) {
	case PluralRules.KEYWORD_ZERO: return PluralCategory.ZERO;
	case PluralRules.KEYWORD_ONE: return PluralCategory.ONE;
	case PluralRules.KEYWORD_TWO: return PluralCategory.TWO;
	case PluralRules.KEYWORD_FEW: return PluralCategory.FEW;
	case PluralRules.KEYWORD_MANY: return PluralCategory.MANY;
	case PluralRules.KEYWORD_OTHER: return PluralCategory.OTHER;
	}
	return PluralCategory.OTHER;
}
 
Example #5
Source File: PluralsHolder.java    From mojito with Apache License 2.0 5 votes vote down vote up
void retainForms(LocaleId localeId, List<String> pluralForms) {
    if (localeId != null && !LocaleId.EMPTY.equals(localeId)) {
        ULocale ulocale = ULocale.forLanguageTag(localeId.toBCP47());
        PluralRules pluralRules = PluralRules.forLocale(ulocale);
        pluralForms.retainAll(pluralRules.getKeywords());
    }
}
 
Example #6
Source File: PoPluralRuleTest.java    From mojito with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyFormSize() {
    for (String bcp47tag : PoPluralRule.mappingForNonDefault.keySet()) {
        PluralRules cldrPluralRule = PluralRules.forLocale(Locale.forLanguageTag(bcp47tag));
         int cldrSize = cldrPluralRule.getKeywords().size();
         int poSize = PoPluralRule.fromBcp47Tag(bcp47tag).getCldrForms().size();
         int copySize = PoPluralRule.fromBcp47Tag(bcp47tag).getFormsToCopyOnImport().getFormMap().values().size();
        logger.debug("{} --> cldr size: {}, po size: {}, copy size: {}", 
                bcp47tag, cldrSize, poSize, copySize);
        Assert.assertEquals(copySize, cldrSize - poSize);
    }
}
 
Example #7
Source File: PluralRulesObject.java    From es6draft with MIT License 5 votes vote down vote up
/**
 * Returns the ICU {@link PluralRules} instance.
 * 
 * @return the PluralRules instance
 */
public PluralRules getPluralRules() {
    if (pluralRules == null) {
        pluralRules = createPluralRules();
    }
    return pluralRules;
}
 
Example #8
Source File: PluralRulesLoader.java    From fitnotifications with Apache License 2.0 4 votes vote down vote up
/**
 * Access through singleton.
 */
private PluralRulesLoader() {
    rulesIdToRules = new HashMap<String, PluralRules>();
}
 
Example #9
Source File: DefaultLanguagePluralRulesImpl.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public DefaultLanguagePluralRulesImpl(HumanLanguage language) {
	this.language = language;
	this.cardinal = PluralRules.forLocale(language.getLocale(), PluralType.CARDINAL);
	this.ordinal = PluralRules.forLocale(language.getLocale(), PluralType.ORDINAL);
}
 
Example #10
Source File: DefaultLanguagePluralRulesImpl.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private PluralRules getPluralRules(NumberType numberType) {
	return numberType == NumberType.ORDINAL ? this.ordinal : this.cardinal;
}
 
Example #11
Source File: RepositoryStatisticService.java    From mojito with Apache License 2.0 4 votes vote down vote up
private Long computeDiffToSourceLocaleCount(String targetLocaleBcp47Tag) {
    ULocale targetLocale = ULocale.forLanguageTag(targetLocaleBcp47Tag);
    PluralRules pluralRulesTargetLocale = PluralRules.forLocale(targetLocale);
    return 6L - pluralRulesTargetLocale.getKeywords().size();
}
 
Example #12
Source File: PluralFormForLocaleService.java    From mojito with Apache License 2.0 4 votes vote down vote up
public List<PluralFormForLocale> getPluralFormsForLocales() {
    
    List<PluralFormForLocale> pluralFormForLocales = new ArrayList<>();
    
    List<Locale> locales = localeRepository.findAll();
    
    for (Locale locale : locales) {
        
        ULocale forLanguageTag = ULocale.forLanguageTag(locale.getBcp47Tag());
        
        PluralRules pluralRules = PluralRules.forLocale(forLanguageTag);
        for (String keyword : pluralRules.getKeywords()) {
            logger.debug("{} : {} : {}", locale.getId(), locale.getBcp47Tag(), keyword);
            
            PluralFormForLocale pluralFormForLocale = new PluralFormForLocale();
            pluralFormForLocale.setPluralForm(pluralFormService.findByPluralFormString(keyword));
            pluralFormForLocale.setLocale(locale);
            
            pluralFormForLocales.add(pluralFormForLocale);
        }
    }
    
    return pluralFormForLocales;
}
 
Example #13
Source File: PluralRulesObject.java    From es6draft with MIT License 4 votes vote down vote up
private PluralRules createPluralRules() {
    ULocale locale = ULocale.forLanguageTag(this.locale);
    PluralType pluralType = "cardinal".equals(type) ? PluralType.CARDINAL : PluralType.ORDINAL;
    return PluralRules.forLocale(locale, pluralType);
}
 
Example #14
Source File: LanguageData.java    From es6draft with MIT License 2 votes vote down vote up
/**
 * Returns the set of available locales supported by the {@link PluralRules} class.
 * 
 * @return the set of available locales
 * @see PluralRules#getAvailableULocales()
 */
static Set<String> getAvailablePluralRulesLocales() {
    return addDerivedLanguages(toLanguageTags(PluralRules.getAvailableULocales()));
}