MCPcopy Create free account
hub / github.com/plotly/plotly.js / findLocaleStrings

Function findLocaleStrings

tasks/find_locale_strings.js:21–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19findLocaleStrings();
20
21function findLocaleStrings() {
22 glob(srcGlob).then(function(files) {
23 var dict = {};
24 var hasTranslation = false;
25 var maxLen = 0;
26
27 files.forEach(function(file) {
28 var code = fs.readFileSync(file, 'utf-8');
29 var filePartialPath = file.substr(constants.pathToSrc.length);
30
31 falafel(code, { ecmaVersion: 'latest', locations: true }, function(node) {
32 if(node.type === 'CallExpression' &&
33 (node.callee.name === '_' || node.callee.source() === 'Lib._')
34 ) {
35 // parse through code string looking for translated strings
36 // You may either assign `Lib.localize` to `_` and use that, or
37 // call `Lib.localize` directly.
38
39 var strNode = node.arguments[1];
40 if(node.arguments.length !== 2) {
41 logError(file, node, 'Localize takes 2 args');
42 }
43 if(strNode.type !== 'Literal') {
44 logError(file, node, 'Translated string must be a literal');
45 }
46 if(!dict[strNode.value]) {
47 dict[strNode.value] = filePartialPath + ':' + node.loc.start.line;
48 maxLen = Math.max(maxLen, strNode.value.length);
49 hasTranslation = true;
50 }
51 } else if(node.type === 'VariableDeclarator' && node.id.name === '_') {
52 // make sure localize is the only thing we assign to a variable `_`
53 // NB: this does not preclude using `_` for an unused function arg
54
55 var src = node.init.source();
56 if(!localizeRE.test(src)) {
57 logError(file, node, 'Use `_` only to mean localization');
58 }
59 }
60 });
61 });
62
63 if(!hasTranslation) {
64 console.error('Found no translations.');
65 EXIT_CODE = 1;
66 }
67
68 if(!EXIT_CODE) {
69 if(noOutput) {
70 console.log('ok find_locale_strings - no output requested.');
71 } else {
72 var strings = Object.keys(dict).sort().map(function(k) {
73 return k + spaces(maxLen - k.length) + ' // ' + dict[k];
74 }).join('\n');
75 common.writeFile(constants.pathToTranslationKeys, strings);
76 console.log('ok find_locale_strings - wrote new key file.');
77 }
78 }

Callers 1

Calls 3

globFunction · 0.85
logErrorFunction · 0.85
spacesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…