( translations: Record<string, ParsedTranslation>, messageParts: TemplateStringsArray, substitutions: readonly any[], )
| 49 | * error is thrown. |
| 50 | */ |
| 51 | export function translate( |
| 52 | translations: Record<string, ParsedTranslation>, |
| 53 | messageParts: TemplateStringsArray, |
| 54 | substitutions: readonly any[], |
| 55 | ): [TemplateStringsArray, readonly any[]] { |
| 56 | const message = parseMessage(messageParts, substitutions); |
| 57 | // Look up the translation using the messageId, and then the legacyId if available. |
| 58 | let translation = translations[message.id]; |
| 59 | // If the messageId did not match a translation, try matching the legacy ids instead |
| 60 | if (message.legacyIds !== undefined) { |
| 61 | for (let i = 0; i < message.legacyIds.length && translation === undefined; i++) { |
| 62 | translation = translations[message.legacyIds[i]]; |
| 63 | } |
| 64 | } |
| 65 | if (translation === undefined) { |
| 66 | throw new MissingTranslationError(message); |
| 67 | } |
| 68 | return [ |
| 69 | translation.messageParts, |
| 70 | translation.placeholderNames.map((placeholder) => { |
| 71 | if (message.substitutions.hasOwnProperty(placeholder)) { |
| 72 | return message.substitutions[placeholder]; |
| 73 | } else { |
| 74 | throw new Error( |
| 75 | `There is a placeholder name mismatch with the translation provided for the message ${describeMessage( |
| 76 | message, |
| 77 | )}.\n` + |
| 78 | `The translation contains a placeholder with name ${placeholder}, which does not exist in the message.`, |
| 79 | ); |
| 80 | } |
| 81 | }), |
| 82 | ]; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Parse the `messageParts` and `placeholderNames` out of a target `message`. |
no test coverage detected
searching dependent graphs…