( source: JsonObject, target: JsonObject, lang: string )
| 142 | } |
| 143 | |
| 144 | async function addMissingKeysFromSource( |
| 145 | source: JsonObject, |
| 146 | target: JsonObject, |
| 147 | lang: string |
| 148 | ): Promise<JsonObject> { |
| 149 | const merged: JsonObject = {}; |
| 150 | const keys = new Set([...Object.keys(source), ...Object.keys(target)]); |
| 151 | |
| 152 | for (const key of keys) { |
| 153 | if (key in source && !(key in target)) { |
| 154 | // Key exists in source but not in target |
| 155 | merged[key] = await translateJsonValue(source[key], lang); |
| 156 | } else if ( |
| 157 | key in source && |
| 158 | key in target && |
| 159 | typeof source[key] === "object" && |
| 160 | !Array.isArray(source[key]) && |
| 161 | typeof target[key] === "object" && |
| 162 | !Array.isArray(target[key]) |
| 163 | ) { |
| 164 | // Both have the key and its value is an object, merge recursively |
| 165 | merged[key] = await addMissingKeysFromSource( |
| 166 | source[key] as JsonObject, |
| 167 | target[key] as JsonObject, |
| 168 | lang |
| 169 | ); |
| 170 | } else { |
| 171 | // Key exists in target or both, prefer target's value |
| 172 | merged[key] = target[key]; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return merged; |
| 177 | } |
| 178 | |
| 179 | // Main function to load the source file and update each target file |
| 180 | async function updateLocalizationFiles() { |
no test coverage detected