@docusaurus/theme-translations 2.0.0-beta.13 → 2.0.0-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +13 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +2 -3
- package/lib/index.js.map +1 -0
- package/package.json +4 -6
- package/src/index.ts +59 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
export declare function codeTranslationLocalesToTry(locale: string): string[];
|
|
8
|
+
export declare function readDefaultCodeTranslationMessages({ dirPath, locale, name, }: {
|
|
9
|
+
dirPath?: string;
|
|
10
|
+
locale: string;
|
|
11
|
+
name: string;
|
|
12
|
+
}): Promise<Record<string, string>>;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAUH,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAgBpE;AAGD,wBAAsB,kCAAkC,CAAC,EACvD,OAAoC,EACpC,MAAM,EACN,IAAI,GACL,EAAE;IACD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAgBlC"}
|
package/lib/index.js
CHANGED
|
@@ -11,9 +11,7 @@ const tslib_1 = require("tslib");
|
|
|
11
11
|
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
12
12
|
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
13
13
|
function getDefaultLocalesDirPath() {
|
|
14
|
-
|
|
15
|
-
const pkg = JSON.parse(fs_extra_1.default.readFileSync(path_1.default.join(parentDirPath, 'package.json'), 'utf8'));
|
|
16
|
-
return path_1.default.join(parentDirPath, pkg.files[0]);
|
|
14
|
+
return path_1.default.join(__dirname, '../locales');
|
|
17
15
|
}
|
|
18
16
|
// Return an ordered list of locales we should try
|
|
19
17
|
function codeTranslationLocalesToTry(locale) {
|
|
@@ -50,3 +48,4 @@ async function readDefaultCodeTranslationMessages({ dirPath = getDefaultLocalesD
|
|
|
50
48
|
return {};
|
|
51
49
|
}
|
|
52
50
|
exports.readDefaultCodeTranslationMessages = readDefaultCodeTranslationMessages;
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,6DAAwB;AACxB,qEAA0B;AAE1B,SAAS,wBAAwB;IAC/B,OAAO,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;AAC5C,CAAC;AAED,kDAAkD;AAClD,SAAgB,2BAA2B,CAAC,MAAc;IACxD,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACrE,IAAI,CAAC,UAAU,EAAE;QACf,OAAO,CAAC,MAAM,CAAC,CAAC;KACjB;IACD,2FAA2F;IAC3F,8EAA8E;IAC9E,IAAI,UAAU,CAAC,QAAQ,KAAK,MAAM,EAAE;QAClC,MAAM,eAAe,GAAG,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAC,cAAc;QAC7D,iBAAiB;QACjB,OAAO,CAAC,MAAM,EAAE,GAAG,eAAe,CAAC,QAAQ,IAAI,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC;KAC1E;IACD,yDAAyD;SACpD;QACH,OAAO,CAAC,MAAM,EAAE,UAAU,CAAC,QAAS,CAAC,CAAC;KACvC;AACH,CAAC;AAhBD,kEAgBC;AAED,oEAAoE;AAC7D,KAAK,UAAU,kCAAkC,CAAC,EACvD,OAAO,GAAG,wBAAwB,EAAE,EACpC,MAAM,EACN,IAAI,GAKL;IACC,MAAM,YAAY,GAAG,2BAA2B,CAAC,MAAM,CAAC,CAAC;IAEzD,kDAAkD;IAClD,mCAAmC;IACnC,gDAAgD;IAChD,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE;QACtC,MAAM,QAAQ,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,EAAE,WAAW,EAAE,GAAG,IAAI,OAAO,CAAC,CAAC;QAEpE,IAAI,MAAM,kBAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;YACjC,MAAM,WAAW,GAAG,MAAM,kBAAE,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;YACxD,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;SAChC;KACF;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AAxBD,gFAwBC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/theme-translations",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.14",
|
|
4
4
|
"description": "Docusaurus theme translations.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
|
-
"files": [
|
|
8
|
-
"locales"
|
|
9
|
-
],
|
|
10
7
|
"publishConfig": {
|
|
11
8
|
"access": "public"
|
|
12
9
|
},
|
|
@@ -26,11 +23,12 @@
|
|
|
26
23
|
"tslib": "^2.3.1"
|
|
27
24
|
},
|
|
28
25
|
"devDependencies": {
|
|
29
|
-
"
|
|
26
|
+
"@docusaurus/core": "2.0.0-beta.14",
|
|
27
|
+
"@docusaurus/logger": "2.0.0-beta.14",
|
|
30
28
|
"lodash": "^4.17.20"
|
|
31
29
|
},
|
|
32
30
|
"engines": {
|
|
33
31
|
"node": ">=14"
|
|
34
32
|
},
|
|
35
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "c4824a8937d8f1aa0806667749cbc74058e2b294"
|
|
36
34
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import path from 'path';
|
|
9
|
+
import fs from 'fs-extra';
|
|
10
|
+
|
|
11
|
+
function getDefaultLocalesDirPath(): string {
|
|
12
|
+
return path.join(__dirname, '../locales');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Return an ordered list of locales we should try
|
|
16
|
+
export function codeTranslationLocalesToTry(locale: string): string[] {
|
|
17
|
+
const intlLocale = Intl.Locale ? new Intl.Locale(locale) : undefined;
|
|
18
|
+
if (!intlLocale) {
|
|
19
|
+
return [locale];
|
|
20
|
+
}
|
|
21
|
+
// if locale is just a simple language like "pt", we want to fallback to pt-BR (not pt-PT!)
|
|
22
|
+
// see https://github.com/facebook/docusaurus/pull/4536#issuecomment-810088783
|
|
23
|
+
if (intlLocale.language === locale) {
|
|
24
|
+
const maximizedLocale = intlLocale.maximize(); // pt-Latn-BR`
|
|
25
|
+
// ["pt","pt-BR"]
|
|
26
|
+
return [locale, `${maximizedLocale.language}-${maximizedLocale.region}`];
|
|
27
|
+
}
|
|
28
|
+
// if locale is like "pt-BR", we want to fallback to "pt"
|
|
29
|
+
else {
|
|
30
|
+
return [locale, intlLocale.language!];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Useful to implement getDefaultCodeTranslationMessages() in themes
|
|
35
|
+
export async function readDefaultCodeTranslationMessages({
|
|
36
|
+
dirPath = getDefaultLocalesDirPath(),
|
|
37
|
+
locale,
|
|
38
|
+
name,
|
|
39
|
+
}: {
|
|
40
|
+
dirPath?: string;
|
|
41
|
+
locale: string;
|
|
42
|
+
name: string;
|
|
43
|
+
}): Promise<Record<string, string>> {
|
|
44
|
+
const localesToTry = codeTranslationLocalesToTry(locale);
|
|
45
|
+
|
|
46
|
+
// Return the content of the first file that match
|
|
47
|
+
// fr_FR.json => fr.json => nothing
|
|
48
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
49
|
+
for (const localeToTry of localesToTry) {
|
|
50
|
+
const filePath = path.resolve(dirPath, localeToTry, `${name}.json`);
|
|
51
|
+
|
|
52
|
+
if (await fs.pathExists(filePath)) {
|
|
53
|
+
const fileContent = await fs.readFile(filePath, 'utf8');
|
|
54
|
+
return JSON.parse(fileContent);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {};
|
|
59
|
+
}
|