(message: string)
| 11 | } |
| 12 | |
| 13 | function parseMessageParams(message: string) { |
| 14 | const params: LocalizableParam[] = []; |
| 15 | const seenParams = new Set<string>(); |
| 16 | |
| 17 | const matches = message.matchAll(MESSAGE_PARAMS_REGEX); |
| 18 | for (const match of matches) { |
| 19 | if (match.length == 0) { |
| 20 | continue; |
| 21 | } |
| 22 | const placeholder = match[1]; |
| 23 | const name = match[2]; |
| 24 | |
| 25 | // Handles the same key being used more than once in the format |
| 26 | if (seenParams.has(name)) { |
| 27 | continue; |
| 28 | } |
| 29 | seenParams.add(name); |
| 30 | |
| 31 | const format = match[3]; |
| 32 | let type: LocalizableParamType; |
| 33 | if (!format || format === '' || format === '%s') { |
| 34 | type = LocalizableParamType.STRING; |
| 35 | } else if (format === '%d') { |
| 36 | type = LocalizableParamType.INTEGER; |
| 37 | } else { |
| 38 | throw new Error(`Unsupported format type: ${format}`); |
| 39 | } |
| 40 | const param: LocalizableParam = { |
| 41 | placeholder, |
| 42 | name, |
| 43 | type, |
| 44 | }; |
| 45 | params.push(param); |
| 46 | } |
| 47 | return params; |
| 48 | } |
| 49 | |
| 50 | export function messageToFormat(baseEnglishMessage: string, localizedMessage: string) { |
| 51 | const baseEnglishParams = parseMessageParams(baseEnglishMessage); |
no test coverage detected