Skip to content

Instantly share code, notes, and snippets.

@florentdestremau
Created February 18, 2020 15:32
Show Gist options
  • Save florentdestremau/0e7233a5c8a934f4d29bb01026978da9 to your computer and use it in GitHub Desktop.
Save florentdestremau/0e7233a5c8a934f4d29bb01026978da9 to your computer and use it in GitHub Desktop.
Simple conversion script to import a Symfony-Yaml file into a React-i18n project
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import frYaml from 'js-yaml-loader!../translations/messages.fr.yml';
import enYaml from 'js-yaml-loader!../translations/messages.en.yml';
const splitPlurals = (object) => {
const newObject = {};
Object.keys(object).forEach((key) => {
let elem = object[key];
if (typeof elem === 'object') {
newObject[key] = splitPlurals(elem);
return;
}
// replace all symfony parameters %param% with {{param}} in all strings for js
elem = String(elem).replace(/%([^%]+(?=%))%/gi, '{{$1}}');
// splits all plurales like "one apple|many apples" into different keys apple and apple_plural
if (typeof elem === 'string' && elem.includes('|')) {
const plural = elem.split('|');
newObject[key] = plural.shift();
newObject[`${key}_plural`] = plural.shift();
return;
}
newObject[key] = elem;
});
return newObject;
};
i18n
.use(initReactI18next)
.init({
resources: {
fr: {
translation: splitPlurals(frYaml),
},
en: {
translation: splitPlurals(enYaml),
},
},
lng: (window && window.locale) || 'fr',
fallbackLng: 'fr',
// keySeparator: false, // don't count "." as separator
});
export default i18n;
@florentdestremau
Copy link
Author

oh this is custom code for me, you can remove this line and hard-code your default lng

@kuronana
Copy link

Ah yes, ok, thx for your help and thx you for your quick response :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment