Created
October 4, 2017 13:40
-
-
Save renancouto/165cc2513be98eefa20b3a73a3cc9b08 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/lodehiy
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// https://github.com/sindresorhus/pupa | |
const pupa = (tpl, data) => { | |
if (typeof tpl !== 'string') { | |
throw new TypeError(`Expected a string in the first argument, got ${typeof tpl}`); | |
} | |
if (typeof data !== 'object') { | |
throw new TypeError(`Expected an Object/Array in the second argument, got ${typeof data}`); | |
} | |
const re = /{(.*?)}/g; | |
return tpl.replace(re, (_, key) => { | |
let ret = data; | |
for (const prop of key.split('.')) { | |
ret = ret ? ret[prop] : ''; | |
} | |
return ret || ''; | |
}); | |
}; | |
const locale = { | |
"foo": "I'm foo", | |
"bar": "I'm dynamic {value}", | |
"lorem": { 0: "no {x}", 1: "there's a {x} here", n: "there's a lot of {x}" }, | |
"job": { 0: "jobs", 1: "job", n: "jobs" } | |
}; | |
const t = (key, data = {}, count) => { | |
let str = locale[key]; | |
if (typeof str !== 'string' && count !== undefined) { | |
str = str[count > 1 ? 'n' : count]; | |
} | |
return pupa(str, data); | |
}; | |
console.log(t('foo')); | |
console.log(t('bar', { value: 'hoera!' })); | |
console.log(t('lorem', { x: t('job', {}, 0) }, 0)); | |
console.log(t('lorem', { x: t('job', {}, 1) }, 1)); | |
console.log(t('lorem', { x: t('job', {}, 10) }, 10)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// https://github.com/sindresorhus/pupa | |
const pupa = (tpl, data) => { | |
if (typeof tpl !== 'string') { | |
throw new TypeError(`Expected a string in the first argument, got ${typeof tpl}`); | |
} | |
if (typeof data !== 'object') { | |
throw new TypeError(`Expected an Object/Array in the second argument, got ${typeof data}`); | |
} | |
const re = /{(.*?)}/g; | |
return tpl.replace(re, (_, key) => { | |
let ret = data; | |
for (const prop of key.split('.')) { | |
ret = ret ? ret[prop] : ''; | |
} | |
return ret || ''; | |
}); | |
}; | |
const locale = { | |
"foo": "I'm foo", | |
"bar": "I'm dynamic {value}", | |
"lorem": { 0: "no {x}", 1: "there's a {x} here", n: "there's a lot of {x}" }, | |
"job": { 0: "jobs", 1: "job", n: "jobs" } | |
}; | |
const t = (key, data = {}, count) => { | |
let str = locale[key]; | |
if (typeof str !== 'string' && count !== undefined) { | |
str = str[count > 1 ? 'n' : count]; | |
} | |
return pupa(str, data); | |
}; | |
console.log(t('foo')); | |
console.log(t('bar', { value: 'hoera!' })); | |
console.log(t('lorem', { x: t('job', {}, 0) }, 0)); | |
console.log(t('lorem', { x: t('job', {}, 1) }, 1)); | |
console.log(t('lorem', { x: t('job', {}, 10) }, 10)); | |
</script></body> | |
</html> |
This file contains hidden or 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
// https://github.com/sindresorhus/pupa | |
const pupa = (tpl, data) => { | |
if (typeof tpl !== 'string') { | |
throw new TypeError(`Expected a string in the first argument, got ${typeof tpl}`); | |
} | |
if (typeof data !== 'object') { | |
throw new TypeError(`Expected an Object/Array in the second argument, got ${typeof data}`); | |
} | |
const re = /{(.*?)}/g; | |
return tpl.replace(re, (_, key) => { | |
let ret = data; | |
for (const prop of key.split('.')) { | |
ret = ret ? ret[prop] : ''; | |
} | |
return ret || ''; | |
}); | |
}; | |
const locale = { | |
"foo": "I'm foo", | |
"bar": "I'm dynamic {value}", | |
"lorem": { 0: "no {x}", 1: "there's a {x} here", n: "there's a lot of {x}" }, | |
"job": { 0: "jobs", 1: "job", n: "jobs" } | |
}; | |
const t = (key, data = {}, count) => { | |
let str = locale[key]; | |
if (typeof str !== 'string' && count !== undefined) { | |
str = str[count > 1 ? 'n' : count]; | |
} | |
return pupa(str, data); | |
}; | |
console.log(t('foo')); | |
console.log(t('bar', { value: 'hoera!' })); | |
console.log(t('lorem', { x: t('job', {}, 0) }, 0)); | |
console.log(t('lorem', { x: t('job', {}, 1) }, 1)); | |
console.log(t('lorem', { x: t('job', {}, 10) }, 10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment