Skip to content

Commit 1cc2694

Browse files
authored
Merge pull request #854 from nextcloud-libraries/feat/overload-translate
feat: Overload translate function to either allow number or placeholder as third arg
2 parents a24c8b4 + fbc3885 commit 1cc2694

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

lib/translation.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,49 @@ type ExtractedVariables<T extends string> =
3939

4040
type TranslationVariables<K extends string> = Record<ExtractedVariables<K>, string | number | TranslationVariableReplacementObject<string | number>>
4141

42+
export function translate<T extends string>(app: string, text: T, placeholders?: TranslationVariables<T>, options?: TranslationOptions): string
43+
export function translate<T extends string>(app: string, text: T, number?: number, options?: TranslationOptions): string
44+
/**
45+
* @inheritdoc
46+
* @deprecated This overload is deprecated either use placeholders or a number but not both
47+
*/
48+
export function translate<T extends string>(app: string, text: T, placeholders?: TranslationVariables<T>, number?: number, options?: TranslationOptions): string
49+
4250
/**
4351
* Translate a string
4452
*
45-
* @param {string} app the id of the app for which to translate the string
46-
* @param {string} text the string to translate
47-
* @param {object} vars map of placeholder key to value
48-
* @param {number} number to replace %n with
49-
* @param {object} [options] options object
50-
* @param {boolean} options.escape enable/disable auto escape of placeholders (by default enabled)
51-
* @param {boolean} options.sanitize enable/disable sanitization (by default enabled)
52-
*
53-
* @return {string}
53+
* @param app the id of the app for which to translate the string
54+
* @param text the string to translate
55+
* @param placeholdersOrNumber map of placeholder key to value or a number replacing `%n`
56+
* @param optionsOrNumber the translation options or a number to replace `%n` with
57+
* @param options options object
58+
* @param options.escape enable/disable auto escape of placeholders (by default enabled)
59+
* @param options.sanitize enable/disable sanitization (by default enabled)
5460
*/
5561
export function translate<T extends string>(
5662
app: string,
5763
text: T,
58-
vars?: TranslationVariables<T>,
59-
number?: number,
64+
placeholdersOrNumber?: TranslationVariables<T>|number,
65+
optionsOrNumber?: number|TranslationOptions,
6066
options?: TranslationOptions,
6167
): string {
68+
const vars = typeof placeholdersOrNumber === 'object' ? placeholdersOrNumber : undefined
69+
const number = typeof optionsOrNumber === 'number' ? optionsOrNumber : (typeof placeholdersOrNumber === 'number' ? placeholdersOrNumber : undefined)
70+
6271
const allOptions = {
6372
// defaults
6473
escape: true,
6574
sanitize: true,
6675
// overwrite with user config
67-
...(options || {}),
76+
...(
77+
typeof options === 'object'
78+
? options
79+
: (
80+
typeof optionsOrNumber === 'object'
81+
? optionsOrNumber
82+
: {}
83+
)
84+
),
6885
}
6986

7087
const identity = <T, >(value: T): T => value

tests/translation.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ describe('translate', () => {
114114
expect(translation).toBe('Hallo <img src="x">')
115115
})
116116

117+
it('with number as third parameter', () => {
118+
const text = 'Number: %n'
119+
const translation = translate('core', text, 4)
120+
expect(translation).toBe('Number: 4')
121+
})
122+
123+
it('with options as forth parameter', () => {
124+
const text = 'Hello {name}'
125+
const translation = translate('core', text, { name: '<img src=x onerror=alert(1)//>' }, { escape: false })
126+
expect(translation).toBe('Hallo <img src="x">')
127+
})
128+
117129
it('singular', () => {
118130
const text = 'Hello world!'
119131
const translation = translate('core', text)

0 commit comments

Comments
 (0)