Skip to content

Commit 81173e0

Browse files
committed
Add pluralization questions
1 parent f1afa22 commit 81173e0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ List of 300 VueJS Interview Questions
215215
|206| [What is Vue I18n plugin?](#what-is-vue-i-8n--plugin)|
216216
|207| [#What are the types of formatting?](#what-are-the-types-of-formatting)|
217217
|208| [What is custom formatting?](#what-is-custom-formatting)|
218+
|209| [How do you handle Pluralization?](#how-do-you-handle-pluralization)|
218219

219220
1. ### What is VueJS?
220221
**Vue.js** is an open-source, progressive Javascript framework for building user interfaces that aim to be incrementally adoptable. The core library of VueJS is focused on the `view layer` only, and is easy to pick up and integrate with other libraries or existing projects.
@@ -3692,3 +3693,34 @@ List of 300 VueJS Interview Questions
36923693
// Run!
36933694
new Vue({ i18n }).$mount('#app')
36943695
```
3696+
209. ### How do you handle Pluralization?
3697+
You can translate with pluralization by defining the locale that have a pipe | separator, and define plurals in pipe separator. Remember that template should use $tc() instead of $t().
3698+
First you need to difine the messages,
3699+
```javascript
3700+
const messages = {
3701+
en: {
3702+
user: 'user | users',
3703+
friend: 'no friend | one friend | {count} friends'
3704+
}
3705+
}
3706+
```
3707+
And the template can configure the messages with values
3708+
```javascript
3709+
<p>{{ $tc('user', 1) }}</p>
3710+
<p>{{ $tc('user', 10) }}</p>
3711+
3712+
<p>{{ $tc('friend', 0) }}</p>
3713+
<p>{{ $tc('friend', 1) }}</p>
3714+
<p>{{ $tc('friend', 10, { count: 10 }) }}</p>
3715+
```
3716+
Finally it outputs the result as below
3717+
```javascript
3718+
<p>user</p>
3719+
<p>users</p>
3720+
3721+
<p>no friend</p>
3722+
<p>one friend</p>
3723+
<p>10 friends</p>
3724+
```
3725+
3726+

0 commit comments

Comments
 (0)