Skip to content

Commit 961c049

Browse files
committed
feat: add transChoice and mixin $tChoice(), as aliases.
1 parent 43ab5e1 commit 961c049

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function trans(key: string, replacements: ReplacementsInterface = {}): st
6363
/**
6464
* Translates the given message based on a count.
6565
*/
66-
export function trans_choice(key: string, number: number, replacements: ReplacementsInterface = {}): string {
66+
export function transChoice(key: string, number: number, replacements: ReplacementsInterface = {}): string {
6767
const message = trans(key, replacements)
6868

6969
replacements.count = number.toString()
@@ -118,13 +118,20 @@ export const reset = (): void => {
118118
}
119119
}
120120

121+
122+
/**
123+
* Alias to `transChoice` to mimic the same function name from Laravel Framework.
124+
*/
125+
export const trans_choice = transChoice;
126+
121127
/**
122128
* The Vue Plugin. to be used on your Vue app like this: `app.use(i18nVue)`
123129
*/
124130
export const i18nVue: Plugin = {
125131
install: (app, currentOptions: OptionsInterface = {}) => {
126132
options = { ...options, ...currentOptions }
127133
app.config.globalProperties.$t = (key: string, replacements: ReplacementsInterface) => trans(key, replacements)
134+
app.config.globalProperties.$tChoice = (key: string, number: number, replacements: ReplacementsInterface) => transChoice(key, number, replacements)
128135
loadLanguageAsync(options.lang)
129136
}
130137
}

test/fixtures/lang/pt.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"Welcome!": "Bem-vindo!",
3-
"Welcome, :name!": "Bem-vindo, :name!"
3+
"Welcome, :name!": "Bem-vindo, :name!",
4+
"{1} :count minute ago|[2,*] :count minutes ago": "{1} há :count minuto|[2,*] há :count minutos"
45
}

test/plurization.test.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { trans_choice } from '../src';
1+
import { trans_choice, transChoice } from '../src';
22

33
it.each([
44
['first', 'first', 1],
@@ -41,10 +41,10 @@ it.each([
4141

4242
['first', '{0} first | { 1 } second', 0],
4343
['first', '[4,*]first | [1,3]second', 100],
44-
])('translates plurization with "trans_choice" helper', async (expected, message, number) => {
44+
])('translates plurization with "transChoice" helper', async (expected, message, number) => {
4545
const wrapper = await global.mountPlugin()
4646

47-
expect(trans_choice(message, number)).toBe(expected)
47+
expect(transChoice(message, number)).toBe(expected)
4848
})
4949

5050
it.each([
@@ -56,3 +56,18 @@ it.each([
5656

5757
expect(trans_choice(message, number, replacements)).toBe(expected)
5858
})
59+
60+
it('translates even using the mixin "$tChoice()', async () => {
61+
const wrapper = await global.mountPlugin(
62+
`<h1>{{ $tChoice('{1} :count minute ago|[2,*] :count minutes ago', 3)}}</h1>`
63+
)
64+
65+
expect(wrapper.html()).toBe('<h1>há 3 minutos</h1>');
66+
})
67+
68+
it('translates even using an alias "trans_choice"', async () => {
69+
await global.mountPlugin()
70+
71+
expect(trans_choice('{1} :count minute ago|[2,*] :count minutes ago', 3))
72+
.toBe('há 3 minutos');
73+
})

0 commit comments

Comments
 (0)