diff --git a/app/containers/App/constants.js b/app/containers/App/constants.js index db0c5ce795..0004482269 100644 --- a/app/containers/App/constants.js +++ b/app/containers/App/constants.js @@ -12,3 +12,4 @@ export const LOAD_REPOS = 'boilerplate/App/LOAD_REPOS'; export const LOAD_REPOS_SUCCESS = 'boilerplate/App/LOAD_REPOS_SUCCESS'; export const LOAD_REPOS_ERROR = 'boilerplate/App/LOAD_REPOS_ERROR'; +export const DEFAULT_LOCALE = 'en'; diff --git a/app/i18n.js b/app/i18n.js index 3ad360f948..21f10f17de 100644 --- a/app/i18n.js +++ b/app/i18n.js @@ -5,6 +5,7 @@ * */ import { addLocaleData } from 'react-intl'; +import { DEFAULT_LOCALE } from '../app/containers/App/constants'; import enLocaleData from 'react-intl/locale-data/en'; import deLocaleData from 'react-intl/locale-data/de'; @@ -20,16 +21,22 @@ export const appLocales = [ import enTranslationMessages from './translations/en.json'; import deTranslationMessages from './translations/de.json'; -export const formatTranslationMessages = (messages) => { +export const formatTranslationMessages = (locale, messages) => { + const defaultFormattedMessages = locale !== DEFAULT_LOCALE ? formatTranslationMessages(DEFAULT_LOCALE, enTranslationMessages) : {}; const formattedMessages = {}; - for (const message of messages) { - formattedMessages[message.id] = message.message || message.defaultMessage; + const messageKeys = Object.keys(messages); + for (const messageKey of messageKeys) { + if (locale === DEFAULT_LOCALE) { + formattedMessages[messageKey] = messages[messageKey]; + } else { + formattedMessages[messageKey] = messages[messageKey] || defaultFormattedMessages[messageKey]; + } } return formattedMessages; }; export const translationMessages = { - en: formatTranslationMessages(enTranslationMessages), - de: formatTranslationMessages(deTranslationMessages), + en: formatTranslationMessages('en', enTranslationMessages), + de: formatTranslationMessages('de', deTranslationMessages), }; diff --git a/app/translations/de.json b/app/translations/de.json index 0290000da5..8846b62819 100644 --- a/app/translations/de.json +++ b/app/translations/de.json @@ -1,152 +1,32 @@ -[ - { - "id": "app.components.LocaleToggle.de", - "defaultMessage": "de", - "message": "" - }, - { - "id": "app.components.LocaleToggle.en", - "defaultMessage": "en", - "message": "" - }, - { - "id": "boilerplate.components.Footer.author.message", - "defaultMessage": "Made with love by {author}.", - "message": "Mit Liebe gemacht von {author}." - }, - { - "id": "boilerplate.components.Footer.license.message", - "defaultMessage": "This project is licensed under the MIT license.", - "message": "Dieses Projekt wird unter der MIT-Lizenz veröffentlicht." - }, - { - "id": "boilerplate.containers.FeaturePage.css.header", - "defaultMessage": "Features", - "message": "" - }, - { - "id": "boilerplate.containers.FeaturePage.css.message", - "defaultMessage": "Next generation CSS", - "message": "Die nächste Generation von CSS" - }, - { - "id": "boilerplate.containers.FeaturePage.feedback.header", - "defaultMessage": "Instant feedback", - "message": "Sofortiges Feedback" - }, - { - "id": "boilerplate.containers.FeaturePage.feedback.message", - "defaultMessage": "Enjoy the best DX and code your app at the speed of thought! Your\n saved changes to the CSS and JS are reflected instantaneously\n without refreshing the page. Preserve application state even when\n you update something in the underlying code!", - "message": "Genießen Sie die beste Entwicklungserfahrung und programmieren Sie Ihre App so schnell wie noch nie! Ihre Änderungen an dem CSS und JavaScript sind sofort reflektiert, ohne die Seite aktualisieren zu müssen. So bleibt der Anwendungszustand bestehen, auch wenn Sie etwas in dem darunter liegenden Code aktualisieren!" - }, - { - "id": "boilerplate.containers.FeaturePage.header", - "defaultMessage": "Features", - "message": "" - }, - { - "id": "boilerplate.containers.FeaturePage.home", - "defaultMessage": "Home", - "message": "" - }, - { - "id": "boilerplate.containers.FeaturePage.internationalization.header", - "defaultMessage": "Complete i18n Standard Internationalization & Pluralization", - "message": "Komplette i18n Standard-Internationalisierung und Pluralisierung" - }, - { - "id": "boilerplate.containers.FeaturePage.internationalization.message", - "defaultMessage": "Scalable apps need to support multiple languages, easily add and support multiple languages with `react-intl`.", - "message": "Das Internet ist global. Mehrsprachige- und Pluralisierungsunterstützung ist entscheidend für große Web-Anwendungen." - }, - { - "id": "boilerplate.containers.FeaturePage.javascript.header", - "defaultMessage": "Next generation JavaScript", - "message": "Die nächste Generation von JavaScript" - }, - { - "id": "boilerplate.containers.FeaturePage.javascript.message", - "defaultMessage": "Use template strings, object destructuring, arrow functions, JSX\n syntax and more, today.", - "message": "Benutzen Sie ES6 template strings, object destructuring, arrow functions, JSX syntax und mehr, heute." - }, - { - "id": "boilerplate.containers.FeaturePage.network.header", - "defaultMessage": "Offline-first", - "message": "" - }, - { - "id": "boilerplate.containers.FeaturePage.network.message", - "defaultMessage": "The next frontier in performant web apps: availability without a\n network connection from the instant your users load the app.", - "message": "Die nächste Schwelle für performanten Web-Anwendungen: Verfügbarkeit ohne Netzwerkverbindung wenn Ihre Benutzer die App einmal heruntergeladen haben." - }, - { - "id": "boilerplate.containers.FeaturePage.routing.header", - "defaultMessage": "Industry-standard routing", - "message": "Standard Routing" - }, - { - "id": "boilerplate.containers.FeaturePage.routing.message", - "defaultMessage": "Write composable CSS that's co-located with your components for\n complete modularity. Unique generated class names keep the\n specificity low while eliminating style clashes. Ship only the\n styles that are on the page for the best performance.", - "message": "Schreiben Sie CSS, das am selben Ort wie ihre Komponenten ist. Deterministisch generierte, einzigartige Klassennamen halten die Spezifität niedrig während styling Konflikte vermieden werden. Senden Sie nur das CSS an ihre Benutzer welches dann wirklich sichtbar ist für die schnellste Performance!" - }, - { - "id": "boilerplate.containers.FeaturePage.scaffolding.header", - "defaultMessage": "Quick scaffolding", - "message": "Schnelles Scaffolding" - }, - { - "id": "boilerplate.containers.FeaturePage.scaffolding.message", - "defaultMessage": "Automate the creation of components, containers, routes, selectors\n and sagas - and their tests - right from the CLI!", - "message": "Automatisieren Sie die Kreation von Komponenten, Containern, Routen, Selektoren und Sagas – und ihre Tests – direkt von dem Terminal!" - }, - { - "id": "boilerplate.containers.FeaturePage.state_management.header", - "defaultMessage": "Predictable state management", - "message": "Berechenbare Stateverwaltung" - }, - { - "id": "boilerplate.containers.FeaturePage.state_management.message", - "defaultMessage": "Unidirectional data flow allows for change logging and time travel\n debugging.", - "message": "Unidirectional data flow erlaubt uns alle Änderungen ihrer Applikation zu loggen und time travel debugging einzusetzen." - }, - { - "id": "boilerplate.containers.HomePage.features.Button", - "defaultMessage": "Features", - "message": "" - }, - { - "id": "boilerplate.containers.HomePage.start_project.header", - "defaultMessage": "Start your next react project in seconds", - "message": "Beginnen Sie Ihr nächstes React Projekt in Sekunden" - }, - { - "id": "boilerplate.containers.HomePage.start_project.message", - "defaultMessage": "A highly scalable, offline-first foundation with the best DX and a focus on performance and best practices", - "message": "Ein skalierendes, offline-first Fundament mit der besten DX und einem Fokus auf Performance und bewährte Methoden" - }, - { - "id": "boilerplate.containers.HomePage.tryme.atPrefix", - "defaultMessage": "@", - "message": "" - }, - { - "id": "boilerplate.containers.HomePage.tryme.header", - "defaultMessage": "Try me!", - "message": "Probiere mich!" - }, - { - "id": "boilerplate.containers.HomePage.tryme.message", - "defaultMessage": "Show Github repositories by", - "message": "Zeige die Github Repositories von" - }, - { - "id": "boilerplate.containers.NotFoundPage.header", - "defaultMessage": "Page not found.", - "message": "Seite nicht gefunden." - }, - { - "id": "boilerplate.containers.NotFoundPage.home", - "defaultMessage": "Home", - "message": "" - } -] +{ + "app.components.LocaleToggle.de": "", + "app.components.LocaleToggle.en": "", + "boilerplate.components.Footer.author.message": "Mit Liebe gemacht von {author}.", + "boilerplate.components.Footer.license.message": "Dieses Projekt wird unter der MIT-Lizenz veröffentlicht.", + "boilerplate.containers.FeaturePage.css.header": "", + "boilerplate.containers.FeaturePage.css.message": "Die nächste Generation von CSS", + "boilerplate.containers.FeaturePage.feedback.header": "Sofortiges Feedback", + "boilerplate.containers.FeaturePage.feedback.message": "Genießen Sie die beste Entwicklungserfahrung und programmieren Sie Ihre App so schnell wie noch nie! Ihre Änderungen an dem CSS und JavaScript sind sofort reflektiert, ohne die Seite aktualisieren zu müssen. So bleibt der Anwendungszustand bestehen, auch wenn Sie etwas in dem darunter liegenden Code aktualisieren!", + "boilerplate.containers.FeaturePage.header": "", + "boilerplate.containers.FeaturePage.home": "", + "boilerplate.containers.FeaturePage.internationalization.header": "Komplette i18n Standard-Internationalisierung und Pluralisierung", + "boilerplate.containers.FeaturePage.internationalization.message": "Das Internet ist global. Mehrsprachige- und Pluralisierungsunterstützung ist entscheidend für große Web-Anwendungen.", + "boilerplate.containers.FeaturePage.javascript.header": "Das Internet ist global. Mehrsprachige- und Pluralisierungsunterstützung ist entscheidend für große Web-Anwendungen.", + "boilerplate.containers.FeaturePage.javascript.message": "Benutzen Sie ES6 template strings, object destructuring, arrow functions, JSX syntax und mehr, heute.", + "boilerplate.containers.FeaturePage.network.header": "", + "boilerplate.containers.FeaturePage.network.message": "The next frontier in performant web apps: availability without a\n network connection from the instant your users load the app.", + "boilerplate.containers.FeaturePage.routing.header": "Standard Routing", + "boilerplate.containers.FeaturePage.routing.message": "Schreiben Sie CSS, das am selben Ort wie ihre Komponenten ist. Deterministisch generierte, einzigartige Klassennamen halten die Spezifität niedrig während styling Konflikte vermieden werden. Senden Sie nur das CSS an ihre Benutzer welches dann wirklich sichtbar ist für die schnellste Performance!", + "boilerplate.containers.FeaturePage.scaffolding.header": "Schnelles Scaffolding", + "boilerplate.containers.FeaturePage.scaffolding.message": "Automatisieren Sie die Kreation von Komponenten, Containern, Routen, Selektoren und Sagas – und ihre Tests – direkt von dem Terminal!", + "boilerplate.containers.FeaturePage.state_management.header": "Berechenbare Stateverwaltung", + "boilerplate.containers.FeaturePage.state_management.message": "Unidirectional data flow erlaubt uns alle Änderungen ihrer Applikation zu loggen und time travel debugging einzusetzen.", + "boilerplate.containers.HomePage.features.Button": "", + "boilerplate.containers.HomePage.start_project.header": "Beginnen Sie Ihr nächstes React Projekt in Sekunden", + "boilerplate.containers.HomePage.start_project.message": "Ein skalierendes, offline-first Fundament mit der besten DX und einem Fokus auf Performance und bewährte Methoden", + "boilerplate.containers.HomePage.tryme.atPrefix": "", + "boilerplate.containers.HomePage.tryme.header": "Probiere mich!", + "boilerplate.containers.HomePage.tryme.message": "Zeige die Github Repositories von", + "boilerplate.containers.NotFoundPage.header": "Seite nicht gefunden.", + "boilerplate.containers.NotFoundPage.home": "" +} diff --git a/app/translations/en.json b/app/translations/en.json index 369a26c82b..5591a8bd01 100644 --- a/app/translations/en.json +++ b/app/translations/en.json @@ -1,152 +1,30 @@ -[ - { - "id": "app.components.LocaleToggle.de", - "defaultMessage": "de", - "message": "" - }, - { - "id": "app.components.LocaleToggle.en", - "defaultMessage": "en", - "message": "" - }, - { - "id": "boilerplate.components.Footer.author.message", - "defaultMessage": "Made with love by {author}.", - "message": "Made with love by {author}." - }, - { - "id": "boilerplate.components.Footer.license.message", - "defaultMessage": "This project is licensed under the MIT license.", - "message": "This project is licensed under the MIT license." - }, - { - "id": "boilerplate.containers.FeaturePage.css.header", - "defaultMessage": "Features", - "message": "Next generation CSS" - }, - { - "id": "boilerplate.containers.FeaturePage.css.message", - "defaultMessage": "Next generation CSS", - "message": "Write composable CSS that's co-located with your components for\n complete modularity. Unique generated class names keep the\n specificity low while eliminating style clashes. Ship only the\n styles that are on the page for the best performance." - }, - { - "id": "boilerplate.containers.FeaturePage.feedback.header", - "defaultMessage": "Instant feedback", - "message": "Instant feedback" - }, - { - "id": "boilerplate.containers.FeaturePage.feedback.message", - "defaultMessage": "Enjoy the best DX and code your app at the speed of thought! Your\n saved changes to the CSS and JS are reflected instantaneously\n without refreshing the page. Preserve application state even when\n you update something in the underlying code!", - "message": "Enjoy the best DX and code your app at the speed of thought! Your\n saved changes to the CSS and JS are reflected instantaneously\n without refreshing the page. Preserve application state even when\n you update something in the underlying code!" - }, - { - "id": "boilerplate.containers.FeaturePage.header", - "defaultMessage": "Features", - "message": "" - }, - { - "id": "boilerplate.containers.FeaturePage.home", - "defaultMessage": "Home", - "message": "Home" - }, - { - "id": "boilerplate.containers.FeaturePage.internationalization.header", - "defaultMessage": "Complete i18n Standard Internationalization & Pluralization", - "message": "Complete i18n Standard Internationalization & Pluralization" - }, - { - "id": "boilerplate.containers.FeaturePage.internationalization.message", - "defaultMessage": "Scalable apps need to support multiple languages, easily add and support multiple languages with `react-intl`.", - "message": "Scalable apps need to support multiple languages, easily add and support multiple languages with `react-intl`." - }, - { - "id": "boilerplate.containers.FeaturePage.javascript.header", - "defaultMessage": "Next generation JavaScript", - "message": "Next generation JavaScript" - }, - { - "id": "boilerplate.containers.FeaturePage.javascript.message", - "defaultMessage": "Use template strings, object destructuring, arrow functions, JSX\n syntax and more, today.", - "message": "Use template strings, object destructuring, arrow functions, JSX\n syntax and more, today." - }, - { - "id": "boilerplate.containers.FeaturePage.network.header", - "defaultMessage": "Offline-first", - "message": "Offline-first" - }, - { - "id": "boilerplate.containers.FeaturePage.network.message", - "defaultMessage": "The next frontier in performant web apps: availability without a\n network connection from the instant your users load the app.", - "message": "The next frontier in performant web apps: availability without a\n network connection from the instant your users load the app." - }, - { - "id": "boilerplate.containers.FeaturePage.routing.header", - "defaultMessage": "Industry-standard routing", - "message": "Industry-standard routing" - }, - { - "id": "boilerplate.containers.FeaturePage.routing.message", - "defaultMessage": "Write composable CSS that's co-located with your components for\n complete modularity. Unique generated class names keep the\n specificity low while eliminating style clashes. Ship only the\n styles that are on the page for the best performance.", - "message": "It's natural to want to add pages (e.g. '/about') to your\n application, and routing makes this possible." - }, - { - "id": "boilerplate.containers.FeaturePage.scaffolding.header", - "defaultMessage": "Quick scaffolding", - "message": "Quick scaffolding" - }, - { - "id": "boilerplate.containers.FeaturePage.scaffolding.message", - "defaultMessage": "Automate the creation of components, containers, routes, selectors\n and sagas - and their tests - right from the CLI!", - "message": "Automate the creation of components, containers, routes, selectors\n and sagas - and their tests - right from the CLI!" - }, - { - "id": "boilerplate.containers.FeaturePage.state_management.header", - "defaultMessage": "Predictable state management", - "message": "Predictable state management" - }, - { - "id": "boilerplate.containers.FeaturePage.state_management.message", - "defaultMessage": "Unidirectional data flow allows for change logging and time travel\n debugging.", - "message": "Unidirectional data flow allows for change logging and time travel\n debugging." - }, - { - "id": "boilerplate.containers.HomePage.features.Button", - "defaultMessage": "Features", - "message": "Features" - }, - { - "id": "boilerplate.containers.HomePage.start_project.header", - "defaultMessage": "Start your next react project in seconds", - "message": "Start your next react project in seconds" - }, - { - "id": "boilerplate.containers.HomePage.start_project.message", - "defaultMessage": "A highly scalable, offline-first foundation with the best DX and a focus on performance and best practices", - "message": "A highly scalable, offline-first foundation with the best DX and a focus on performance and best practices" - }, - { - "id": "boilerplate.containers.HomePage.tryme.atPrefix", - "defaultMessage": "@", - "message": "@" - }, - { - "id": "boilerplate.containers.HomePage.tryme.header", - "defaultMessage": "Try me!", - "message": "Try me!" - }, - { - "id": "boilerplate.containers.HomePage.tryme.message", - "defaultMessage": "Show Github repositories by", - "message": "Show Github repositories by" - }, - { - "id": "boilerplate.containers.NotFoundPage.header", - "defaultMessage": "Page not found.", - "message": "Page not found." - }, - { - "id": "boilerplate.containers.NotFoundPage.home", - "defaultMessage": "Home", - "message": "Home" - } -] +{ + "boilerplate.components.Footer.author.message": "Made with love by {author}.", + "boilerplate.components.Footer.license.message": "This project is licensed under the MIT license.", + "boilerplate.containers.FeaturePage.css.header": "Features", + "boilerplate.containers.FeaturePage.css.message": "Next generation CSS", + "boilerplate.containers.FeaturePage.feedback.header": "Instant feedback", + "boilerplate.containers.FeaturePage.feedback.message": "Enjoy the best DX and code your app at the speed of thought! Your\n saved changes to the CSS and JS are reflected instantaneously\n without refreshing the page. Preserve application state even when\n you update something in the underlying code!", + "boilerplate.containers.FeaturePage.header": "Features", + "boilerplate.containers.FeaturePage.home": "Home", + "boilerplate.containers.FeaturePage.internationalization.header": "Complete i18n Standard Internationalization & Pluralization", + "boilerplate.containers.FeaturePage.internationalization.message": "Scalable apps need to support multiple languages, easily add and support multiple languages with `react-intl`.", + "boilerplate.containers.FeaturePage.javascript.header": "Next generation JavaScript", + "boilerplate.containers.FeaturePage.javascript.message": "Use template strings, object destructuring, arrow functions, JSX\n syntax and more, today.", + "boilerplate.containers.FeaturePage.network.header": "Offline-first", + "boilerplate.containers.FeaturePage.network.message": "The next frontier in performant web apps: availability without a\n network connection from the instant your users load the app.", + "boilerplate.containers.FeaturePage.routing.header": "Industry-standard routing", + "boilerplate.containers.FeaturePage.routing.message": "Write composable CSS that's co-located with your components for\n complete modularity. Unique generated class names keep the\n specificity low while eliminating style clashes. Ship only the\n styles that are on the page for the best performance.", + "boilerplate.containers.FeaturePage.scaffolding.header": "Quick scaffolding", + "boilerplate.containers.FeaturePage.scaffolding.message": "Automate the creation of components, containers, routes, selectors\n and sagas - and their tests - right from the CLI!", + "boilerplate.containers.FeaturePage.state_management.header": "Predictable state management", + "boilerplate.containers.FeaturePage.state_management.message": "Unidirectional data flow allows for change logging and time travel\n debugging.", + "boilerplate.containers.HomePage.features.Button": "Features", + "boilerplate.containers.HomePage.start_project.header": "Start your next react project in seconds", + "boilerplate.containers.HomePage.start_project.message": "A highly scalable, offline-first foundation with the best DX and a focus on performance and best practices", + "boilerplate.containers.HomePage.tryme.atPrefix": "@", + "boilerplate.containers.HomePage.tryme.header": "Try me!", + "boilerplate.containers.HomePage.tryme.message": "Show Github repositories by", + "boilerplate.containers.NotFoundPage.header": "Page not found.", + "boilerplate.containers.NotFoundPage.home": "Home" +} diff --git a/internals/generators/language/format-translation-messages.hbs b/internals/generators/language/format-translation-messages.hbs index 74c78b4632..143601fe95 100644 --- a/internals/generators/language/format-translation-messages.hbs +++ b/internals/generators/language/format-translation-messages.hbs @@ -1 +1 @@ -$1 {{language}}: formatTranslationMessages({{language}}TranslationMessages), +$1 {{language}}: formatTranslationMessages('{{language}}', {{language}}TranslationMessages), diff --git a/internals/generators/language/index.js b/internals/generators/language/index.js index 6922062342..969799620e 100644 --- a/internals/generators/language/index.js +++ b/internals/generators/language/index.js @@ -48,7 +48,7 @@ module.exports = { actions.push({ type: 'modify', path: '../../app/i18n.js', - pattern: /([a-z]+:\sformatTranslationMessages\([a-z]+TranslationMessages\),\n)(?!.*[a-z]+:\sformatTranslationMessages\([a-z]+TranslationMessages\),)/g, + pattern: /([a-z]+:\sformatTranslationMessages\('[a-z]+',\s[a-z]+TranslationMessages\),\n)(?!.*[a-z]+:\sformatTranslationMessages\('[a-z]+',\s[a-z]+TranslationMessages\),)/g, templateFile: './language/format-translation-messages.hbs', }); actions.push({ diff --git a/internals/scripts/extract-intl.js b/internals/scripts/extract-intl.js index 901aac3a64..38f46e0555 100644 --- a/internals/scripts/extract-intl.js +++ b/internals/scripts/extract-intl.js @@ -12,6 +12,7 @@ const addCheckmark = require('./helpers/checkmark'); const pkg = require('../../package.json'); const i18n = require('../../app/i18n'); +import { DEFAULT_LOCALE } from '../../app/containers/App/constants'; require('shelljs/global'); @@ -61,8 +62,9 @@ for (const locale of locales) { try { // Parse the old translation message JSON files const messages = JSON.parse(fs.readFileSync(translationFileName)); - for (const message of messages) { - oldLocaleMappings[locale][message.id] = message; + const messageKeys = Object.keys(messages); + for (const messageKey of messageKeys) { + oldLocaleMappings[locale][messageKey] = messages[messageKey]; } } catch (error) { if (error.code !== 'ENOENT') { @@ -88,14 +90,10 @@ const extractFromFile = async (fileName) => { for (const locale of locales) { const oldLocaleMapping = oldLocaleMappings[locale][message.id]; // Merge old translations into the babel extracted instances where react-intl is used - localeMappings[locale][message.id] = { - id: message.id, - description: message.description, - defaultMessage: message.defaultMessage, - message: (oldLocaleMapping && oldLocaleMapping.message) - ? oldLocaleMapping.message - : '', - }; + const newMsg = ( locale === DEFAULT_LOCALE) ? message.defaultMessage : ''; + localeMappings[locale][message.id] = (oldLocaleMapping) + ? oldLocaleMapping + : newMsg; } } } catch (error) { @@ -125,14 +123,9 @@ const extractFromFile = async (fileName) => { // Sort the translation JSON file so that git diffing is easier // Otherwise the translation messages will jump around every time we extract - let messages = Object.values(localeMappings[locale]).sort((a, b) => { - a = a.id.toUpperCase(); - b = b.id.toUpperCase(); - return do { - if (a < b) -1; - else if (a > b) 1; - else 0; - }; + let messages = {}; + Object.keys(localeMappings[locale]).sort().forEach(function(key) { + messages[key] = localeMappings[locale][key]; }); // Write to file the JSON representation of the translation messages diff --git a/internals/templates/i18n.js b/internals/templates/i18n.js index 561e80ac26..f02dcd9b07 100644 --- a/internals/templates/i18n.js +++ b/internals/templates/i18n.js @@ -5,6 +5,7 @@ * */ import { addLocaleData } from 'react-intl'; +import { DEFAULT_LOCALE } from 'containers/App/constants'; import enLocaleData from 'react-intl/locale-data/en'; @@ -16,15 +17,21 @@ import enTranslationMessages from './translations/en.json'; addLocaleData(enLocaleData); -const formatTranslationMessages = (messages) => { +export const formatTranslationMessages = (locale, messages) => { + const defaultFormattedMessages = locale !== DEFAULT_LOCALE ? formatTranslationMessages(DEFAULT_LOCALE, enTranslationMessages) : {}; const formattedMessages = {}; - for (const message of messages) { - formattedMessages[message.id] = message.message || message.defaultMessage; + const messageKeys = Object.keys(messages); + for (const messageKey of messageKeys) { + if (locale === DEFAULT_LOCALE) { + formattedMessages[messageKey] = messages[messageKey]; + } else { + formattedMessages[messageKey] = messages[messageKey] || defaultFormattedMessages[messageKey]; + } } return formattedMessages; }; export const translationMessages = { - en: formatTranslationMessages(enTranslationMessages), + en: formatTranslationMessages('en', enTranslationMessages), };