(engine: VisualNovelEngine, statement: string)
| 80 | * @returns The translated string |
| 81 | */ |
| 82 | export function translate (engine: VisualNovelEngine, statement: string): string { |
| 83 | // Find all elements in the string that match the "_(key)" format |
| 84 | const matches = statement.match (/_\(\S+\)/g); |
| 85 | const language = engine.preference ('Language') as string; |
| 86 | |
| 87 | // Check if any matches were found, if not then no translation is needed |
| 88 | if (matches === null) { |
| 89 | return statement; |
| 90 | } |
| 91 | |
| 92 | // Go through all the found matches so we can get the string it maps to |
| 93 | for (const match of matches) { |
| 94 | // Remove the _() from the key |
| 95 | const path = match.replace ('_(', '').replace (')', '').split ('.'); |
| 96 | |
| 97 | // Retrieve the string from the translations using the given key |
| 98 | const translationsForLang = translations (engine, language) as Record<string, unknown> | undefined; |
| 99 | if (!translationsForLang) continue; |
| 100 | |
| 101 | let data: unknown = translationsForLang[path[0]]; |
| 102 | |
| 103 | for (let j = 1; j < path.length; j++) { |
| 104 | if (typeof data === 'object' && data !== null) { |
| 105 | data = (data as Record<string, unknown>)[path[j]]; |
| 106 | } |
| 107 | } |
| 108 | if (typeof data === 'string') { |
| 109 | statement = statement.replace (match, data); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return statement; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Recursively replace all occurrences of |
no test coverage detected