Skip to content

Instantly share code, notes, and snippets.

Avatar

Laurent Payot laurentpayot

View GitHub Profile
@laurentpayot
laurentpayot / i18n.js
Last active Jul 3, 2019
JavaScript i18n function with internal references in 20 LOC (for simple plural languages such as English or French)
View i18n.js
function i18n(path, data) {
let val = path.split('.').reduce((acc, curr) => acc[curr], i18n.locales[i18n.locale])
if (Array.isArray(val)) {
return val[data - 1] || val[val.length - 1]
} else {
return val ? val
.replace(/{[^@}]+}/g, s => data[s.slice(1, -1)])
.replace(/{@[^}]+}/g, s => {
let n = null
let subpath = s.slice(2, -1).replace(/\(.*\)/, argStr => {
You can’t perform that action at this time.