|
| 1 | +import Vue from "vue"; |
| 2 | +import VueI18Next from "@panter/vue-i18next"; |
| 3 | +import i18next from "i18next"; |
| 4 | +import i18nextXHRBackend from "i18next-xhr-backend"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Logic from ../src/js/localization.js |
| 8 | + */ |
| 9 | +function parseInputFile(data) { |
| 10 | + // Remove the $n interpolate of Chrome $1, $2, ... -> {{1}}, {{2}}, ... |
| 11 | + const REGEXP_CHROME = /\$([1-9])/g; |
| 12 | + const dataChrome = data.replace(REGEXP_CHROME, "{{$1}}"); |
| 13 | + |
| 14 | + // Remove the .message of the nesting $t(xxxxx.message) -> $t(xxxxx) |
| 15 | + const REGEXP_NESTING = /\$t\(([^\)]*).message\)/g; |
| 16 | + const dataNesting = dataChrome.replace(REGEXP_NESTING, "$t($1)"); |
| 17 | + |
| 18 | + // Move the .message of the json object to root xxxxx.message -> xxxxx |
| 19 | + const jsonData = JSON.parse(dataNesting); |
| 20 | + Object.entries(jsonData).forEach(([key, value]) => { |
| 21 | + jsonData[key] = value.message; |
| 22 | + }); |
| 23 | + |
| 24 | + return jsonData; |
| 25 | +} |
| 26 | + |
| 27 | +i18next.use(i18nextXHRBackend).init({ |
| 28 | + lng: "en", |
| 29 | + debug: true, |
| 30 | + getAsync: false, |
| 31 | + ns: ["messages"], |
| 32 | + defaultNS: ["messages"], |
| 33 | + fallbackLng: { |
| 34 | + default: ["en"], |
| 35 | + }, |
| 36 | + backend: { |
| 37 | + loadPath: "/locales/{{lng}}/{{ns}}.json", |
| 38 | + parse: parseInputFile, |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +Vue.use(VueI18Next); |
| 43 | + |
| 44 | +const i18n = new VueI18Next(i18next); |
| 45 | + |
| 46 | +export const decorators = [ |
| 47 | + (story) => ({ |
| 48 | + i18n, |
| 49 | + components: { story }, |
| 50 | + template: ` |
| 51 | + <div style="margin: 1rem;"> |
| 52 | + <link rel="stylesheet" href="/css/opensans_webfontkit/fonts.css" /> |
| 53 | + <link rel="stylesheet" href="/css/main.css" /> |
| 54 | + <story /> |
| 55 | + </div> |
| 56 | +`, |
| 57 | + }), |
| 58 | +]; |
| 59 | + |
| 60 | +export const parameters = { |
| 61 | + actions: { argTypesRegex: "^on[A-Z].*" }, |
| 62 | + controls: { |
| 63 | + matchers: { |
| 64 | + color: /(background|color)$/i, |
| 65 | + date: /Date$/, |
| 66 | + }, |
| 67 | + }, |
| 68 | +}; |
0 commit comments