Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 05cb407

Browse files
committed
language generation fixed. closes #23
extract-intl is TODO for now
1 parent 78039e2 commit 05cb407

File tree

4 files changed

+172
-180
lines changed

4 files changed

+172
-180
lines changed

internals/generators/language/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,31 @@ module.exports = {
5454

5555
actions.push({
5656
type: 'modify',
57-
path: '../../app/i18n.js',
57+
path: '../../app/i18n.ts',
5858
pattern: /(const ..LocaleData = require\('react-intl\/locale-data\/..'\);\n)+/g,
5959
templateFile: './language/intl-locale-data.hbs',
6060
});
6161
actions.push({
6262
type: 'modify',
63-
path: '../../app/i18n.js',
63+
path: '../../app/i18n.ts',
6464
pattern: /(\s+'[a-z]+',\n)(?!.*\s+'[a-z]+',)/g,
6565
templateFile: './language/app-locale.hbs',
6666
});
6767
actions.push({
6868
type: 'modify',
69-
path: '../../app/i18n.js',
69+
path: '../../app/i18n.ts',
7070
pattern: /(const ..TranslationMessages = require\('\.\/translations\/..\.json'\);\n)(?!const ..TranslationMessages = require\('\.\/translations\/..\.json'\);\n)/g,
7171
templateFile: './language/translation-messages.hbs',
7272
});
7373
actions.push({
7474
type: 'modify',
75-
path: '../../app/i18n.js',
75+
path: '../../app/i18n.ts',
7676
pattern: /(addLocaleData\([a-z]+LocaleData\);\n)(?!.*addLocaleData\([a-z]+LocaleData\);)/g,
7777
templateFile: './language/add-locale-data.hbs',
7878
});
7979
actions.push({
8080
type: 'modify',
81-
path: '../../app/i18n.js',
81+
path: '../../app/i18n.ts',
8282
pattern: /([a-z]+:\sformatTranslationMessages\('[a-z]+',\s[a-z]+TranslationMessages\),\n)(?!.*[a-z]+:\sformatTranslationMessages\('[a-z]+',\s[a-z]+TranslationMessages\),)/g,
8383
templateFile: './language/format-translation-messages.hbs',
8484
});
@@ -90,7 +90,7 @@ module.exports = {
9090
});
9191
actions.push({
9292
type: 'modify',
93-
path: '../../app/app.js',
93+
path: '../../app/app.tsx',
9494
pattern: /(import\('intl\/locale-data\/jsonp\/[a-z]+\.js'\),\n)(?!.*import\('intl\/locale-data\/jsonp\/[a-z]+\.js'\),)/g,
9595
templateFile: './language/polyfill-intl-locale.hbs',
9696
});

internals/scripts/extract-intl.js

