`
vcycyv
  • 浏览: 3930 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Writing plural and singular words using ChoiceForm

 
阅读更多

转贴自http://blogs.dekoh.com/dev/2007/10/25/writing-plural-and-singular-words-using-choiceformat-in-messageformat/

 

In Dekoh Photos application I wanted to internationalize String that represents the number of comments made on a Photo.

So when there are no comments made i wanted to display “No Comments”, when there is 1 comment i wanted to display “1 Comment” and if there are more than one comment i wanted to display “n Comments”.

Using the following neat trick that employs choice format all these possibilities can be addressed in one message key.

    private static final MessageFormat comments = new MessageFormat(
            "{0, choice, 0#No Comments|1#{0} comment|2#{0} comments}");

    public static void main(String[] args)
    {
        String zero = comments.format(new Object[]{0});
        System.out.println("zero= " + zero);
        String one = comments.format(new Object[]{1});
        System.out.println("one = " + one);
        String two = comments.format(new Object[]{2});
        System.out.println("two = " + two);
        String three = comments.format(new Object[]{3});
        System.out.println("three = " + three);
    }

Will print :

zero= No Comments
one = 1 comment
two = 2 comments
three = 3 comments

So the trick likes in this line

"{0, choice, 0#No Comments|1#{0} comment|2#{0} comments}");

{0} represents the number of comments, which will be supplied at runtime as a value. If this value is anything more than 1 {0} Comments will be returned.

Cool, eh ?

分享到:
评论

相关推荐

    I2Localization2.8.6_f2

    Built-in rules for each language covering those that just need singular/plural forms up to those that use 6 variants. Right to Left Languages Correct rendering of RTL languages with support for line ...

    I2 Localization2.6.6b1

    Built-in rules for each language covering those that just need singular/plural forms up to those that use 6 variants. Right to Left Languages Correct rendering of RTL languages with support for line ...

    inflect:正确生成复数,序数,不定冠词; 将数字转换为单词

    # plural plural_noun plural_verb plural_adj singular_noun no num # compare compare_nouns compare_nouns compare_adjs # a an # present_participle # ordinal number_to_words # join # inflect classical ...

    Just Enough English Grammar Illustrated

    Singular and Plural Gender Nouns as Subjects Nouns as Objects etc. 3. Pronouns 4. Adjectives 5. Prepositions 6. Verbs 7. Adverbs 8. Conjunctions 9. etc. 10. Review Test Biographical note Gabrielle ...

    Inflector:英语名词复数和单数的变形符

    反射器 使英语名词复数和单数的变形词。 .../* Singular to plural / Plural to singular */ echo Inflector :: pluralize ( 'search' ); // outputs searches echo Inflector :: singularize ( 'c

    Singular-开源

    Singular是一款强大的开源计算机代数系统,专门设计用于处理多项式计算,特别是在交换代数、代数几何和奇点理论领域。它提供了丰富的数学工具,帮助研究者和学者进行复杂的数学建模和分析。 在交换代数方面,...

    Android代码-confetti

    Confetti (plural) vs confetto (singular) Confetti is a high-performance, easily-configurable particle system library that can animate any set of objects through space. You can specify your starting ...

    plural4j:Java中的复数单词形式

    plural4j-是一个Java实用程序,用于生成多个单词形式。 [ ]( ) 玛文 < groupId>com.github.plural4j</ groupId> < artifactId>plural4j < version>1.0.0 建造 mvn -DskipTests=true clean package install ...

    make-plural:将Unicode CLDR复数规则转换为可执行JavaScript

    plural-预编译的Unicode CLDR复数规则作为JavaScript函数 发展 git clone https://github.com/eemeli/make-plural.git cd make-plural npm install npm run build 请注意,在v5版本中,编译器和CLI从规范的make-...

    TreeTagger2

    DT Article and determiner EX Existential there FW Foreign word IN Preposition or subordinating conjunction JJ Adjective JJR Comparative adjective JJS Superlative adjective LS List item marker MD Modal...

    SGA_GA_matlab_plural5dd_

    标题“SGA_GA_matlab_plural5dd_”暗示了一个基于MATLAB的遗传算法(Genetic Algorithm, 简称GA)实现,用于解决多目标优化问题。描述中提到的“简单小程序”,意味着这是一个基础的遗传算法应用,可能用于教学或...

    inflection:复数和单数化英文名词

    Plural ( "person" ) = > "people"inflection . Plural ( "Person" ) = > "People"inflection . Plural ( "PERSON" ) = > "PEOPLE"inflection . Plural ( "bus" ) = > "buses"inflection . Plural ( "BUS" ) = > ...

    Plural-a-writing-utility-

    正确的,而不是愚蠢地使用 dom 作为模型,只需制作一个模型。 基本单位是复数——一组文本的替代版本。 所以基本上你只有一个你正在处理的文本,你可以对特定的块进行版本控制,将它们推送到一个数组中。...

    ADT.rar_ADT Plural_复数_复数 ADT

    在这个实例中,"ADT.rar_ADT Plural_复数_复数 ADT" 指的是一个关于复数的ADT实现,它涉及到复数的表示、输出和基本的数学运算。 复数是由实部和虚部组成的数学对象,通常表示为 \(a + bi\) 形式,其中 \(a\) 是...

    GMAT Mini-Test Questions and Answers

    ### GMAT Mini-Test: Sentence Correction Questions and Answers #### Introduction The GMAT (Graduate Management Admission Test) is a standardized test used by many business schools to assess the ...

    plural-rotate:简单元素旋转库

    **plural-rotate: 简单元素旋转库** 在Web开发中,动态视觉效果是吸引用户注意力和提升用户体验的重要手段之一。"plural-rotate"是一个专门用于实现元素旋转的JavaScript库,它为开发者提供了一个简单易用的接口,...

    C语言编写短路电流计算的程序.doc

    1. 函数 PluralMul(Plural plural1, Plural plural2):用于计算两个复数的乘积。 2. 函数 PluralDiv(float num, Plural den):用于计算浮点数除以复数的结果。 3. 函数 PluralDiv(Plural plu):用于计算复数的倒数。...

    verilog_IEEE官方标准手册-2005_IEEE_P1364.rar_plural6m5_verilog IEEE规范_

    5. **结构描述**:Verilog能够描述电路的物理结构,包括门级表示,如NOT、AND、OR、NAND、NOR等逻辑门,以及更复杂的组件如触发器、寄存器和计数器。 6. **时序控制**:时钟边沿检测、同步和异步复位、时钟域跨越等...

Global site tag (gtag.js) - Google Analytics