Created
February 18, 2020 15:32
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
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
oh this is custom code for me, you can remove this line and hard-code your default
lng