Lines changed: 166 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,164 +1,166 @@
1-
/* eslint-disable no-restricted-syntax */
2-
/**
3-
* This script will extract the internationalization messages from all components
4-
* and package them in the translation json files in the translations file.
5-
*/
6-
7-
require('shelljs/global');
8-
9-
const fs = require('fs');
10-
const nodeGlob = require('glob');
11-
const { transform } = require('@babel/core');
12-
const get = require('lodash/get');
13-
14-
const animateProgress = require('./helpers/progress');
15-
const addCheckmark = require('./helpers/checkmark');
16-
17-
const { appLocales, DEFAULT_LOCALE } = require('../../app/i18n');
18-
19-
const babel = require('../../babel.config.js');
20-
const { presets } = babel;
21-
let plugins = babel.plugins || [];
22-
23-
plugins.push('react-intl');
24-
25-
// NOTE: styled-components plugin is filtered out as it creates errors when used with transform
26-
plugins = plugins.filter(p => p !== 'styled-components');
27-
28-
// Glob to match all js files except test files
29-
const FILES_TO_PARSE = 'app/**/!(*.test).js';
30-
31-
const newLine = () => process.stdout.write('\n');
32-
33-
// Progress Logger
34-
let progress;
35-
const task = message => {
36-
progress = animateProgress(message);
37-
process.stdout.write(message);
38-
39-
return error => {
40-
if (error) {
41-
process.stderr.write(error);
42-
}
43-
clearTimeout(progress);
44-
return addCheckmark(() => newLine());
45-
};
46-
};
47-
48-
// Wrap async functions below into a promise
49-
const glob = pattern =>
50-
new Promise((resolve, reject) => {
51-
nodeGlob(
52-
pattern,
53-
(error, value) => (error ? reject(error) : resolve(value)),
54-
);
55-
});
56-
57-
const readFile = fileName =>
58-
new Promise((resolve, reject) => {
59-
fs.readFile(
60-
fileName,
61-
'utf8',
62-
(error, value) => (error ? reject(error) : resolve(value)),
63-
);
64-
});
65-
66-
// Store existing translations into memory
67-
const oldLocaleMappings = [];
68-
const localeMappings = [];
69-
70-
// Loop to run once per locale
71-
for (const locale of appLocales) {
72-
oldLocaleMappings[locale] = {};
73-
localeMappings[locale] = {};
74-
// File to store translation messages into
75-
const translationFileName = `app/translations/${locale}.json`;
76-
try {
77-
// Parse the old translation message JSON files
78-
const messages = JSON.parse(fs.readFileSync(translationFileName));
79-
const messageKeys = Object.keys(messages);
80-
for (const messageKey of messageKeys) {
81-
oldLocaleMappings[locale][messageKey] = messages[messageKey];
82-
}
83-
} catch (error) {
84-
if (error.code !== 'ENOENT') {
85-
process.stderr.write(
86-
`There was an error loading this translation file: ${translationFileName}
87-
\n${error}`,
88-
);
89-
}
90-
}
91-
}
92-
93-
const extractFromFile = async filename => {
94-
try {
95-
const code = await readFile(filename);
96-
97-
const output = await transform(code, { filename, presets, plugins });
98-
const messages = get(output, 'metadata.react-intl.messages', []);
99-
100-
for (const message of messages) {
101-
for (const locale of appLocales) {
102-
const oldLocaleMapping = oldLocaleMappings[locale][message.id];
103-
// Merge old translations into the babel extracted instances where react-intl is used
104-
const newMsg = locale === DEFAULT_LOCALE ? message.defaultMessage : '';
105-
localeMappings[locale][message.id] = oldLocaleMapping || newMsg;
106-
}
107-
}
108-
} catch (error) {
109-
process.stderr.write(`\nError transforming file: ${filename}\n${error}\n`);
110-
}
111-
};
112-
113-
const memoryTask = glob(FILES_TO_PARSE);
114-
const memoryTaskDone = task('Storing language files in memory');
115-
116-
memoryTask.then(files => {
117-
memoryTaskDone();
118-
119-
const extractTask = Promise.all(
120-
files.map(fileName => extractFromFile(fileName)),
121-
);
122-
const extractTaskDone = task('Run extraction on all files');
123-
// Run extraction on all files that match the glob on line 16
124-
extractTask.then(() => {
125-
extractTaskDone();
126-
127-
// Make the directory if it doesn't exist, especially for first run
128-
mkdir('-p', 'app/translations'); // eslint-disable-line
129-
130-
let localeTaskDone;
131-
let translationFileName;
132-
133-
for (const locale of appLocales) {
134-
translationFileName = `app/translations/${locale}.json`;
135-
localeTaskDone = task(
136-
`Writing translation messages for ${locale} to: ${translationFileName}`,
137-
);
138-
139-
// Sort the translation JSON file so that git diffing is easier
140-
// Otherwise the translation messages will jump around every time we extract
141-
const messages = {};
142-
Object.keys(localeMappings[locale])
143-
.sort()
144-
.forEach(key => {
145-
messages[key] = localeMappings[locale][key];
146-
});
147-
148-
// Write to file the JSON representation of the translation messages
149-
const prettified = `${JSON.stringify(messages, null, 2)}\n`;
150-
151-
try {
152-
fs.writeFileSync(translationFileName, prettified);
153-
localeTaskDone();
154-
} catch (error) {
155-
localeTaskDone(
156-
`There was an error saving this translation file: ${translationFileName}
157-
\n${error}`,
158-
);
159-
}
160-
}
161-
162-
process.exit();
163-
});
164-
});
1+
// /* eslint-disable no-restricted-syntax */
2+
// /**
3+
// * This script will extract the internationalization messages from all components
4+
// * and package them in the translation json files in the translations file.
5+
// */
6+
7+
// COMMENTED OUT - TODO
8+
9+
// require('shelljs/global');
10+
11+
// const fs = require('fs');
12+
// const nodeGlob = require('glob');
13+
// const { transform } = require('@babel/core');
14+
// const get = require('lodash/get');
15+
16+
// const animateProgress = require('./helpers/progress');
17+
// const addCheckmark = require('./helpers/checkmark');
18+
19+
// const { appLocales, DEFAULT_LOCALE } = require('../../app/i18n.ts');
20+
21+
// const babel = require('../../babel.config.js');
22+
// const { presets } = babel;
23+
// let plugins = babel.plugins || [];
24+
25+
// plugins.push('react-intl');
26+
27+
// // NOTE: styled-components plugin is filtered out as it creates errors when used with transform
28+
// plugins = plugins.filter(p => p !== 'styled-components');
29+
30+
// // Glob to match all js files except test files
31+
// const FILES_TO_PARSE = 'app/**/!(*.test).ts*';
32+
33+
// const newLine = () => process.stdout.write('\n');
34+
35+
// // Progress Logger
36+
// let progress;
37+
// const task = message => {
38+
// progress = animateProgress(message);
39+
// process.stdout.write(message);
40+
41+
// return error => {
42+
// if (error) {
43+
// process.stderr.write(error);
44+
// }
45+
// clearTimeout(progress);
46+
// return addCheckmark(() => newLine());
47+
// };
48+
// };
49+
50+
// // Wrap async functions below into a promise
51+
// const glob = pattern =>
52+
// new Promise((resolve, reject) => {
53+
// nodeGlob(
54+
// pattern,
55+
// (error, value) => (error ? reject(error) : resolve(value)),
56+
// );
57+
// });
58+
59+
// const readFile = fileName =>
60+
// new Promise((resolve, reject) => {
61+
// fs.readFile(
62+
// fileName,
63+
// 'utf8',
64+
// (error, value) => (error ? reject(error) : resolve(value)),
65+
// );
66+
// });
67+
68+
// // Store existing translations into memory
69+
// const oldLocaleMappings = [];
70+
// const localeMappings = [];
71+
72+
// // Loop to run once per locale
73+
// for (const locale of appLocales) {
74+
// oldLocaleMappings[locale] = {};
75+
// localeMappings[locale] = {};
76+
// // File to store translation messages into
77+
// const translationFileName = `app/translations/${locale}.json`;
78+
// try {
79+
// // Parse the old translation message JSON files
80+
// const messages = JSON.parse(fs.readFileSync(translationFileName));
81+
// const messageKeys = Object.keys(messages);
82+
// for (const messageKey of messageKeys) {
83+
// oldLocaleMappings[locale][messageKey] = messages[messageKey];
84+
// }
85+
// } catch (error) {
86+
// if (error.code !== 'ENOENT') {
87+
// process.stderr.write(
88+
// `There was an error loading this translation file: ${translationFileName}
89+
// \n${error}`,
90+
// );
91+
// }
92+
// }
93+
// }
94+
95+
// const extractFromFile = async filename => {
96+
// try {
97+
// const code = await readFile(filename);
98+
99+
// const output = await transform(code, { filename, presets, plugins });
100+
// const messages = get(output, 'metadata.react-intl.messages', []);
101+
102+
// for (const message of messages) {
103+
// for (const locale of appLocales) {
104+
// const oldLocaleMapping = oldLocaleMappings[locale][message.id];
105+
// // Merge old translations into the babel extracted instances where react-intl is used
106+
// const newMsg = locale === DEFAULT_LOCALE ? message.defaultMessage : '';
107+
// localeMappings[locale][message.id] = oldLocaleMapping || newMsg;
108+
// }
109+
// }
110+
// } catch (error) {
111+
// process.stderr.write(`\nError transforming file: ${filename}\n${error}\n`);
112+
// }
113+
// };
114+
115+
// const memoryTask = glob(FILES_TO_PARSE);
116+
// const memoryTaskDone = task('Storing language files in memory');
117+
118+
// memoryTask.then(files => {
119+
// memoryTaskDone();
120+
121+
// const extractTask = Promise.all(
122+
// files.map(fileName => extractFromFile(fileName)),
123+
// );
124+
// const extractTaskDone = task('Run extraction on all files');
125+
// // Run extraction on all files that match the glob on line 16
126+
// extractTask.then(() => {
127+
// extractTaskDone();
128+
129+
// // Make the directory if it doesn't exist, especially for first run
130+
// mkdir('-p', 'app/translations'); // eslint-disable-line
131+
132+
// let localeTaskDone;
133+
// let translationFileName;
134+
135+
// for (const locale of appLocales) {
136+
// translationFileName = `app/translations/${locale}.json`;
137+
// localeTaskDone = task(
138+
// `Writing translation messages for ${locale} to: ${translationFileName}`,
139+
// );
140+
141+
// // Sort the translation JSON file so that git diffing is easier
142+
// // Otherwise the translation messages will jump around every time we extract
143+
// const messages = {};
144+
// Object.keys(localeMappings[locale])
145+
// .sort()
146+
// .forEach(key => {
147+
// messages[key] = localeMappings[locale][key];
148+
// });
149+
150+
// // Write to file the JSON representation of the translation messages
151+
// const prettified = `${JSON.stringify(messages, null, 2)}\n`;
152+
153+
// try {
154+
// fs.writeFileSync(translationFileName, prettified);
155+
// localeTaskDone();
156+
// } catch (error) {
157+
// localeTaskDone(
158+
// `There was an error saving this translation file: ${translationFileName}
159+
// \n${error}`,
160+
// );
161+
// }
162+
// }
163+
164+
// process.exit();
165+
// });
166+
// });

package-lock.json

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)