Skip to content

Commit

Permalink
Add a way to override default translations, fixes #45
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjalava committed Jun 16, 2017
1 parent de1a043 commit 0d2e8a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Application expects to find an object named `sizeme_options` (TODO: rename to `S
gaTrackingId: "UA-40735596-2",
shopType: "magento",
debugState: false,
uiOptions: {}
uiOptions: {},
additionalTranslations: {}
}
```
* [serviceStatus] (_Boolean_): is SizeMe enabled. Default: true.
Expand All @@ -50,3 +51,16 @@ Application expects to find an object named `sizeme_options` (TODO: rename to `S
- [sizeSelectorType] (_String_): type of the size selector used in the shop. Possible values at the moment: "default" (default, doh) and "swatches"
- [addToCartElement] (_String_): DOM-element to listen to for add-to-cart events
- [addToCartEvent] (_String_): DOM-event for add-to-cart

* [additionalTranslations] (_Object_): Optionally override translations defined under ['i18n'](src/i18n). Example of how to
override the Swedish translation for chest:
```javascript
{
sv: {
humanMeasurements: {
chest: "Bröst"
}
}
}
```
Similarly override any other key in any other language (en, fi, sv).
14 changes: 13 additions & 1 deletion src/i18n.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global sizeme_options */

import uiOptions from "./api/uiOptions";
import i18n from "i18next";
import en from "./i18n/en.json";
Expand All @@ -7,7 +9,7 @@ import sv from "./i18n/sv.json";
i18n.init({
lng: uiOptions.lang || document.documentElement.lang || "en",
fallbackLng: "en",
debug: true,
debug: false,
interpolation: {
format: (value, format) => {
switch (format) {
Expand Down Expand Up @@ -35,4 +37,14 @@ i18n.init({
}
});

if (sizeme_options.additionalTranslations) {
console.log(sizeme_options.additionalTranslations);
const addtr = sizeme_options.additionalTranslations;
["en", "fi", "sv"].forEach(lng => {
if (addtr[lng]) {
i18n.addResourceBundle(lng, "translation", addtr[lng], true, true);
}
});
}

export default i18n;

0 comments on commit 0d2e8a7

Please sign in to comment.