Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Support for elongation of numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
jopdeklein committed Feb 14, 2019
1 parent 16c9b68 commit ff41d79
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ const pseudoLocalization = (() => {
const start = (
options = {
strategy: "accented",
blacklistedNodeNames: opts.blacklistedNodeNames
blacklistedNodeNames: opts.blacklistedNodeNames,
elongateNumbers: false
}
) => {
opts.blacklistedNodeNames = options.blacklistedNodeNames;
opts.strategy = options.strategy;
opts.elongateNumbers = options.elongateNumbers;
pseudoLocalize(document.body);
observer.observe(document.body, observerConfig);
};
Expand Down
25 changes: 17 additions & 8 deletions localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,39 @@ const strategies = {
};

const psuedoLocalizeString = (string, options = { strategy: "accented" }) => {
let opts = strategies[options.strategy];
let strategyOptions = strategies[options.strategy];

let pseudoLocalizedText = "";
for (let character of string) {
if (opts.map[character]) {
const convertedCharacter = strategyOptions.map[character]
const characterAsInt = parseInt(character)

if (
options.elongateNumbers &&
!isNaN(characterAsInt) &&
characterAsInt >= 3
) {
pseudoLocalizedText += character.repeat(2);
} else if (convertedCharacter) {
const cl = character.toLowerCase();
// duplicate "a", "e", "o" and "u" to emulate ~30% longer text
if (
opts.elongate &&
strategyOptions.elongate &&
(cl === "a" || cl === "e" || cl === "o" || cl === "u")
) {
pseudoLocalizedText += opts.map[character] + opts.map[character];
} else pseudoLocalizedText += opts.map[character];
pseudoLocalizedText += convertedCharacter.repeat(2);
} else pseudoLocalizedText += strategyOptions.map[character];
} else pseudoLocalizedText += character;
}

// If this string is from the DOM, it should already contain the pre- and postfix.
if (
pseudoLocalizedText.startsWith(opts.prefix) &&
pseudoLocalizedText.endsWith(opts.postfix)
pseudoLocalizedText.startsWith(strategyOptions.prefix) &&
pseudoLocalizedText.endsWith(strategyOptions.postfix)
) {
return pseudoLocalizedText;
}
return opts.prefix + pseudoLocalizedText + opts.postfix;
return strategyOptions.prefix + pseudoLocalizedText + strategyOptions.postfix;
};

module.exports = psuedoLocalizeString;

0 comments on commit ff41d79

Please sign in to comment.