Skip to content

Commit 5072071

Browse files
authored
Merge pull request #3 from translation/develop
Support Common Modules as locale files
2 parents 68af7b0 + 302f275 commit 5072071

File tree

9 files changed

+84
-49
lines changed

9 files changed

+84
-49
lines changed

bin/commands/sync.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ var _languages = require("../util/languages");
1515

1616
var _source = require("../util/source");
1717

18-
var _path = require("path");
19-
20-
var _fs = require("fs");
18+
var _helpers = require("../util/helpers");
2119

2220
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
2321

@@ -84,9 +82,7 @@ function _default(config) {
8482
sourceTranslations.forEach(function (segment) {
8583
updatedSourceTranslations[segment.key] = segment.target;
8684
});
87-
(0, _fs.writeFileSync)((0, _path.join)(process.cwd(), config.translations_directory, "".concat(config.source_locale, ".json")), JSON.stringify(updatedSourceTranslations, null, 4), {
88-
encoding: 'utf8'
89-
});
85+
(0, _helpers.writeLocaleFile)(config.source_locale, updatedSourceTranslations, config);
9086
}
9187

9288
var api = new _Translation["default"](config);
@@ -114,9 +110,7 @@ function _default(config) {
114110
sourceTranslations.forEach(function (segment) {
115111
translations[segment.key] = segment.target;
116112
});
117-
(0, _fs.writeFileSync)((0, _path.join)(process.cwd(), config.translations_directory, "".concat(config.source_locale, ".json")), JSON.stringify(translations, null, 4), {
118-
encoding: 'utf8'
119-
});
113+
(0, _helpers.writeLocaleFile)(config.source_locale, translations, config);
120114
});
121115
}
122116

@@ -133,9 +127,7 @@ function _default(config) {
133127
}
134128
}
135129
});
136-
(0, _fs.writeFileSync)((0, _path.join)(process.cwd(), config.translations_directory, "".concat(locale, ".json")), JSON.stringify(translations, null, 4), {
137-
encoding: 'utf8'
138-
});
130+
(0, _helpers.writeLocaleFile)(locale, translations, config);
139131
});
140132

