@localazy/strapi-plugin 0.1.6 → 0.1.7
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/admin/src/modules/@common/utils/__tests__/iso-locales-utils.test.js +102 -0
- package/admin/src/modules/@common/utils/iso-locales-utils.js +45 -2
- package/package.json +1 -1
- package/server/config/development/index.js +2 -2
- package/server/controllers/localazy-transfer-controller.js +3 -1
- package/server/services/strapi-i18n-service.js +3 -3
- package/server/utils/__tests__/iso-locales-utils.test.js +104 -0
- package/server/utils/intl-display-name.js +2 -5
- package/server/utils/iso-locales-utils.js +40 -2
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const { isoStrapiToLocalazy, isoLocalazyToStrapi } = require("../iso-locales-utils");
|
|
2
|
+
|
|
3
|
+
describe("iso-locales-utils.js", () => {
|
|
4
|
+
describe("transform Strapi ISO to Localazy ISO", () => {
|
|
5
|
+
it("should transform codes containing lang only", () => {
|
|
6
|
+
// setup
|
|
7
|
+
const input = ["en", "fr", "de", "es", "it", "ja", "ko", "pt", "ru", "zh"];
|
|
8
|
+
|
|
9
|
+
// execute
|
|
10
|
+
const output = input.map((item) => isoStrapiToLocalazy(item));
|
|
11
|
+
|
|
12
|
+
// verify
|
|
13
|
+
const expected = input;
|
|
14
|
+
expect(output).toEqual(expected);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("should transform codes containing lang-region", () => {
|
|
18
|
+
// setup
|
|
19
|
+
const input = ["en-US", "fr-CA", "de-DE", "es-ES", "it-IT", "ja-JP", "ko-KR", "pt-BR", "ru-RU", "zh-CN"];
|
|
20
|
+
|
|
21
|
+
// execute
|
|
22
|
+
const output = input.map((item) => isoStrapiToLocalazy(item));
|
|
23
|
+
|
|
24
|
+
// verify
|
|
25
|
+
const expected = ["en_US", "fr_CA", "de_DE", "es_ES", "it_IT", "ja_JP", "ko_KR", "pt_BR", "ru_RU", "zh_CN"];
|
|
26
|
+
expect(output).toEqual(expected);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should transform codes containing lang-script", () => {
|
|
30
|
+
// setup
|
|
31
|
+
const input = ["en-Latn", "fr-Latn", "de-Latn", "es-Latn", "it-Latn", "ja-Latn", "ko-Latn", "pt-Latn", "ru-Latn", "zh-Latn"];
|
|
32
|
+
|
|
33
|
+
// execute
|
|
34
|
+
const output = input.map((item) => isoStrapiToLocalazy(item));
|
|
35
|
+
|
|
36
|
+
// verify
|
|
37
|
+
const expected = ["en#Latn", "fr#Latn", "de#Latn", "es#Latn", "it#Latn", "ja#Latn", "ko#Latn", "pt#Latn", "ru#Latn", "zh#Latn"];
|
|
38
|
+
expect(output).toEqual(expected);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should transform codes containing lang-script-region", () => {
|
|
42
|
+
// setup
|
|
43
|
+
const input = ["en-Latn-US", "fr-Latn-CA", "de-Latn-DE", "es-Latn-ES", "it-Latn-IT", "ja-Latn-JP", "ko-Latn-KR", "pt-Latn-BR", "ru-Latn-RU", "zh-Hant-CN"];
|
|
44
|
+
|
|
45
|
+
// execute
|
|
46
|
+
const output = input.map((item) => isoStrapiToLocalazy(item));
|
|
47
|
+
|
|
48
|
+
// verify
|
|
49
|
+
const expected = ["en_US#Latn", "fr_CA#Latn", "de_DE#Latn", "es_ES#Latn", "it_IT#Latn", "ja_JP#Latn", "ko_KR#Latn", "pt_BR#Latn", "ru_RU#Latn", "zh_CN#Hant"];
|
|
50
|
+
expect(output).toEqual(expected);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("transform Strapi ISO to Localazy ISO", () => {
|
|
55
|
+
it("should transform codes containing lang only", () => {
|
|
56
|
+
// setup
|
|
57
|
+
const input = ["en", "fr", "de", "es", "it", "ja", "ko", "pt", "ru", "zh"];
|
|
58
|
+
|
|
59
|
+
// execute
|
|
60
|
+
const output = input.map((item) => isoLocalazyToStrapi(item));
|
|
61
|
+
|
|
62
|
+
// verify
|
|
63
|
+
const expected = input;
|
|
64
|
+
expect(output).toEqual(expected);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("should transform codes containing lang-region", () => {
|
|
68
|
+
// setup
|
|
69
|
+
const input = ["en_US", "fr_CA", "de_DE", "es_ES", "it_IT", "ja_JP", "ko_KR", "pt_BR", "ru_RU", "zh_CN"];
|
|
70
|
+
|
|
71
|
+
// execute
|
|
72
|
+
const output = input.map((item) => isoLocalazyToStrapi(item));
|
|
73
|
+
|
|
74
|
+
// verify
|
|
75
|
+
const expected = ["en-US", "fr-CA", "de-DE", "es-ES", "it-IT", "ja-JP", "ko-KR", "pt-BR", "ru-RU", "zh-CN"];
|
|
76
|
+
expect(output).toEqual(expected);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should transform codes containing lang-script", () => {
|
|
80
|
+
// setup
|
|
81
|
+
const input = ["en#Latn", "fr#Latn", "de#Latn", "es#Latn", "it#Latn", "ja#Latn", "ko#Latn", "pt#Latn", "ru#Latn", "zh#Latn"];
|
|
82
|
+
|
|
83
|
+
// execute
|
|
84
|
+
const output = input.map((item) => isoLocalazyToStrapi(item));
|
|
85
|
+
|
|
86
|
+
// verify
|
|
87
|
+
const expected = ["en-Latn", "fr-Latn", "de-Latn", "es-Latn", "it-Latn", "ja-Latn", "ko-Latn", "pt-Latn", "ru-Latn", "zh-Latn"];
|
|
88
|
+
expect(output).toEqual(expected);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should transform codes containing lang-script-region", () => {
|
|
92
|
+
// setup
|
|
93
|
+
const input = ["en_US#Latn", "fr_CA#Latn", "de_DE#Latn", "es_ES#Latn", "it_IT#Latn", "ja_JP#Latn", "ko_KR#Latn", "pt_BR#Latn", "ru_RU#Latn", "zh_CN#Hant"];
|
|
94
|
+
|
|
95
|
+
// execute
|
|
96
|
+
const output = input.map((item) => isoLocalazyToStrapi(item));
|
|
97
|
+
// verify
|
|
98
|
+
const expected = ["en-Latn-US", "fr-Latn-CA", "de-Latn-DE", "es-Latn-ES", "it-Latn-IT", "ja-Latn-JP", "ko-Latn-KR", "pt-Latn-BR", "ru-Latn-RU", "zh-Hant-CN"];
|
|
99
|
+
expect(output).toEqual(expected);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -3,7 +3,31 @@ export const isoStrapiToLocalazy = (isoStrapi) => {
|
|
|
3
3
|
return null;
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
const split = isoStrapi.split("-");
|
|
7
|
+
const language = split[0];
|
|
8
|
+
|
|
9
|
+
const hasRegionAndScript = split.length === 3;
|
|
10
|
+
|
|
11
|
+
if (hasRegionAndScript) {
|
|
12
|
+
const script = split[1];
|
|
13
|
+
const region = split[2];
|
|
14
|
+
|
|
15
|
+
return `${language}_${region}#${script}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const hasPossiblyRegion = split.length === 2;
|
|
19
|
+
|
|
20
|
+
if (hasPossiblyRegion) {
|
|
21
|
+
const regionOrScript = split[1];
|
|
22
|
+
|
|
23
|
+
// is it script - the way Strapi resolves that
|
|
24
|
+
const isItRegion = regionOrScript.toUpperCase() === regionOrScript;
|
|
25
|
+
|
|
26
|
+
// it is region; otherwise it is script
|
|
27
|
+
return isItRegion ? `${language}_${regionOrScript}` : `${language}#${regionOrScript}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return isoStrapi.replace("-", "_");
|
|
7
31
|
};
|
|
8
32
|
|
|
9
33
|
export const isoLocalazyToStrapi = (isoLocalazy) => {
|
|
@@ -11,5 +35,24 @@ export const isoLocalazyToStrapi = (isoLocalazy) => {
|
|
|
11
35
|
return null;
|
|
12
36
|
}
|
|
13
37
|
|
|
14
|
-
|
|
38
|
+
const scriptIndex = isoLocalazy.indexOf("#");
|
|
39
|
+
const hasScript = scriptIndex > -1;
|
|
40
|
+
|
|
41
|
+
if (hasScript) {
|
|
42
|
+
const script = isoLocalazy.substring(scriptIndex + 1);
|
|
43
|
+
const isoLocalazyWithoutScript = isoLocalazy.substring(0, scriptIndex);
|
|
44
|
+
const split = isoLocalazyWithoutScript.split("_");
|
|
45
|
+
const language = split[0];
|
|
46
|
+
const region = split[1];
|
|
47
|
+
|
|
48
|
+
if (!region) {
|
|
49
|
+
return `${language}-${script}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const isoStrapi = `${language}-${script}-${region.toUpperCase()}`;
|
|
53
|
+
|
|
54
|
+
return isoStrapi;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return isoLocalazy.replace("_", "-");
|
|
15
58
|
};
|
package/package.json
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module.exports = {
|
|
4
4
|
LOCALAZY_OAUTH_URL: "https://testing.localazy.com/oauth/authorize",
|
|
5
|
-
LOCALAZY_OAUTH_APP_CLIENT_ID: "
|
|
6
|
-
LOCALAZY_PLUGIN_CONNECTOR_API_URL: "
|
|
5
|
+
LOCALAZY_OAUTH_APP_CLIENT_ID: "18503917571895066466",
|
|
6
|
+
LOCALAZY_PLUGIN_CONNECTOR_API_URL: "https://testing.localazy.com/plugin-connector",
|
|
7
7
|
LOCALAZY_PUBLIC_API_URL: "https://testing.localazy.com/pubapi",
|
|
8
8
|
LOCALAZY_PLUGIN_ID: "1",
|
|
9
9
|
LOCALAZY_DEFAULT_FILE_NAME: "strapi.json",
|
|
@@ -25,7 +25,9 @@ module.exports = {
|
|
|
25
25
|
const messageReport = [];
|
|
26
26
|
|
|
27
27
|
// Strapi Service
|
|
28
|
-
const StrapiService = strapi
|
|
28
|
+
const StrapiService = strapi
|
|
29
|
+
.plugin("localazy")
|
|
30
|
+
.service("strapiService");
|
|
29
31
|
|
|
30
32
|
// Strapi i18n Service
|
|
31
33
|
const StrapiI18nService = strapi
|
|
@@ -13,17 +13,18 @@ module.exports = ({ strapi }) => ({
|
|
|
13
13
|
async createStrapiLocale(ctx, isoLocalazy) {
|
|
14
14
|
try {
|
|
15
15
|
const isoStrapi = isoLocalazyToStrapi(isoLocalazy);
|
|
16
|
-
|
|
16
|
+
let localeName = intlDisplayName(isoStrapi);
|
|
17
17
|
|
|
18
18
|
if (!localeName || !isoStrapi) {
|
|
19
19
|
throw new Error("Invalid locale");
|
|
20
20
|
}
|
|
21
|
+
localeName = `${localeName} (${isoStrapi})`;
|
|
21
22
|
|
|
22
23
|
const newLocaleCtx = { ...ctx };
|
|
23
24
|
newLocaleCtx.request.body = {
|
|
24
25
|
isDefault: false,
|
|
25
26
|
code: isoStrapi,
|
|
26
|
-
name: localeName,
|
|
27
|
+
name: localeName.substring(0, 50), // limit name to 50 characters (given by Strapi)
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
await strapi
|
|
@@ -39,7 +40,6 @@ module.exports = ({ strapi }) => ({
|
|
|
39
40
|
parseLocalazyKey(key) {
|
|
40
41
|
const split = key.split(/\.|\[|\]/);
|
|
41
42
|
const filteredSplit = split.filter((item) => item !== "");
|
|
42
|
-
// return split;
|
|
43
43
|
|
|
44
44
|
return {
|
|
45
45
|
uid: `${filteredSplit[0]}.${filteredSplit[1]}`,
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { isoStrapiToLocalazy, isoLocalazyToStrapi } = require("../iso-locales-utils.js");
|
|
4
|
+
|
|
5
|
+
describe("iso-locales-utils.js", () => {
|
|
6
|
+
describe("transform Strapi ISO to Localazy ISO", () => {
|
|
7
|
+
it("should transform codes containing lang only", () => {
|
|
8
|
+
// setup
|
|
9
|
+
const input = ["en", "fr", "de", "es", "it", "ja", "ko", "pt", "ru", "zh"];
|
|
10
|
+
|
|
11
|
+
// execute
|
|
12
|
+
const output = input.map((item) => isoStrapiToLocalazy(item));
|
|
13
|
+
|
|
14
|
+
// verify
|
|
15
|
+
const expected = input;
|
|
16
|
+
expect(output).toEqual(expected);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should transform codes containing lang-region", () => {
|
|
20
|
+
// setup
|
|
21
|
+
const input = ["en-US", "fr-CA", "de-DE", "es-ES", "it-IT", "ja-JP", "ko-KR", "pt-BR", "ru-RU", "zh-CN"];
|
|
22
|
+
|
|
23
|
+
// execute
|
|
24
|
+
const output = input.map((item) => isoStrapiToLocalazy(item));
|
|
25
|
+
|
|
26
|
+
// verify
|
|
27
|
+
const expected = ["en_US", "fr_CA", "de_DE", "es_ES", "it_IT", "ja_JP", "ko_KR", "pt_BR", "ru_RU", "zh_CN"];
|
|
28
|
+
expect(output).toEqual(expected);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should transform codes containing lang-script", () => {
|
|
32
|
+
// setup
|
|
33
|
+
const input = ["en-Latn", "fr-Latn", "de-Latn", "es-Latn", "it-Latn", "ja-Latn", "ko-Latn", "pt-Latn", "ru-Latn", "zh-Latn"];
|
|
34
|
+
|
|
35
|
+
// execute
|
|
36
|
+
const output = input.map((item) => isoStrapiToLocalazy(item));
|
|
37
|
+
|
|
38
|
+
// verify
|
|
39
|
+
const expected = ["en#Latn", "fr#Latn", "de#Latn", "es#Latn", "it#Latn", "ja#Latn", "ko#Latn", "pt#Latn", "ru#Latn", "zh#Latn"];
|
|
40
|
+
expect(output).toEqual(expected);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should transform codes containing lang-script-region", () => {
|
|
44
|
+
// setup
|
|
45
|
+
const input = ["en-Latn-US", "fr-Latn-CA", "de-Latn-DE", "es-Latn-ES", "it-Latn-IT", "ja-Latn-JP", "ko-Latn-KR", "pt-Latn-BR", "ru-Latn-RU", "zh-Hant-CN"];
|
|
46
|
+
|
|
47
|
+
// execute
|
|
48
|
+
const output = input.map((item) => isoStrapiToLocalazy(item));
|
|
49
|
+
|
|
50
|
+
// verify
|
|
51
|
+
const expected = ["en_US#Latn", "fr_CA#Latn", "de_DE#Latn", "es_ES#Latn", "it_IT#Latn", "ja_JP#Latn", "ko_KR#Latn", "pt_BR#Latn", "ru_RU#Latn", "zh_CN#Hant"];
|
|
52
|
+
expect(output).toEqual(expected);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("transform Strapi ISO to Localazy ISO", () => {
|
|
57
|
+
it("should transform codes containing lang only", () => {
|
|
58
|
+
// setup
|
|
59
|
+
const input = ["en", "fr", "de", "es", "it", "ja", "ko", "pt", "ru", "zh"];
|
|
60
|
+
|
|
61
|
+
// execute
|
|
62
|
+
const output = input.map((item) => isoLocalazyToStrapi(item));
|
|
63
|
+
|
|
64
|
+
// verify
|
|
65
|
+
const expected = input;
|
|
66
|
+
expect(output).toEqual(expected);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should transform codes containing lang-region", () => {
|
|
70
|
+
// setup
|
|
71
|
+
const input = ["en_US", "fr_CA", "de_DE", "es_ES", "it_IT", "ja_JP", "ko_KR", "pt_BR", "ru_RU", "zh_CN"];
|
|
72
|
+
|
|
73
|
+
// execute
|
|
74
|
+
const output = input.map((item) => isoLocalazyToStrapi(item));
|
|
75
|
+
|
|
76
|
+
// verify
|
|
77
|
+
const expected = ["en-US", "fr-CA", "de-DE", "es-ES", "it-IT", "ja-JP", "ko-KR", "pt-BR", "ru-RU", "zh-CN"];
|
|
78
|
+
expect(output).toEqual(expected);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should transform codes containing lang-script", () => {
|
|
82
|
+
// setup
|
|
83
|
+
const input = ["en#Latn", "fr#Latn", "de#Latn", "es#Latn", "it#Latn", "ja#Latn", "ko#Latn", "pt#Latn", "ru#Latn", "zh#Latn"];
|
|
84
|
+
|
|
85
|
+
// execute
|
|
86
|
+
const output = input.map((item) => isoLocalazyToStrapi(item));
|
|
87
|
+
|
|
88
|
+
// verify
|
|
89
|
+
const expected = ["en-Latn", "fr-Latn", "de-Latn", "es-Latn", "it-Latn", "ja-Latn", "ko-Latn", "pt-Latn", "ru-Latn", "zh-Latn"];
|
|
90
|
+
expect(output).toEqual(expected);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("should transform codes containing lang-script-region", () => {
|
|
94
|
+
// setup
|
|
95
|
+
const input = ["en_US#Latn", "fr_CA#Latn", "de_DE#Latn", "es_ES#Latn", "it_IT#Latn", "ja_JP#Latn", "ko_KR#Latn", "pt_BR#Latn", "ru_RU#Latn", "zh_CN#Hant"];
|
|
96
|
+
|
|
97
|
+
// execute
|
|
98
|
+
const output = input.map((item) => isoLocalazyToStrapi(item));
|
|
99
|
+
// verify
|
|
100
|
+
const expected = ["en-Latn-US", "fr-Latn-CA", "de-Latn-DE", "es-Latn-ES", "it-Latn-IT", "ja-Latn-JP", "ko-Latn-KR", "pt-Latn-BR", "ru-Latn-RU", "zh-Hant-CN"];
|
|
101
|
+
expect(output).toEqual(expected);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
module.exports = (isoCode) => {
|
|
4
|
-
const isoDashed = isoStrapiToLocalazy(isoCode);
|
|
1
|
+
module.exports = (isoStrapi) => {
|
|
5
2
|
let languageNames = new Intl.DisplayNames(["en"], { type: "language" });
|
|
6
|
-
return languageNames.of(
|
|
3
|
+
return languageNames.of(isoStrapi);
|
|
7
4
|
};
|
|
@@ -5,7 +5,28 @@ const isoStrapiToLocalazy = (isoStrapi) => {
|
|
|
5
5
|
return null;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
const split = isoStrapi.split("-");
|
|
9
|
+
const language = split[0];
|
|
10
|
+
|
|
11
|
+
const hasRegionAndScript = split.length === 3;
|
|
12
|
+
if (hasRegionAndScript) {
|
|
13
|
+
const script = split[1];
|
|
14
|
+
const region = split[2];
|
|
15
|
+
return `${language}_${region}#${script}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const hasPossiblyRegion = split.length === 2;
|
|
19
|
+
if (hasPossiblyRegion) {
|
|
20
|
+
const regionOrScript = split[1];
|
|
21
|
+
|
|
22
|
+
// is it script - the way Strapi resolves that
|
|
23
|
+
const isItRegion = regionOrScript.toUpperCase() === regionOrScript;
|
|
24
|
+
|
|
25
|
+
// it is region; otherwise it is script
|
|
26
|
+
return isItRegion ? `${language}_${regionOrScript}` : `${language}#${regionOrScript}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return isoStrapi.replace("-", "_");
|
|
9
30
|
};
|
|
10
31
|
|
|
11
32
|
const isoLocalazyToStrapi = (isoLocalazy) => {
|
|
@@ -13,7 +34,24 @@ const isoLocalazyToStrapi = (isoLocalazy) => {
|
|
|
13
34
|
return null;
|
|
14
35
|
}
|
|
15
36
|
|
|
16
|
-
|
|
37
|
+
const scriptIndex = isoLocalazy.indexOf("#");
|
|
38
|
+
const hasScript = scriptIndex > -1;
|
|
39
|
+
if (hasScript) {
|
|
40
|
+
const script = isoLocalazy.substring(scriptIndex + 1);
|
|
41
|
+
const isoLocalazyWithoutScript = isoLocalazy.substring(0, scriptIndex);
|
|
42
|
+
const split = isoLocalazyWithoutScript.split("_");
|
|
43
|
+
const language = split[0];
|
|
44
|
+
const region = split[1];
|
|
45
|
+
|
|
46
|
+
if (!region) {
|
|
47
|
+
return `${language}-${script}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const isoStrapi = `${language}-${script}-${region.toUpperCase()}`;
|
|
51
|
+
return isoStrapi;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return isoLocalazy.replace("_", "-");
|
|
17
55
|
};
|
|
18
56
|
|
|
19
57
|
module.exports = {
|