Lusito/webextension-polyfill-ts

Require browser.i18n.getMessage substitutions parameter to be string

arty-name opened this issue · 2 comments

When providing substitutions as an array, there’s no real difference whether the values are strings or not: they are cast into strings. However there’s also an option to provide a single substitution value as is, not as an array with a single element. Here the type of the value becomes essential: Chrome will not substitute non-string values.

// message.json
// { "test": {"message": "Test $value$", "placeholders": {"value": {"content": "$1"} } } }

// Firefox 
await browser.i18n.getMessage('test', ['1']) // 'Test 1'
await browser.i18n.getMessage('test', [1]) // 'Test 1'
await browser.i18n.getMessage('test', '1') // 'Test 1'
await browser.i18n.getMessage('test', 1) // 'Test 1'

// Chrome
await chrome.i18n.getMessage('test', ['1']) // 'Test 1'
await chrome.i18n.getMessage('test', [1]) // 'Test 1'
await chrome.i18n.getMessage('test', '1') // 'Test 1'
await chrome.i18n.getMessage('test', 1) // 'Test '

Would it be possible to provide a type like substitutions: any[] | string?

Hmm, MDN says that getMessage accepts string or string array. Chrome docs say that it's up to 9 strings, so I'm guessing substitutions: string[] | string should be correct.

Would you like to contribute a fix for this?

Oh, cool, I didn’t realize the schemas coming from Mozilla can be overridden here.