Skip to content

Commit 1576f84

Browse files
author
Peter Bengtsson
authored
Unbreak Japanese markdown-y links (#51104)
1 parent 1fb3887 commit 1576f84

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/fixtures/fixtures/translations/ja-jp/content/get-started/start-your-journey/hello-world.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ recover from it.
2323
## Use of a reusable that might have auto-title links
2424

2525
{% data reusables.gated-features.more-info %}
26+
27+
If you type in "bar" into Google Translate (English to Japanese)
28+
you get "バー". The translation guidelines says that when you're
29+
mentioning an English word, you should leave it and translate it
30+
using the same character composition used for regular Markdown
31+
links. Here's one such example:
32+
33+
[[Bar](バー)](/get-started/foo/bar)

src/fixtures/tests/translations.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,21 @@ describe('translations', () => {
103103
const stillAutotitle = texts.filter((text) => /autotitle/i.test(text))
104104
expect(stillAutotitle.length).toBe(0)
105105
})
106+
107+
test('markdown link looking constructs inside links', async () => {
108+
// On this page, the translators had written:
109+
//
110+
// [[Bar](バー)](/get-started/foo/bar)
111+
//
112+
// which needs to become:
113+
//
114+
// <a href="/ja/get-started/foo/bar">[Bar](バー)</a>
115+
const $ = await getDOM('/ja/get-started/start-your-journey/hello-world')
116+
const links = $('#article-contents a[href]')
117+
const texts = links
118+
.filter((i, element) => $(element).attr('href').includes('get-started/foo/bar'))
119+
.map((i, element) => $(element).text())
120+
.get()
121+
expect(texts.includes('[Bar] (バー)')).toBeTruthy()
122+
})
106123
})

src/languages/lib/correct-translation-content.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,38 @@ export function correctTranslatedContentStrings(content, englishContent, context
7777
'- % data variables.product.prodname_copilot_enterprise %}',
7878
'- {% data variables.product.prodname_copilot_enterprise %}',
7979
)
80+
81+
// This might not be exclusive to Japanese but put here because, at
82+
// the time of writing, it only happens on the Japanse translations.
83+
// According to the Microsoft translation guidelines, they're not
84+
// supposed to translate words that will be seen in the UI, but
85+
// instead mention then like this:
86+
//
87+
// [Save changes](THE TRANSLATION OF "Save changes" IN JAPANESE)
88+
//
89+
// The problem is when these are wrapped in a deliberate Markdown link.
90+
// For example:
91+
//
92+
// [[Save changes](THE TRANSLATION OF "Save changes" IN JAPANESE)](#some-section)
93+
//
94+
// A real observed example is:
95+
//
96+
// [[Allow deletions](削除を許可)](#allow-deletions)
97+
//
98+
// Here, because "削除を許可" contains no spaces, the Markdown parser
99+
// thinks "削除を許可" is the URL! But in actuality,
100+
// `[Allow deletions](削除を許可)` is the text and `#allow-deletions`
101+
// is the URL.
102+
// This problem does not exhibit if the text "削除を許可" were to contain
103+
// a space character. But we can't assume that we can just add a space.
104+
// For example "削除 を許可" would be incorrect. And where do you put the
105+
// space? Between which characters.
106+
// Instead, we can inject a "hair space" whitespace character between
107+
// the `]` and the `(`. Then, the Markdown processor does not get confused
108+
// and the link is rendered correctly.
109+
// The `\u200A` is the "hair space" character. Technically whitespace
110+
// but not wide enough to visually appear as a space.
111+
content = content.replace(/\[(\[.*?\])(\(\S+\)\]\()/g, '[$1\u200A$2')
80112
}
81113

82114
if (context.code === 'zh') {

0 commit comments

Comments
 (0)