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

Support for elongation of numbers #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = (
{
strategy = "accented",
blacklistedNodeNames = opts.blacklistedNodeNames
blacklistedNodeNames = opts.blacklistedNodeNames,
elongateNumbers = false
} = {}
) => {
opts.blacklistedNodeNames = blacklistedNodeNames;
opts.strategy = strategy;
opts.elongateNumbers = elongateNumbers;

pseudoLocalize(document.body);
observer.observe(document.body, observerConfig);
Expand Down
36 changes: 27 additions & 9 deletions localize.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ const BIDI_MAP = {
Z: "Z"
};

const EVEN_NUMBER_MAP = {
0: '⁰',
2: '²',
4: '⁴',
6: '⁶',
8: '⁸'
};

const strategies = {
accented: {
prefix: "",
Expand All @@ -126,30 +134,40 @@ 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) &&
EVEN_NUMBER_MAP[character]
) {
// Duplicate even numbers with superscript variations
pseudoLocalizedText += character + EVEN_NUMBER_MAP[character];
} else if (convertedCharacter) {
const cl = character.toLowerCase();
// duplicate "a", "e", "o" and "u" to emulate ~30% longer text
// 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;