Skip to content

Commit 4b29fb0

Browse files
heiskrCopilot
andauthored
🌎 Apply translation corrections to variable YAML values (#60720)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5dc5ad8 commit 4b29fb0

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

β€Žsrc/data-directory/lib/get-data.tsβ€Ž

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,25 @@ function getDataByDir(
187187
if (allData && key) {
188188
const value = allData[key]
189189
if (value) {
190-
return matter(value).content
190+
let content = matter(value).content
191+
if (dir !== englishRoot) {
192+
let englishContent = content
193+
try {
194+
const englishData = getYamlContent(englishRoot, fullPath.join(path.sep), englishRoot)
195+
if (englishData?.[key]) {
196+
englishContent = matter(englishData[key]).content
197+
}
198+
} catch (error) {
199+
if ((error as FileSystemError).code !== 'ENOENT') {
200+
throw error
201+
}
202+
}
203+
content = correctTranslatedContentStrings(content, englishContent, {
204+
dottedPath,
205+
code: langCode,
206+
})
207+
}
208+
return content
191209
}
192210
} else {
193211
console.warn(`Unable to find variables Yaml file ${fullPath.join(path.sep)}`)

β€Žsrc/data-directory/tests/get-data.tsβ€Ž

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,74 @@ describe('get-data on corrupt translations', () => {
296296
}
297297
})
298298
})
299+
300+
describe('get-data applies corrections to translated variables', () => {
301+
let dd: DataDirectory
302+
const enDirBefore = languages.en.dir
303+
languages.ja = Object.assign({}, languages.en, {})
304+
305+
beforeAll(() => {
306+
dd = new DataDirectory({
307+
data: {
308+
variables: {
309+
myproduct: {
310+
name: 'GitHub',
311+
},
312+
phases: {
313+
preview: '{% ifversion ghes < 3.16 %}beta{% else %}public preview{% endif %}',
314+
},
315+
},
316+
},
317+
})
318+
languages.en.dir = dd.root
319+
320+
const jaTranslationsRoot = path.join(dd.root, 'translations', 'ja-JP')
321+
fs.mkdirSync(jaTranslationsRoot, { recursive: true })
322+
languages.ja.dir = jaTranslationsRoot
323+
new DataDirectory(
324+
{
325+
data: {
326+
variables: {
327+
myproduct: {
328+
// Corrupted: `data` translated to Japanese `データ`
329+
name: '{% データ variables.myproduct.name %}',
330+
},
331+
phases: {
332+
// Not corrupted β€” should pass through unchanged
333+
preview: '{% ifversion ghes < 3.16 %}ベータ{% else %}パブγƒͺックプレビγƒ₯γƒΌ{% endif %}',
334+
},
335+
},
336+
},
337+
},
338+
jaTranslationsRoot,
339+
)
340+
})
341+
342+
afterAll(() => {
343+
dd.destroy()
344+
languages.en.dir = enDirBefore
345+
})
346+
347+
test('corrects corrupted Liquid keywords in translated variables', () => {
348+
// English variable is returned as-is
349+
{
350+
const result = getDataByLanguage('variables.myproduct.name', 'en')
351+
expect(result).toBe('GitHub')
352+
}
353+
// Japanese translation with corrupted `データ` β†’ `data` gets corrected
354+
{
355+
const result = getDataByLanguage('variables.myproduct.name', 'ja')
356+
expect(result).toBe('{% data variables.myproduct.name %}')
357+
}
358+
})
359+
360+
test('leaves valid translated variables unchanged', () => {
361+
// Valid ifversion in translated variable should pass through
362+
{
363+
const result = getDataByLanguage('variables.phases.preview', 'ja')
364+
expect(result).toBe(
365+
'{% ifversion ghes < 3.16 %}ベータ{% else %}パブγƒͺックプレビγƒ₯γƒΌ{% endif %}',
366+
)
367+
}
368+
})
369+
})

0 commit comments

Comments
Β (0)