Skip to content

Commit 1fc69f2

Browse files
committed
feat: allow array as replacements
1 parent 74248d4 commit 1fc69f2

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { OptionsInterface } from './interfaces/options'
33
import { PluginOptionsInterface } from './interfaces/plugin-options'
44
import { LanguageInterface } from './interfaces/language'
55
import { LanguageJsonFileInterface } from './interfaces/language-json-file'
6-
import { ReplacementsInterface } from './interfaces/replacements'
6+
import { Replacements } from './interfaces/replacements'
77
import { choose } from './pluralization'
88
import { avoidExceptionOnPromise, avoidException } from './utils/avoid-exceptions'
99
import { hasPhpTranslations } from './utils/has-php-translations'
@@ -50,32 +50,28 @@ export function loadLanguageAsync(lang: string, dashLangTry = false): Promise<st
5050
/**
5151
* Get the translation for the given key.
5252
*/
53-
export function trans(key: string, replacements: ReplacementsInterface = {}): string {
53+
export function trans(key: string, replacements: Replacements = {}): string {
5454
return I18n.getSharedInstance().trans(key, replacements)
5555
}
5656

5757
/**
5858
* Get the translation for the given key and watch for any changes.
5959
*/
60-
export function wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
60+
export function wTrans(key: string, replacements: Replacements = {}): ComputedRef<string> {
6161
return I18n.getSharedInstance().wTrans(key, replacements)
6262
}
6363

6464
/**
6565
* Translates the given message based on a count.
6666
*/
67-
export function transChoice(key: string, number: number, replacements: ReplacementsInterface = {}): string {
67+
export function transChoice(key: string, number: number, replacements: Replacements = {}): string {
6868
return I18n.getSharedInstance().transChoice(key, number, replacements)
6969
}
7070

7171
/**
7272
* Translates the given message based on a count and watch for changes.
7373
*/
74-
export function wTransChoice(
75-
key: string,
76-
number: number,
77-
replacements: ReplacementsInterface = {}
78-
): ComputedRef<string> {
74+
export function wTransChoice(key: string, number: number, replacements: Replacements = {}): ComputedRef<string> {
7975
return I18n.getSharedInstance().wTransChoice(key, number, replacements)
8076
}
8177

@@ -117,8 +113,8 @@ export const i18nVue: Plugin = {
117113

118114
const i18n = options.shared ? I18n.getSharedInstance(options, true) : new I18n(options)
119115

120-
app.config.globalProperties.$t = (key: string, replacements: ReplacementsInterface) => i18n.trans(key, replacements)
121-
app.config.globalProperties.$tChoice = (key: string, number: number, replacements: ReplacementsInterface) =>
116+
app.config.globalProperties.$t = (key: string, replacements: Replacements) => i18n.trans(key, replacements)
117+
app.config.globalProperties.$tChoice = (key: string, number: number, replacements: Replacements) =>
122118
i18n.transChoice(key, number, replacements)
123119

124120
app.provide('i18n', i18n)
@@ -398,14 +394,14 @@ export class I18n {
398394
/**
399395
* Get the translation for the given key.
400396
*/
401-
trans(key: string, replacements: ReplacementsInterface = {}): string {
397+
trans(key: string, replacements: Replacements = {}): string {
402398
return this.wTrans(key, replacements).value
403399
}
404400

405401
/**
406402
* Get the translation for the given key and watch for any changes.
407403
*/
408-
wTrans(key: string, replacements: ReplacementsInterface = {}): ComputedRef<string> {
404+
wTrans(key: string, replacements: Replacements = {}): ComputedRef<string> {
409405
watchEffect(() => {
410406
let value = this.findTranslation(key)
411407

@@ -422,18 +418,16 @@ export class I18n {
422418
/**
423419
* Translates the given message based on a count.
424420
*/
425-
transChoice(key: string, number: number, replacements: ReplacementsInterface = {}): string {
421+
transChoice(key: string, number: number, replacements: Replacements = {}): string {
426422
return this.wTransChoice(key, number, replacements).value
427423
}
428424

429425
/**
430426
* Translates the given message based on a count and watch for changes.
431427
*/
432-
wTransChoice(key: string, number: number, replacements: ReplacementsInterface = {}): ComputedRef<string> {
428+
wTransChoice(key: string, number: number, replacements: Replacements = {}): ComputedRef<string> {
433429
const message = this.wTrans(key, replacements)
434430

435-
replacements.count = number.toString()
436-
437431
return computed(() => this.makeReplacements(choose(message.value, number, this.options.lang), replacements))
438432
}
439433

@@ -461,7 +455,7 @@ export class I18n {
461455
/**
462456
* Make the place-holder replacements on a line.
463457
*/
464-
makeReplacements(message?: string, replacements?: ReplacementsInterface): string {
458+
makeReplacements(message?: string, replacements?: Replacements): string {
465459
const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1)
466460

467461
Object.entries(replacements || [])

src/interfaces/replacements.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export interface ReplacementsInterface {
22
[key: string]: string
33
}
4+
5+
export type Replacements = ReplacementsInterface | (string | number)[]

0 commit comments

Comments
 (0)