Skip to content

Instantly share code, notes, and snippets.

@anuragl94
Last active June 26, 2018 13:14
Show Gist options
  • Save anuragl94/bcb4b6d89d92cda2ebf9126aeebfee69 to your computer and use it in GitHub Desktop.
Save anuragl94/bcb4b6d89d92cda2ebf9126aeebfee69 to your computer and use it in GitHub Desktop.
Examples of ICU MessageFormat strings

1. Simple string substitution

String

Hello, {USERNAME}!

Output

// For USERNAME = "Anurag"
Hello, Anurag

2. Pluralization

String

You have {TASKS_COUNT, plural,
  =0{no tasks}
  one{# task}
  other{# tasks}}

Output

// For TASKS_COUNT = 0
You have no tasks

// For TASKS_COUNT = 1
You have 1 task

// For TASKS_COUNT = 10
You have 10 tasks

3. Pluralization with offset

String

You {USER_COUNT, plural, offset:1
  =0{didn't like this}
  =1{liked this}
  =2{and another person liked this}
  other{and # others liked this}}

Output

// For USER_COUNT = 0
You didn't like this

// For USER_COUNT = 1
You liked this

// For USER_COUNT = 2
You and another person liked this

// For USER_COUNT = 3
You and 2 others liked this

4. Ordinals

String

Celebrate your {AGE, selectordinal,
  one{#st}
  two{#nd}
  few{#rd}
  other{#th}
} birthday

Output

// For AGE = 20
Celebrate your 20th birthday

// For AGE = 21
Celebrate your 21st birthday

// For AGE = 22
Celebrate your 22nd birthday

// For AGE = 23
Celebrate your 23rd birthday

5. Select from enumerated values

String

Let's congratulate {GENDER, select,
  male {him}
  female {her}
  other {them}}

Output

// For GENDER = male
Let's congratulate him

// For GENDER = female
Let's congratulate her

// For GENDER = <any other, or missing>
Let's congratulate them
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment