-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
Copy pathtranslations.ts
266 lines (246 loc) · 7.45 KB
/
translations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import _ from 'lodash';
import {mergeTranslations} from '@docusaurus/utils';
import {CURRENT_VERSION_NAME} from './constants';
import {
collectSidebarCategories,
transformSidebarItems,
collectSidebarLinks,
collectSidebarDocItems,
collectSidebarRefs,
} from './sidebars/utils';
import type {
LoadedVersion,
LoadedContent,
} from '@docusaurus/plugin-content-docs';
import type {
Sidebar,
SidebarItemCategory,
SidebarItemCategoryLink,
Sidebars,
} from './sidebars/types';
import type {
TranslationFileContent,
TranslationFile,
TranslationMessage,
} from '@docusaurus/types';
function getVersionFileName(versionName: string): string {
if (versionName === CURRENT_VERSION_NAME) {
return versionName;
}
// I don't like this "version-" prefix,
// but it's for consistency with site/versioned_docs
return `version-${versionName}`;
}
function getSidebarTranslationFileContent(
sidebar: Sidebar,
sidebarName: string,
): TranslationFileContent {
type TranslationMessageEntry = [string, TranslationMessage];
const categories = collectSidebarCategories(sidebar);
const categoryContent: TranslationFileContent = Object.fromEntries(
categories.flatMap((category) => {
const entries: TranslationMessageEntry[] = [];
entries.push([
`sidebar.${sidebarName}.category.${category.label}`,
{
message: category.label,
description: `The label for category ${category.label} in sidebar ${sidebarName}`,
},
]);
if (category.link?.type === 'generated-index') {
if (category.link.title) {
entries.push([
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.title`,
{
message: category.link.title,
description: `The generated-index page title for category ${category.label} in sidebar ${sidebarName}`,
},
]);
}
if (category.link.description) {
entries.push([
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.description`,
{
message: category.link.description,
description: `The generated-index page description for category ${category.label} in sidebar ${sidebarName}`,
},
]);
}
}
return entries;
}),
);
const links = collectSidebarLinks(sidebar);
const linksContent: TranslationFileContent = Object.fromEntries(
links.map((link) => [
`sidebar.${sidebarName}.link.${link.label}`,
{
message: link.label,
description: `The label for link ${link.label} in sidebar ${sidebarName}, linking to ${link.href}`,
},
]),
);
const docs = collectSidebarDocItems(sidebar)
.concat(collectSidebarRefs(sidebar))
.filter((item) => item.translatable);
const docLinksContent: TranslationFileContent = Object.fromEntries(
docs.map((doc) => [
`sidebar.${sidebarName}.doc.${doc.label!}`,
{
message: doc.label!,
description: `The label for the doc item ${doc.label!} in sidebar ${sidebarName}, linking to the doc ${
doc.id
}`,
},
]),
);
return mergeTranslations([categoryContent, linksContent, docLinksContent]);
}
function translateSidebar({
sidebar,
sidebarName,
sidebarsTranslations,
}: {
sidebar: Sidebar;
sidebarName: string;
sidebarsTranslations: TranslationFileContent;
}): Sidebar {
function transformSidebarCategoryLink(
category: SidebarItemCategory,
): SidebarItemCategoryLink | undefined {
if (!category.link) {
return undefined;
}
if (category.link.type === 'generated-index') {
const title =
sidebarsTranslations[
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.title`
]?.message ?? category.link.title;
const description =
sidebarsTranslations[
`sidebar.${sidebarName}.category.${category.label}.link.generated-index.description`
]?.message ?? category.link.description;
return {
...category.link,
title,
description,
};
}
return category.link;
}
return transformSidebarItems(sidebar, (item) => {
if (item.type === 'category') {
const link = transformSidebarCategoryLink(item);
return {
...item,
label:
sidebarsTranslations[`sidebar.${sidebarName}.category.${item.label}`]
?.message ?? item.label,
...(link && {link}),
};
}
if (item.type === 'link') {
return {
...item,
label:
sidebarsTranslations[`sidebar.${sidebarName}.link.${item.label}`]
?.message ?? item.label,
};
}
if ((item.type === 'doc' || item.type === 'ref') && item.translatable) {
return {
...item,
label:
sidebarsTranslations[`sidebar.${sidebarName}.doc.${item.label!}`]
?.message ?? item.label,
};
}
return item;
});
}
function getSidebarsTranslations(
version: LoadedVersion,
): TranslationFileContent {
return mergeTranslations(
Object.entries(version.sidebars).map(([sidebarName, sidebar]) =>
getSidebarTranslationFileContent(sidebar, sidebarName),
),
);
}
function translateSidebars(
version: LoadedVersion,
sidebarsTranslations: TranslationFileContent,
): Sidebars {
return _.mapValues(version.sidebars, (sidebar, sidebarName) =>
translateSidebar({
sidebar,
sidebarName,
sidebarsTranslations,
}),
);
}
function getVersionTranslationFiles(version: LoadedVersion): TranslationFile[] {
const versionTranslations: TranslationFileContent = {
'version.label': {
message: version.label,
description: `The label for version ${version.versionName}`,
},
};
const sidebarsTranslations: TranslationFileContent =
getSidebarsTranslations(version);
return [
{
path: getVersionFileName(version.versionName),
content: mergeTranslations([versionTranslations, sidebarsTranslations]),
},
];
}
function translateVersion(
version: LoadedVersion,
translationFiles: {[fileName: string]: TranslationFile},
): LoadedVersion {
const versionTranslations =
translationFiles[getVersionFileName(version.versionName)]!.content;
return {
...version,
label: versionTranslations['version.label']?.message ?? version.label,
sidebars: translateSidebars(version, versionTranslations),
};
}
function getVersionsTranslationFiles(
versions: LoadedVersion[],
): TranslationFile[] {
return versions.flatMap(getVersionTranslationFiles);
}
function translateVersions(
versions: LoadedVersion[],
translationFiles: {[fileName: string]: TranslationFile},
): LoadedVersion[] {
return versions.map((version) => translateVersion(version, translationFiles));
}
export function getLoadedContentTranslationFiles(
loadedContent: LoadedContent,
): TranslationFile[] {
return getVersionsTranslationFiles(loadedContent.loadedVersions);
}
export function translateLoadedContent(
loadedContent: LoadedContent,
translationFiles: TranslationFile[],
): LoadedContent {
const translationFilesMap: {[fileName: string]: TranslationFile} = _.keyBy(
translationFiles,
(f) => f.path,
);
return {
loadedVersions: translateVersions(
loadedContent.loadedVersions,
translationFilesMap,
),
};
}