141133
if (config.purge) {

bin/util/helpers.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.writeLocaleFile = void 0;
7+
8+
var _fs = require("fs");
9+
10+
var _util = require("util");
11+
12+
var _path = require("path");
13+
14+
var writeLocaleFile = function writeLocaleFile(locale, translations, config) {
15+
var outputString,
16+
fileExtension = 'json';
17+
18+
if (config.output === 'module') {
19+
outputString = 'module.exports = ' + (0, _util.inspect)(translations, false, 2, false);
20+
fileExtension = 'js';
21+
} else {
22+
outputString = JSON.stringify(translations, null, 4);
23+
}
24+
25+
(0, _fs.writeFileSync)((0, _path.join)(process.cwd(), config.translations_directory, "".concat(locale, ".").concat(fileExtension)), outputString, {
26+
encoding: 'utf8'
27+
});
28+
};
29+
30+
exports.writeLocaleFile = writeLocaleFile;

bin/util/languages.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ var fetchSegmentsFromLanguageFiles = function fetchSegmentsFromLanguageFiles(con
2929
var languageFilesPath = (0, _path.resolve)(process.cwd(), config.translations_directory);
3030
var languageFiles = (0, _fs.readdirSync)(languageFilesPath).map(function (file) {
3131
var languagePath = (0, _path.resolve)(process.cwd(), config.translations_directory, file);
32+
var languageObject;
3233

33-
var languageModule = require(languagePath);
34+
if (config.output === 'module') {
35+
languageObject = require(languagePath);
36+
} else {
37+
var languageModule = require(languagePath);
38+
39+
var defaultImport = languageModule["default"];
40+
languageObject = defaultImport ? defaultImport : languageModule;
41+
}
3442

35-
var defaultImport = languageModule["default"];
36-
var languageObject = defaultImport ? defaultImport : languageModule;
3743
var fileName = file.replace(process.cwd(), '');
3844
return {
3945
fileName: fileName,

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@translation/vue",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"license": "MIT",
55
"author": "Simon Depelchin",
66
"publishConfig": {

src/commands/sync.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import * as log from '../util/log'
22
import Translation from '../Translation'
33
import { fetchSegmentsFromLanguageFiles } from '../util/languages'
44
import { fetchSegmentsFromVueFiles } from '../util/source'
5-
import { join } from 'path'
6-
import { writeFileSync } from 'fs'
5+
import { writeLocaleFile } from '../util/helpers'
76

87
export default function(config) {
98
log.info('Synchronizing project over Translation.io ...')
@@ -82,15 +81,7 @@ export default function(config) {
8281
updatedSourceTranslations[segment.key] = segment.target
8382
})
8483

85-
writeFileSync(
86-
join(
87-
process.cwd(),
88-
config.translations_directory,
89-
`${config.source_locale}.json`
90-
),
91-
JSON.stringify(updatedSourceTranslations, null, 4),
92-
{ encoding: 'utf8' }
93-
)
84+
writeLocaleFile(config.source_locale, updatedSourceTranslations, config)
9485
}
9586

9687
const api = new Translation(config)
@@ -128,15 +119,7 @@ export default function(config) {
128119
translations[segment.key] = segment.target
129120
})
130121

131-
writeFileSync(
132-
join(
133-
process.cwd(),
134-
config.translations_directory,
135-
`${config.source_locale}.json`
136-
),
137-
JSON.stringify(translations, null, 4),
138-
{ encoding: 'utf8' }
139-
)
122+
writeLocaleFile(config.source_locale, translations, config)
140123
})
141124
}
142125

@@ -156,15 +139,7 @@ export default function(config) {
156139
}
157140
})
158141

159-
writeFileSync(
160-
join(
161-
process.cwd(),
162-
config.translations_directory,
163-
`${locale}.json`
164-
),
165-
JSON.stringify(translations, null, 4),
166-
{ encoding: 'utf8' }
167-
)
142+
writeLocaleFile(locale, translations, config)
168143
})
169144

170145
if (config.purge) {

src/util/helpers.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { writeFileSync } from 'fs'
2+
import { inspect } from 'util'
3+
import { join } from 'path'
4+
5+
const writeLocaleFile = (locale, translations, config) => {
6+
let outputString, fileExtension = 'json'
7+
if (config.output === 'module') {
8+
outputString = 'module.exports = ' + inspect(translations, false, 2, false)
9+
fileExtension = 'js'
10+
} else {
11+
outputString = JSON.stringify(translations, null, 4)
12+
}
13+
14+
writeFileSync(
15+
join(
16+
process.cwd(),
17+
config.translations_directory,
18+
`${locale}.${fileExtension}`
19+
),
20+
outputString,
21+
{ encoding: 'utf8' }
22+
)
23+
}
24+
25+
export { writeLocaleFile }

src/util/languages.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ export const fetchSegmentsFromLanguageFiles = config => {
2222
file
2323
)
2424

25-
const languageModule = require(languagePath)
26-
const { default: defaultImport } = languageModule
25+
let languageObject
2726

28-
const languageObject = defaultImport ? defaultImport : languageModule
27+
if (config.output === 'module') {
28+
languageObject = require(languagePath)
29+
} else {
30+
const languageModule = require(languagePath)
31+
const { default: defaultImport } = languageModule
32+
33+
languageObject = defaultImport ? defaultImport : languageModule
34+
}
2935

3036
const fileName = file.replace(process.cwd(), '')
3137

translation.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"key": null,
3+
"output": "json",
34
"source_locale": "en",
45
"target_locales": [],
56
"source_path": "src/**/*.?(js|vue)",

0 commit comments

Comments
 (0)