Skip to content

Commit

Permalink
[FEATURE] manifestCreator: i18n section v22
Browse files Browse the repository at this point in the history
Improve i18n language extraction code by avoiding regexp
  • Loading branch information
tobiasso85 committed Dec 22, 2020
1 parent 535668a commit 9f1b588
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/processors/manifestCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,17 @@ async function createManifest(libraryResource, libBundle, descriptorVersion, _in
if (libBundle.findResource(i18n) != null) {
supportedLocales.add("");
}
const i18nPatternStr = i18n.substring(0, i18n.length - ".properties".length);
const i18nRegexp = new RegExp(i18nPatternStr + "_([^.]+)\\.properties$");

const i18nPatternStr = i18n.substring(0, i18n.length - ".properties".length) + "_";

libBundle.getResources().forEach((resource) => {
const resPath = resource.getPath();
const result = i18nRegexp.exec(resPath);

if (result) {
supportedLocales.add(result[1].replace(/_/g, "-"));
const i18nLastIndex = resPath.lastIndexOf(i18nPatternStr);
if (resPath.endsWith(".properties") && i18nLastIndex >= 0) {
const i18nPath = resPath.substring(i18nLastIndex + i18nPatternStr.length,
resPath.length - ".properties".length);
if (!i18nPath.includes(".")) {
supportedLocales.add(i18nPath.replace(/_/g, "-"));
}
}
});
i18nToSupportedLocales.set(i18n, supportedLocales);
Expand Down
89 changes: 89 additions & 0 deletions test/lib/processors/manifestCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ const libraryContent = `<?xml version="1.0" encoding="UTF-8" ?>
</appData>
</library>`;

const libraryContentSpecialChars = `<?xml version="1.0" encoding="UTF-8" ?>
<library xmlns="http://www.sap.com/sap.ui.library.xsd" >
<name>library.e</name>
<vendor>SAP SE</vendor>
<copyright>my copyright</copyright>
<version>1.0.0</version>
<documentation>Library E</documentation>
<dependencies>
<dependency>
<libraryName>sap.ui.core</libraryName>
</dependency>
</dependencies>
<appData>
<manifest xmlns="http://www.sap.com/ui5/buildext/manifest">
<i18n>i18n(.*)./i18n(.*).properties</i18n>
</manifest>
</appData>
</library>`;

const expectedManifestContent = `{
"_version": "1.21.0",
"sap.app": {
Expand Down Expand Up @@ -61,6 +82,43 @@ const expectedManifestContent = `{
}
}
}`;
const expectedManifestContentSpecialChars = `{
"_version": "1.21.0",
"sap.app": {
"id": "library.e",
"type": "library",
"embeds": [],
"i18n": {
"bundleUrl": "i18n(.*)./i18n(.*).properties",
"supportedLocales": [
"",
"de",
"en"
]
},
"applicationVersion": {
"version": "1.0.0"
},
"title": "Library E",
"description": "Library E",
"resources": "resources.json",
"offline": true
},
"sap.ui": {
"technology": "UI5",
"supportedThemes": []
},
"sap.ui5": {
"dependencies": {
"libs": {
"sap.ui.core": {}
}
},
"library": {
"i18n": false
}
}
}`;

test.beforeEach((t) => {
t.context.verboseLogStub = sinon.stub();
Expand Down Expand Up @@ -108,6 +166,37 @@ test.serial("default manifest creation", async (t) => {
t.is(await result.getString(), expectedManifestContent, "Correct result returned");
});

test.serial("default manifest creation with special characters", async (t) => {
const {manifestCreator, errorLogStub} = t.context;
const prefix = "/resources/sap/ui/mine/";
const libraryResource = {
getPath: () => {
return prefix + ".library";
},
getString: async () => {
return libraryContentSpecialChars;
},
_project: {
dependencies: [{
metadata: {
name: "sap.ui.core"
}
}]
}
};
const resources = ["", "_en", "_de"].map((lang) => {
return {
getPath: () => {
return `${prefix}i18n(.*)./i18n(.*)${lang}.properties`;
}
};
});
t.is(errorLogStub.callCount, 0);

const result = await manifestCreator({libraryResource, resources, options: {}});
t.is(await result.getString(), expectedManifestContentSpecialChars, "Correct result returned");
});

test.serial("manifest creation for sap/apf", async (t) => {
const {manifestCreator, errorLogStub, verboseLogStub} = t.context;

Expand Down

0 comments on commit 9f1b588

Please sign in to comment.