Java Code Examples for com.ibm.icu.text.PluralRules#forLocale()

The following examples show how to use com.ibm.icu.text.PluralRules#forLocale() . 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: 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 3
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 4
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 5
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 6
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 7
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);
}