Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multiple messages files #81

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion test/ProductionModePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const mkWebpackConfig = (options) => ({
production: true,
developmentLocale: "en",
supportedLocales: supportedLocales,
messages: path.join(__dirname, "fixtures/translations/[locale].json"),
messages: [path.join(__dirname, "fixtures/translations/[locale].json"),
path.join(__dirname, "fixtures/more-translations/[locale].json")],
output: "[locale].js"
},
options.additionalGWPAttributes
Expand Down Expand Up @@ -167,6 +168,11 @@ function commonTests(testName, webpackConfig, outputPath) {
expect(result).to.equal("Be the first to like this");
});

it("should include strings defined in all locale files", function() {
const result = Globalize.formatMessage("foo");
expect(result).to.equal("bar");
});

it("should include formatRelativeTime", () => {
const result = Globalize.formatRelativeTime(1, "second");
expect(result).to.equal("in 1 second");
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ like( 1 );
like( 2 );
like( 3 );

// Use Globalize to format a message from a second message file
Globalize.messageFormatter("foo");

// Use Globalize to format relative time.
Globalize.formatRelativeTime( -35, "second" );

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/more-translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"en": {
"foo": "bar"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/more-translations/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"es": {
"foo": "baz"
}
}
18 changes: 12 additions & 6 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ module.exports = {
}
},

readMessages: (messagesFilepath, locale) => {
messagesFilepath = messagesFilepath.replace("[locale]", locale);
if (!fs.existsSync(messagesFilepath) || !fs.statSync(messagesFilepath).isFile()) {
console.warn("Unable to find messages file: `" + messagesFilepath + "`");
return null;
readMessages: (messagesFilepaths, locale) => {
let messages = {};
const filepaths = [].concat(messagesFilepaths);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great solution to make it always an array.


for (let path of filepaths) {
path = path.replace("[locale]", locale);
if (!fs.existsSync(path) || !fs.statSync(path).isFile()) {
console.warn("Unable to find messages file: `" + path + "`");
return null;
}
messages[locale] = Object.assign(messages[locale] || {}, JSON.parse(fs.readFileSync(path))[locale]);
}
return JSON.parse(fs.readFileSync(messagesFilepath));
return messages;
},

tmpdir: (base) => {
Expand Down