Last active
January 16, 2017 07:46
-
-
Save 1000hz/6f43e06fb1c34c5c0ddb3f146f3f5043 to your computer and use it in GitHub Desktop.
i18n for dogs
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
{ | |
const random = (min, max) => { | |
if (min === undefined && max === undefined) return Math.random() | |
if (max === undefined) { | |
max = min | |
min = 0 | |
} | |
return ~~(Math.random() * max) + min | |
} | |
const sample = (arr) => arr[random(arr.length)] | |
const capitalize = (str) => str.charAt(0).toUpperCase() + str.slice(1) | |
const isCapitalized = (str) => | |
str.charCodeAt(0) >= "A".charCodeAt(0) && | |
str.charCodeAt(0) <= "Z".charCodeAt(0) | |
const replaceTextContent = (html, replaceFn) => { | |
const htmlOrTextRegex = /(<.*?>)|([\w-]+)/g | |
return html.replace( | |
htmlOrTextRegex, | |
(_, htmlTag, textContent) => htmlTag || replaceFn(textContent) | |
) | |
} | |
document.querySelectorAll('body').forEach(node => | |
node.innerHTML = replaceTextContent(node.innerHTML, (match) => { | |
const BARKS = ["arf", "woof", "bark", "woof-woof", "ruff", "awooo", "bow-wow"] | |
const bark = sample(BARKS) | |
return isCapitalized(match) | |
? capitalize(bark) | |
: bark | |
}) | |
) | |
String.fromCodePoint(128054) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment