Skip to content

Commit 269910c

Browse files
authored
Translate alert titles (#55559)
1 parent 0b1ca5c commit 269910c

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

src/content-render/tests/data.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ describe('data tag', () => {
1717
foo: 'Foo',
1818
},
1919
},
20+
ui: {
21+
alerts: {},
22+
},
2023
},
2124
})
2225
languages.en.dir = dd.root

src/content-render/unified/alerts.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { h } from 'hastscript'
77
import octicons from '@primer/octicons'
88

99
const alertTypes = {
10-
NOTE: { icon: 'info', color: 'accent', title: 'Note' },
11-
IMPORTANT: { icon: 'report', color: 'done', title: 'Important' },
12-
WARNING: { icon: 'alert', color: 'attention', title: 'Warning' },
13-
TIP: { icon: 'light-bulb', color: 'success', title: 'Tip' },
14-
CAUTION: { icon: 'stop', color: 'danger', title: 'Caution' },
10+
NOTE: { icon: 'info', color: 'accent' },
11+
IMPORTANT: { icon: 'report', color: 'done' },
12+
WARNING: { icon: 'alert', color: 'attention' },
13+
TIP: { icon: 'light-bulb', color: 'success' },
14+
CAUTION: { icon: 'stop', color: 'danger' },
1515
}
1616

1717
// Must contain one of [!NOTE], [!IMPORTANT], ...
@@ -22,7 +22,7 @@ const matcher = (node) =>
2222
node.tagName === 'blockquote' &&
2323
ALERT_REGEXP.test(JSON.stringify(node.children))
2424

25-
export default function alerts() {
25+
export default function alerts({ alertTitles = {} }) {
2626
return (tree) => {
2727
visit(tree, matcher, (node) => {
2828
const key = getAlertKey(node)
@@ -35,7 +35,12 @@ export default function alerts() {
3535
node.tagName = 'div'
3636
node.properties.className = 'ghd-alert ghd-alert-' + alertType.color
3737
node.children = [
38-
h('p', { className: 'ghd-alert-title' }, getOcticonSVG(alertType.icon), alertType.title),
38+
h(
39+
'p',
40+
{ className: 'ghd-alert-title' },
41+
getOcticonSVG(alertType.icon),
42+
alertTitles[key] || '',
43+
),
3944
...removeAlertSyntax(node.children),
4045
]
4146
})

src/content-render/unified/processor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function createProcessor(context) {
7373
.use(rewriteForRowheaders)
7474
.use(rewriteImgSources)
7575
.use(rewriteAssetImgTags)
76-
.use(alerts)
76+
.use(alerts, context)
7777
// HTML AST above ^^^
7878
.use(html)
7979
// String below vvv

src/frame/lib/page.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import cheerio from 'cheerio'
44
import getApplicableVersions from '#src/versions/lib/get-applicable-versions.js'
55
import generateRedirectsForPermalinks from '#src/redirects/lib/permalinks.js'
66
import getEnglishHeadings from '#src/languages/lib/get-english-headings.js'
7+
import { getAlertTitles } from '#src/languages/lib/get-alert-titles.ts'
78
import getTocItems from './get-toc-items.js'
89
import Permalink from './permalink.js'
910
import { renderContent } from '#src/content-render/index.js'
@@ -200,6 +201,9 @@ class Page {
200201
context.englishHeadings = englishHeadings
201202
}
202203

204+
// pull translations for alerts
205+
context.alertTitles = await getAlertTitles(this)
206+
203207
this.intro = await renderContentWithFallback(this, 'rawIntro', context)
204208
this.introPlainText = await renderContentWithFallback(this, 'rawIntro', context, {
205209
textOnly: true,

src/languages/lib/get-alert-titles.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fs from 'fs/promises'
2+
import path from 'path'
3+
import yaml from 'js-yaml'
4+
import languages from './languages'
5+
6+
const cache: Record<string, any> = {}
7+
8+
export async function getAlertTitles(page: Record<string, any>) {
9+
const { languageCode } = page
10+
if (cache[languageCode]) return cache[languageCode]
11+
12+
let file = ''
13+
let yamlFile: Record<string, any> = {}
14+
if (languageCode !== 'en') {
15+
try {
16+
const { dir } = languages[languageCode]
17+
file = await fs.readFile(path.join(dir, `data/ui.yml`), 'utf-8')
18+
yamlFile = yaml.load(file) as Record<string, any>
19+
} catch (e) {
20+
console.warn(`Failed to load translated alert titles`, e)
21+
}
22+
}
23+
if (!file || !yamlFile.alerts) {
24+
const { dir } = languages.en
25+
file = await fs.readFile(path.join(dir, `data/ui.yml`), 'utf-8')
26+
yamlFile = yaml.load(file) as Record<string, any>
27+
}
28+
29+
cache[languageCode] = yamlFile.alerts
30+
return cache[languageCode]
31+
}

0 commit comments

Comments
 (0)