pseudo-localization 2.4.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/pseudo-localize.mjs +31 -0
- package/package.json +27 -23
- package/bin/pseudo-localize +0 -147
- package/dist/cjs/index.js +0 -2
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/localize.js +0 -2
- package/dist/cjs/localize.js.map +0 -1
- package/dist/index.d.ts +0 -19
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -117
- package/dist/localize.d.ts +0 -7
- package/dist/localize.d.ts.map +0 -1
- package/dist/localize.js +0 -150
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import yargs from 'yargs/yargs';
|
|
4
|
+
import { hideBin } from 'yargs/helpers';
|
|
5
|
+
import { pseudoLocalizeString } from '../src/localize.mjs';
|
|
6
|
+
|
|
7
|
+
yargs(hideBin(process.argv))
|
|
8
|
+
.usage(
|
|
9
|
+
'$0 <string> [options]',
|
|
10
|
+
'Pseudo localize a string',
|
|
11
|
+
(yargs) => {
|
|
12
|
+
return yargs
|
|
13
|
+
.positional('string', {
|
|
14
|
+
describe: 'String to pseudo-localize',
|
|
15
|
+
type: 'string',
|
|
16
|
+
})
|
|
17
|
+
.option('strategy', {
|
|
18
|
+
alias: 's',
|
|
19
|
+
type: 'string',
|
|
20
|
+
describe: 'Set the strategy for localization',
|
|
21
|
+
choices: ['accented', 'bidi'],
|
|
22
|
+
default: 'accented',
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
({ string, strategy }) => {
|
|
26
|
+
process.stdout.write(pseudoLocalizeString(string, { strategy }));
|
|
27
|
+
}
|
|
28
|
+
)
|
|
29
|
+
.help()
|
|
30
|
+
.version()
|
|
31
|
+
.parse();
|
package/package.json
CHANGED
|
@@ -1,19 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pseudo-localization",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "./dist/cjs/index.js",
|
|
6
|
-
"types": "./dist/index.d.ts",
|
|
3
|
+
"version": "3.0.1",
|
|
4
|
+
"description": "pseudo-localization for internationalization testing",
|
|
7
5
|
"files": [
|
|
8
6
|
"dist",
|
|
9
7
|
"bin"
|
|
10
8
|
],
|
|
11
|
-
"
|
|
9
|
+
"main": "./src/localize.mjs",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": "./src/localize.mjs"
|
|
14
|
+
},
|
|
15
|
+
"dom": {
|
|
16
|
+
"import": "./src/dom.mjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"bin": {
|
|
20
|
+
"pseudo-localization": "bin/pseudo-localize.mjs"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": "^23"
|
|
24
|
+
},
|
|
12
25
|
"scripts": {
|
|
13
26
|
"start": "node devserver.mjs",
|
|
14
|
-
"prepare": "npm run tsc && npm run transpile-cjs",
|
|
15
|
-
"transpile-cjs": "babel src --out-dir dist/cjs --minified --no-comments --source-maps --extensions '.ts'",
|
|
16
|
-
"generate-types": "tsc --emitDeclarationOnly",
|
|
17
27
|
"check-formatting": "prettier --list-different src/**/*.ts",
|
|
18
28
|
"format": "prettier --write src/**/*.ts",
|
|
19
29
|
"lint": "eslint src/**/*.js",
|
|
@@ -36,25 +46,19 @@
|
|
|
36
46
|
],
|
|
37
47
|
"repository": {
|
|
38
48
|
"type": "git",
|
|
39
|
-
"url": "https://github.com/tryggvigy/pseudo-localization.git"
|
|
49
|
+
"url": "git+https://github.com/tryggvigy/pseudo-localization.git"
|
|
40
50
|
},
|
|
41
51
|
"license": "MIT",
|
|
42
52
|
"devDependencies": {
|
|
43
|
-
"@
|
|
44
|
-
"@
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"eslint": "^
|
|
50
|
-
"jest": "^28.1.3",
|
|
51
|
-
"jest-cli": "^28.1.3",
|
|
52
|
-
"prettier": "^1.19.1"
|
|
53
|
+
"@eslint/js": "^9.17.0",
|
|
54
|
+
"@types/node": "^22.10.2",
|
|
55
|
+
"eslint": "^9.17.0",
|
|
56
|
+
"globals": "^15.13.0",
|
|
57
|
+
"prettier": "^3.4.2",
|
|
58
|
+
"typescript": "^5.7.2",
|
|
59
|
+
"typescript-eslint": "^8.18.0"
|
|
53
60
|
},
|
|
54
61
|
"dependencies": {
|
|
55
|
-
"
|
|
56
|
-
"get-stdin": "^7.0.0",
|
|
57
|
-
"typescript": "^4.7.4",
|
|
58
|
-
"yargs": "^17.2.1"
|
|
62
|
+
"yargs": "^17.7.2"
|
|
59
63
|
}
|
|
60
64
|
}
|
package/bin/pseudo-localize
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
#! /usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const getStdin = require('get-stdin');
|
|
6
|
-
const flatten = require('flat');
|
|
7
|
-
const unflatten = require('flat').unflatten;
|
|
8
|
-
|
|
9
|
-
const { localize } = require('../dist/cjs/index');
|
|
10
|
-
|
|
11
|
-
const argv = require('yargs')
|
|
12
|
-
.usage(
|
|
13
|
-
'$0 [src] [options]',
|
|
14
|
-
'Pseudo localize a string, JSON file, or a JavaScript object',
|
|
15
|
-
function(yargs) {
|
|
16
|
-
yargs.positional('src', {
|
|
17
|
-
describe: 'The source as a path or from STDIN',
|
|
18
|
-
type: 'string',
|
|
19
|
-
coerce: path.resolve,
|
|
20
|
-
conflicts: 'i',
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
)
|
|
24
|
-
.options({
|
|
25
|
-
o: {
|
|
26
|
-
alias: 'output',
|
|
27
|
-
describe:
|
|
28
|
-
'Writes output to STDOUT by default. Optionally specify a JSON file to write the pseudo-localizations to',
|
|
29
|
-
type: 'string',
|
|
30
|
-
coerce: path.resolve,
|
|
31
|
-
},
|
|
32
|
-
i: {
|
|
33
|
-
alias: 'input',
|
|
34
|
-
describe: 'Pass in direct input to pseudo-localize',
|
|
35
|
-
type: 'string',
|
|
36
|
-
conflicts: 'src',
|
|
37
|
-
},
|
|
38
|
-
debug: {
|
|
39
|
-
describe: 'Print out all stack traces and other debug info',
|
|
40
|
-
type: 'boolean',
|
|
41
|
-
},
|
|
42
|
-
pretty: {
|
|
43
|
-
describe: 'Pretty print JSON output',
|
|
44
|
-
type: 'boolean',
|
|
45
|
-
},
|
|
46
|
-
strategy: {
|
|
47
|
-
describe: 'Set the strategy for localization',
|
|
48
|
-
choices: ['accented', 'bidi'],
|
|
49
|
-
default: 'accented',
|
|
50
|
-
},
|
|
51
|
-
})
|
|
52
|
-
.help()
|
|
53
|
-
.version().argv;
|
|
54
|
-
|
|
55
|
-
const debug = function(e) {
|
|
56
|
-
if (argv.debug) {
|
|
57
|
-
console.error(e);
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const write = function(output) {
|
|
62
|
-
argv.output ? fs.writeFileSync(argv.output, output) : fs.writeSync(1, output);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const convert = function(input) {
|
|
66
|
-
const options = { strategy: argv.strategy };
|
|
67
|
-
|
|
68
|
-
if (typeof input === 'string') {
|
|
69
|
-
write(localize(input, options));
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Flatten nested translation JSONs
|
|
74
|
-
input = flatten(input, {
|
|
75
|
-
safe: true,
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
for (let key in input) {
|
|
79
|
-
// this will need to be updated if other primitives
|
|
80
|
-
// support pseudo-localization
|
|
81
|
-
if (typeof input[key] === 'string') {
|
|
82
|
-
input[key] = localize(input[key], options);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// Retrieve initial JSON structure
|
|
87
|
-
output = unflatten(input, {
|
|
88
|
-
safe: true,
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
write(JSON.stringify(output, null, argv.pretty ? 4 : undefined) + '\n');
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
(function() {
|
|
95
|
-
'use strict';
|
|
96
|
-
|
|
97
|
-
let json = null;
|
|
98
|
-
|
|
99
|
-
if (argv.i) {
|
|
100
|
-
convert(argv.i + '\n');
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (argv.src) {
|
|
105
|
-
try {
|
|
106
|
-
json = require(argv.src);
|
|
107
|
-
|
|
108
|
-
if (json.default) {
|
|
109
|
-
json = json.default;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
convert(json);
|
|
113
|
-
} catch (e) {
|
|
114
|
-
debug(e);
|
|
115
|
-
|
|
116
|
-
console.error(
|
|
117
|
-
`${e.message}\nUnable to parse input file. Make sure it is in JSON format or is an exported JS module`
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
process.exit(1);
|
|
121
|
-
}
|
|
122
|
-
} else {
|
|
123
|
-
getStdin()
|
|
124
|
-
.then(function(input) {
|
|
125
|
-
if (!input) {
|
|
126
|
-
console.warn('No input from STDIN detected. Exiting...');
|
|
127
|
-
process.exit(0);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
let string = input;
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
string = JSON.parse(input);
|
|
134
|
-
} catch (e) {
|
|
135
|
-
// do nothing
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
convert(string);
|
|
139
|
-
})
|
|
140
|
-
.catch(function(e) {
|
|
141
|
-
debug(e);
|
|
142
|
-
console.error(
|
|
143
|
-
`${e.message}\nUnable to parse source from STDIN. Is it a valid JSON file?`
|
|
144
|
-
);
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
})(argv);
|
package/dist/cjs/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;Object.defineProperty(exports,"localize",{enumerable:true,get:function get(){return _localize.default}});var _localize=_interopRequireDefault(require("./localize.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}var pseudoLocalization=function(){var mutationObserverOpts={blacklistedNodeNames:["STYLE"]};var opts={strategy:"accented"};var enabled=false;var observer=null;var observerConfig={characterData:true,childList:true,subtree:true};var textNodesUnder=function textNodesUnder(element){var walker=document.createTreeWalker(element,NodeFilter.SHOW_TEXT,function(node){var isAllWhitespace=node.nodeValue&&!/[^\s]/.test(node.nodeValue);if(isAllWhitespace){return NodeFilter.FILTER_REJECT}var isBlacklistedNode=node.parentElement&&mutationObserverOpts.blacklistedNodeNames.includes(node.parentElement.nodeName);if(isBlacklistedNode){return NodeFilter.FILTER_REJECT}return NodeFilter.FILTER_ACCEPT});var currNode;var textNodes=[];while(currNode=walker.nextNode()){textNodes.push(currNode)}return textNodes};var isNonEmptyString=function isNonEmptyString(str){return!!str&&typeof str==="string"};var pseudoLocalize=function pseudoLocalize(element){var textNodesUnderElement=textNodesUnder(element);var _iteratorNormalCompletion=true;var _didIteratorError=false;var _iteratorError=undefined;try{for(var _iterator=textNodesUnderElement[Symbol.iterator](),_step;!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=true){var textNode=_step.value;var nodeValue=textNode.nodeValue;if(isNonEmptyString(nodeValue)){textNode.nodeValue=(0,_localize.default)(nodeValue,opts)}}}catch(err){_didIteratorError=true;_iteratorError=err}finally{try{if(!_iteratorNormalCompletion&&_iterator.return!=null){_iterator.return()}}finally{if(_didIteratorError){throw _iteratorError}}}};var domMutationCallback=function domMutationCallback(mutationsList){if(!observer){return}var _iteratorNormalCompletion2=true;var _didIteratorError2=false;var _iteratorError2=undefined;try{for(var _iterator2=mutationsList[Symbol.iterator](),_step2;!(_iteratorNormalCompletion2=(_step2=_iterator2.next()).done);_iteratorNormalCompletion2=true){var mutation=_step2.value;if(mutation.type==="childList"&&mutation.addedNodes.length>0){observer.disconnect();mutation.addedNodes.forEach(pseudoLocalize);observer.observe(document.body,observerConfig)}else if(mutation.type==="characterData"){var nodeValue=mutation.target.nodeValue;var isBlacklistedNode=!!mutation.target.parentElement&&mutationObserverOpts.blacklistedNodeNames.includes(mutation.target.parentElement.nodeName);if(isNonEmptyString(nodeValue)&&!isBlacklistedNode){observer.disconnect();mutation.target.nodeValue=(0,_localize.default)(nodeValue,opts);observer.observe(document.body,observerConfig)}}}}catch(err){_didIteratorError2=true;_iteratorError2=err}finally{try{if(!_iteratorNormalCompletion2&&_iterator2.return!=null){_iterator2.return()}}finally{if(_didIteratorError2){throw _iteratorError2}}}};var isEnabled=function isEnabled(){return enabled};var start=function start(){var _ref=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{},_ref$strategy=_ref.strategy,strategy=_ref$strategy===void 0?"accented":_ref$strategy,_ref$blacklistedNodeN=_ref.blacklistedNodeNames,blacklistedNodeNames=_ref$blacklistedNodeN===void 0?mutationObserverOpts.blacklistedNodeNames:_ref$blacklistedNodeN;if(isEnabled()){console.error("pseudo-localization is already enabled");return}mutationObserverOpts.blacklistedNodeNames=blacklistedNodeNames;opts.strategy=strategy;pseudoLocalize(document.body);observer=new MutationObserver(domMutationCallback);observer.observe(document.body,observerConfig);enabled=true};var stop=function stop(){if(!isEnabled()){console.error("pseudo-localization is already disabled");return}observer&&observer.disconnect();enabled=false};return{start:start,stop:stop,isEnabled:isEnabled,localize:_localize.default}}();var _default=pseudoLocalization;exports.default=_default;
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["pseudoLocalization","mutationObserverOpts","blacklistedNodeNames","opts","strategy","enabled","observer","observerConfig","characterData","childList","subtree","textNodesUnder","element","walker","document","createTreeWalker","NodeFilter","SHOW_TEXT","node","isAllWhitespace","nodeValue","test","FILTER_REJECT","isBlacklistedNode","parentElement","includes","nodeName","FILTER_ACCEPT","currNode","textNodes","nextNode","push","isNonEmptyString","str","pseudoLocalize","textNodesUnderElement","textNode","pseudoLocalizeString","domMutationCallback","mutationsList","mutation","type","addedNodes","length","disconnect","forEach","observe","body","target","isEnabled","start","console","error","MutationObserver","stop","localize"],"sources":["../../src/index.ts"],"sourcesContent":["import pseudoLocalizeString, {\n PseudoLocalizeStringOptions,\n} from './localize.js';\n\n/**\n * export the underlying pseudo localization function so this import style\n * import { localize } from 'pseudo-localization';\n * can be used.\n */\nexport { default as localize } from './localize.js';\n\ntype MutationObserverCallbackOptions = {\n blacklistedNodeNames?: string[];\n};\n\ntype StartOptions = MutationObserverCallbackOptions &\n PseudoLocalizeStringOptions;\n\nconst pseudoLocalization = (() => {\n const mutationObserverOpts = {\n blacklistedNodeNames: ['STYLE'],\n };\n\n const opts: PseudoLocalizeStringOptions = {\n strategy: 'accented',\n };\n\n // Indicates whether the pseudo-localization is currently enabled.\n let enabled = false;\n\n // Observer for dom updates. Initialization is defered to make parts\n // of the API safe to use in non-browser environments like nodejs\n let observer: MutationObserver | null = null;\n const observerConfig = {\n characterData: true,\n childList: true,\n subtree: true,\n };\n\n const textNodesUnder = (element: Node) => {\n const walker = document.createTreeWalker(\n element,\n NodeFilter.SHOW_TEXT,\n node => {\n const isAllWhitespace = node.nodeValue && !/[^\\s]/.test(node.nodeValue);\n if (isAllWhitespace) {\n return NodeFilter.FILTER_REJECT;\n }\n\n const isBlacklistedNode =\n node.parentElement &&\n mutationObserverOpts.blacklistedNodeNames.includes(\n node.parentElement.nodeName\n );\n if (isBlacklistedNode) {\n return NodeFilter.FILTER_REJECT;\n }\n\n return NodeFilter.FILTER_ACCEPT;\n }\n );\n\n let currNode;\n const textNodes = [];\n while ((currNode = walker.nextNode())) textNodes.push(currNode);\n return textNodes;\n };\n\n const isNonEmptyString = (str: unknown): str is string =>\n !!str && typeof str === 'string';\n\n const pseudoLocalize = (element: Node) => {\n const textNodesUnderElement = textNodesUnder(element);\n for (let textNode of textNodesUnderElement) {\n const nodeValue = textNode.nodeValue;\n if (isNonEmptyString(nodeValue)) {\n textNode.nodeValue = pseudoLocalizeString(nodeValue, opts);\n }\n }\n };\n\n const domMutationCallback: MutationCallback = mutationsList => {\n if (!observer) {\n return;\n }\n for (let mutation of mutationsList) {\n if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {\n // Turn the observer off while performing dom manipulation to prevent\n // infinite dom mutation callback loops\n observer.disconnect();\n // For every node added, recurse down it's subtree and convert\n // all children as well\n mutation.addedNodes.forEach(pseudoLocalize);\n observer.observe(document.body, observerConfig);\n } else if (mutation.type === 'characterData') {\n const nodeValue = mutation.target.nodeValue;\n const isBlacklistedNode =\n !!mutation.target.parentElement &&\n mutationObserverOpts.blacklistedNodeNames.includes(\n mutation.target.parentElement.nodeName\n );\n if (isNonEmptyString(nodeValue) && !isBlacklistedNode) {\n // Turn the observer off while performing dom manipulation to prevent\n // infinite dom mutation callback loops\n observer.disconnect();\n // The target will always be a text node so it can be converted\n // directly\n mutation.target.nodeValue = pseudoLocalizeString(nodeValue, opts);\n observer.observe(document.body, observerConfig);\n }\n }\n }\n };\n\n const isEnabled = () => {\n return enabled;\n };\n\n const start = ({\n strategy = 'accented',\n blacklistedNodeNames = mutationObserverOpts.blacklistedNodeNames,\n }: StartOptions = {}) => {\n if (isEnabled()) {\n console.error('pseudo-localization is already enabled');\n return;\n }\n\n mutationObserverOpts.blacklistedNodeNames = blacklistedNodeNames;\n opts.strategy = strategy;\n // Pseudo localize the DOM\n pseudoLocalize(document.body);\n // Start observing the DOM for changes and run\n // pseudo localization on any added text nodes\n observer = new MutationObserver(domMutationCallback);\n observer.observe(document.body, observerConfig);\n enabled = true;\n };\n\n const stop = () => {\n if (!isEnabled()) {\n console.error('pseudo-localization is already disabled');\n return;\n }\n\n observer && observer.disconnect();\n enabled = false;\n };\n\n return {\n start,\n stop,\n isEnabled,\n localize: pseud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AA3B,C,aA0IerC,kB"}
|
package/dist/cjs/localize.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var ACCENTED_MAP={a:"\u0227",A:"\u0226",b:"\u0180",B:"\u0181",c:"\u0188",C:"\u0187",d:"\u1E13",D:"\u1E12",e:"\u1E17",E:"\u1E16",f:"\u0192",F:"\u0191",g:"\u0260",G:"\u0193",h:"\u0127",H:"\u0126",i:"\u012B",I:"\u012A",j:"\u0135",J:"\u0134",k:"\u0137",K:"\u0136",l:"\u0140",L:"\u013F",m:"\u1E3F",M:"\u1E3E",n:"\u019E",N:"\u0220",o:"\u01FF",O:"\u01FE",p:"\u01A5",P:"\u01A4",q:"\u024B",Q:"\u024A",r:"\u0159",R:"\u0158",s:"\u015F",S:"\u015E",t:"\u0167",T:"\u0166",v:"\u1E7D",V:"\u1E7C",u:"\u016D",U:"\u016C",w:"\u1E87",W:"\u1E86",x:"\u1E8B",X:"\u1E8A",y:"\u1E8F",Y:"\u1E8E",z:"\u1E91",Z:"\u1E90"};var BIDI_MAP={a:"\u0250",A:"\u2200",b:"q",B:"\u0510",c:"\u0254",C:"\u2183",d:"p",D:"\u15E1",e:"\u01DD",E:"\u018E",f:"\u025F",F:"\u2132",g:"\u0183",G:"\u2141",h:"\u0265",H:"H",i:"\u0131",I:"I",j:"\u027E",J:"\u017F",k:"\u029E",K:"\u04FC",l:"\u0285",L:"\u2142",m:"\u026F",M:"W",n:"u",N:"N",o:"o",O:"O",p:"d",P:"\u0500",q:"b",Q:"\xD2",r:"\u0279",R:"\u1D1A",s:"s",S:"S",t:"\u0287",T:"\u22A5",u:"n",U:"\u2229",v:"\u028C",V:"\u0245",w:"\u028D",W:"M",x:"x",X:"X",y:"\u028E",Y:"\u2144",z:"z",Z:"Z"};var strategies={accented:{prefix:"",postfix:"",map:ACCENTED_MAP,elongate:true},bidi:{prefix:"\u202E",postfix:"\u202C",map:BIDI_MAP,elongate:false}};var pseudoLocalizeString=function pseudoLocalizeString(string){var _ref=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{},_ref$strategy=_ref.strategy,strategy=_ref$strategy===void 0?"accented":_ref$strategy;var opts=strategies[strategy];var pseudoLocalizedText="";var _iteratorNormalCompletion=true;var _didIteratorError=false;var _iteratorError=undefined;try{for(var _iterator=string[Symbol.iterator](),_step;!(_iteratorNormalCompletion=(_step=_iterator.next()).done);_iteratorNormalCompletion=true){var character=_step.value;if(character in opts.map){var char=character;var cl=char.toLowerCase();if(opts.elongate&&(cl==="a"||cl==="e"||cl==="o"||cl==="u")){pseudoLocalizedText+=opts.map[char]+opts.map[char]}else pseudoLocalizedText+=opts.map[char]}else pseudoLocalizedText+=character}}catch(err){_didIteratorError=true;_iteratorError=err}finally{try{if(!_iteratorNormalCompletion&&_iterator.return!=null){_iterator.return()}}finally{if(_didIteratorError){throw _iteratorError}}}if(pseudoLocalizedText.startsWith(opts.prefix)&&pseudoLocalizedText.endsWith(opts.postfix)){return pseudoLocalizedText}return opts.prefix+pseudoLocalizedText+opts.postfix};var _default=pseudoLocalizeString;exports.default=_default;
|
|
2
|
-
//# sourceMappingURL=localize.js.map
|
package/dist/cjs/localize.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"localize.js","names":["ACCENTED_MAP","a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J","k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T","v","V","u","U","w","W","x","X","y","Y","z","Z","BIDI_MAP","strategies","accented","prefix","postfix","map","elongate","bidi","pseudoLocalizeString","string","strategy","opts","pseudoLocalizedText","character","char","cl","toLowerCase","startsWith","endsWith"],"sources":["../../src/localize.ts"],"sourcesContent":["const ACCENTED_MAP = {\n a: \"ȧ\",\n A: \"Ȧ\",\n b: \"ƀ\",\n B: \"Ɓ\",\n c: \"ƈ\",\n C: \"Ƈ\",\n d: \"ḓ\",\n D: \"Ḓ\",\n e: \"ḗ\",\n E: \"Ḗ\",\n f: \"ƒ\",\n F: \"Ƒ\",\n g: \"ɠ\",\n G: \"Ɠ\",\n h: \"ħ\",\n H: \"Ħ\",\n i: \"ī\",\n I: \"Ī\",\n j: \"ĵ\",\n J: \"Ĵ\",\n k: \"ķ\",\n K: \"Ķ\",\n l: \"ŀ\",\n L: \"Ŀ\",\n m: \"ḿ\",\n M: \"Ḿ\",\n n: \"ƞ\",\n N: \"Ƞ\",\n o: \"ǿ\",\n O: \"Ǿ\",\n p: \"ƥ\",\n P: \"Ƥ\",\n q: \"ɋ\",\n Q: \"Ɋ\",\n r: \"ř\",\n R: \"Ř\",\n s: \"ş\",\n S: \"Ş\",\n t: \"ŧ\",\n T: \"Ŧ\",\n v: \"ṽ\",\n V: \"Ṽ\",\n u: \"ŭ\",\n U: \"Ŭ\",\n w: \"ẇ\",\n W: \"Ẇ\",\n x: \"ẋ\",\n X: \"Ẋ\",\n y: \"ẏ\",\n Y: \"Ẏ\",\n z: \"ẑ\",\n Z: \"Ẑ\"\n};\n\nconst BIDI_MAP = {\n a: \"ɐ\",\n A: \"∀\",\n b: \"q\",\n B: \"Ԑ\",\n c: \"ɔ\",\n C: \"Ↄ\",\n d: \"p\",\n D: \"ᗡ\",\n e: \"ǝ\",\n E: \"Ǝ\",\n f: \"ɟ\",\n F: \"Ⅎ\",\n g: \"ƃ\",\n G: \"⅁\",\n h: \"ɥ\",\n H: \"H\",\n i: \"ı\",\n I: \"I\",\n j: \"ɾ\",\n J: \"ſ\",\n k: \"ʞ\",\n K: \"Ӽ\",\n l: \"ʅ\",\n L: \"⅂\",\n m: \"ɯ\",\n M: \"W\",\n n: \"u\",\n N: \"N\",\n o: \"o\",\n O: \"O\",\n p: \"d\",\n P: \"Ԁ\",\n q: \"b\",\n Q: \"Ò\",\n r: \"ɹ\",\n R: \"ᴚ\",\n s: \"s\",\n S: \"S\",\n t: \"ʇ\",\n T: \"⊥\",\n u: \"n\",\n U: \"∩\",\n v: \"ʌ\",\n V: \"Ʌ\",\n w: \"ʍ\",\n W: \"M\",\n x: \"x\",\n X: \"X\",\n y: \"ʎ\",\n Y: \"⅄\",\n z: \"z\",\n Z: \"Z\"\n};\n\nconst strategies = {\n accented: {\n prefix: \"\",\n postfix: \"\",\n map: ACCENTED_MAP,\n elongate: true\n },\n bidi: {\n // Surround words with Unicode formatting marks forcing\n // right-to-left directionality of characters\n prefix: \"\\u202e\",\n postfix: \"\\u202c\",\n map: BIDI_MAP,\n elongate: false\n }\n};\n\nexport type Strategy = \"accented\" | \"bidi\";\nexport type PseudoLocalizeStringOptions = {\n strategy?: Strategy;\n};\n\nconst pseudoLocalizeString = (\n string: string,\n { strategy = \"accented\" }: PseudoLocalizeStringOptions = {}\n): string => {\n let opts = strategies[strategy];\n\n let pseudoLocalizedText = \"\";\n for (let character of string) {\n if (character in opts.map) {\n let char = character as keyof typeof opts.map;\n const cl = char.toLowerCase();\n // duplicate \"a\", \"e\", \"o\" and \"u\" to emulate ~30% longer text\n if (\n opts.elongate &&\n (cl === \"a\" || cl === \"e\" || cl === \"o\" || cl === \"u\")\n ) {\n pseudoLocalizedText += opts.map[char] + opts.map[char];\n } else pseudoLocalizedText += opts.map[char];\n } else pseudoLocalizedText += character;\n }\n\n // If this string is from the DOM, it should already contain the pre- and postfix\n if (\n pseudoLocalizedText.startsWith(opts.prefix) &&\n pseudoLocalizedText.endsWith(opts.postfix)\n ) {\n return pseudoLocalizedText;\n }\n return opts.prefix + pseudoLocalizedText + opts.postfix;\n};\n\nexport default pseudoLocalizeString;\n"],"mappings":"6FAAA,GAAMA,aAAY,CAAG,CACnBC,CAAC,CAAE,QADgB,CAEnBC,CAAC,CAAE,QAFgB,CAGnBC,CAAC,CAAE,QAHgB,CAInBC,CAAC,CAAE,QAJgB,CAKnBC,CAAC,CAAE,QALgB,CAMnBC,CAAC,CAAE,QANgB,CAOnBC,CAAC,CAAE,QAPgB,CAQnBC,CAAC,CAAE,QARgB,CASnBC,CAAC,CA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IAUOF,oBAAmB,EAAIC,SAC/B,CAhBU,kMAmBX,GACED,mBAAmB,CAACK,UAApB,CAA+BN,IAAI,CAACR,MAApC,GACAS,mBAAmB,CAACM,QAApB,CAA6BP,IAAI,CAACP,OAAlC,CAFF,CAGE,CACA,MAAOQ,oBACR,CACD,MAAOD,KAAI,CAACR,MAAL,CAAcS,mBAAd,CAAoCD,IAAI,CAACP,OACjD,CA7BD,C,aA+BeI,oB"}
|
package/dist/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { PseudoLocalizeStringOptions } from './localize.js';
|
|
2
|
-
/**
|
|
3
|
-
* export the underlying pseudo localization function so this import style
|
|
4
|
-
* import { localize } from 'pseudo-localization';
|
|
5
|
-
* can be used.
|
|
6
|
-
*/
|
|
7
|
-
export { default as localize } from './localize.js';
|
|
8
|
-
declare type MutationObserverCallbackOptions = {
|
|
9
|
-
blacklistedNodeNames?: string[];
|
|
10
|
-
};
|
|
11
|
-
declare type StartOptions = MutationObserverCallbackOptions & PseudoLocalizeStringOptions;
|
|
12
|
-
declare const pseudoLocalization: {
|
|
13
|
-
start: ({ strategy, blacklistedNodeNames, }?: StartOptions) => void;
|
|
14
|
-
stop: () => void;
|
|
15
|
-
isEnabled: () => boolean;
|
|
16
|
-
localize: (string: string, { strategy }?: PseudoLocalizeStringOptions) => string;
|
|
17
|
-
};
|
|
18
|
-
export default pseudoLocalization;
|
|
19
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAA6B,EAC3B,2BAA2B,EAC5B,MAAM,eAAe,CAAC;AAEvB;;;;GAIG;AACH,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEpD,aAAK,+BAA+B,GAAG;IACrC,oBAAoB,CAAC,EAAE,MAAM,EAAE,CAAC;CACjC,CAAC;AAEF,aAAK,YAAY,GAAG,+BAA+B,GACjD,2BAA2B,CAAC;AAE9B,QAAA,MAAM,kBAAkB;kDAuGnB,YAAY;;;;CAiCb,CAAC;AAEL,eAAe,kBAAkB,CAAC"}
|
package/dist/index.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import pseudoLocalizeString from './localize.js';
|
|
2
|
-
/**
|
|
3
|
-
* export the underlying pseudo localization function so this import style
|
|
4
|
-
* import { localize } from 'pseudo-localization';
|
|
5
|
-
* can be used.
|
|
6
|
-
*/
|
|
7
|
-
export { default as localize } from './localize.js';
|
|
8
|
-
const pseudoLocalization = (() => {
|
|
9
|
-
const mutationObserverOpts = {
|
|
10
|
-
blacklistedNodeNames: ['STYLE'],
|
|
11
|
-
};
|
|
12
|
-
const opts = {
|
|
13
|
-
strategy: 'accented',
|
|
14
|
-
};
|
|
15
|
-
// Indicates whether the pseudo-localization is currently enabled.
|
|
16
|
-
let enabled = false;
|
|
17
|
-
// Observer for dom updates. Initialization is defered to make parts
|
|
18
|
-
// of the API safe to use in non-browser environments like nodejs
|
|
19
|
-
let observer = null;
|
|
20
|
-
const observerConfig = {
|
|
21
|
-
characterData: true,
|
|
22
|
-
childList: true,
|
|
23
|
-
subtree: true,
|
|
24
|
-
};
|
|
25
|
-
const textNodesUnder = (element) => {
|
|
26
|
-
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, node => {
|
|
27
|
-
const isAllWhitespace = node.nodeValue && !/[^\s]/.test(node.nodeValue);
|
|
28
|
-
if (isAllWhitespace) {
|
|
29
|
-
return NodeFilter.FILTER_REJECT;
|
|
30
|
-
}
|
|
31
|
-
const isBlacklistedNode = node.parentElement &&
|
|
32
|
-
mutationObserverOpts.blacklistedNodeNames.includes(node.parentElement.nodeName);
|
|
33
|
-
if (isBlacklistedNode) {
|
|
34
|
-
return NodeFilter.FILTER_REJECT;
|
|
35
|
-
}
|
|
36
|
-
return NodeFilter.FILTER_ACCEPT;
|
|
37
|
-
});
|
|
38
|
-
let currNode;
|
|
39
|
-
const textNodes = [];
|
|
40
|
-
while ((currNode = walker.nextNode()))
|
|
41
|
-
textNodes.push(currNode);
|
|
42
|
-
return textNodes;
|
|
43
|
-
};
|
|
44
|
-
const isNonEmptyString = (str) => !!str && typeof str === 'string';
|
|
45
|
-
const pseudoLocalize = (element) => {
|
|
46
|
-
const textNodesUnderElement = textNodesUnder(element);
|
|
47
|
-
for (let textNode of textNodesUnderElement) {
|
|
48
|
-
const nodeValue = textNode.nodeValue;
|
|
49
|
-
if (isNonEmptyString(nodeValue)) {
|
|
50
|
-
textNode.nodeValue = pseudoLocalizeString(nodeValue, opts);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
const domMutationCallback = mutationsList => {
|
|
55
|
-
if (!observer) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
for (let mutation of mutationsList) {
|
|
59
|
-
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
|
|
60
|
-
// Turn the observer off while performing dom manipulation to prevent
|
|
61
|
-
// infinite dom mutation callback loops
|
|
62
|
-
observer.disconnect();
|
|
63
|
-
// For every node added, recurse down it's subtree and convert
|
|
64
|
-
// all children as well
|
|
65
|
-
mutation.addedNodes.forEach(pseudoLocalize);
|
|
66
|
-
observer.observe(document.body, observerConfig);
|
|
67
|
-
}
|
|
68
|
-
else if (mutation.type === 'characterData') {
|
|
69
|
-
const nodeValue = mutation.target.nodeValue;
|
|
70
|
-
const isBlacklistedNode = !!mutation.target.parentElement &&
|
|
71
|
-
mutationObserverOpts.blacklistedNodeNames.includes(mutation.target.parentElement.nodeName);
|
|
72
|
-
if (isNonEmptyString(nodeValue) && !isBlacklistedNode) {
|
|
73
|
-
// Turn the observer off while performing dom manipulation to prevent
|
|
74
|
-
// infinite dom mutation callback loops
|
|
75
|
-
observer.disconnect();
|
|
76
|
-
// The target will always be a text node so it can be converted
|
|
77
|
-
// directly
|
|
78
|
-
mutation.target.nodeValue = pseudoLocalizeString(nodeValue, opts);
|
|
79
|
-
observer.observe(document.body, observerConfig);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
const isEnabled = () => {
|
|
85
|
-
return enabled;
|
|
86
|
-
};
|
|
87
|
-
const start = ({ strategy = 'accented', blacklistedNodeNames = mutationObserverOpts.blacklistedNodeNames, } = {}) => {
|
|
88
|
-
if (isEnabled()) {
|
|
89
|
-
console.error('pseudo-localization is already enabled');
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
mutationObserverOpts.blacklistedNodeNames = blacklistedNodeNames;
|
|
93
|
-
opts.strategy = strategy;
|
|
94
|
-
// Pseudo localize the DOM
|
|
95
|
-
pseudoLocalize(document.body);
|
|
96
|
-
// Start observing the DOM for changes and run
|
|
97
|
-
// pseudo localization on any added text nodes
|
|
98
|
-
observer = new MutationObserver(domMutationCallback);
|
|
99
|
-
observer.observe(document.body, observerConfig);
|
|
100
|
-
enabled = true;
|
|
101
|
-
};
|
|
102
|
-
const stop = () => {
|
|
103
|
-
if (!isEnabled()) {
|
|
104
|
-
console.error('pseudo-localization is already disabled');
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
observer && observer.disconnect();
|
|
108
|
-
enabled = false;
|
|
109
|
-
};
|
|
110
|
-
return {
|
|
111
|
-
start,
|
|
112
|
-
stop,
|
|
113
|
-
isEnabled,
|
|
114
|
-
localize: pseudoLocalizeString,
|
|
115
|
-
};
|
|
116
|
-
})();
|
|
117
|
-
export default pseudoLocalization;
|
package/dist/localize.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export declare type Strategy = "accented" | "bidi";
|
|
2
|
-
export declare type PseudoLocalizeStringOptions = {
|
|
3
|
-
strategy?: Strategy;
|
|
4
|
-
};
|
|
5
|
-
declare const pseudoLocalizeString: (string: string, { strategy }?: PseudoLocalizeStringOptions) => string;
|
|
6
|
-
export default pseudoLocalizeString;
|
|
7
|
-
//# sourceMappingURL=localize.d.ts.map
|
package/dist/localize.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"localize.d.ts","sourceRoot":"","sources":["../src/localize.ts"],"names":[],"mappings":"AA+HA,oBAAY,QAAQ,GAAG,UAAU,GAAG,MAAM,CAAC;AAC3C,oBAAY,2BAA2B,GAAG;IACxC,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACrB,CAAC;AAEF,QAAA,MAAM,oBAAoB,WAChB,MAAM,iBACa,2BAA2B,KACrD,MA0BF,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
|
package/dist/localize.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
const ACCENTED_MAP = {
|
|
2
|
-
a: "ȧ",
|
|
3
|
-
A: "Ȧ",
|
|
4
|
-
b: "ƀ",
|
|
5
|
-
B: "Ɓ",
|
|
6
|
-
c: "ƈ",
|
|
7
|
-
C: "Ƈ",
|
|
8
|
-
d: "ḓ",
|
|
9
|
-
D: "Ḓ",
|
|
10
|
-
e: "ḗ",
|
|
11
|
-
E: "Ḗ",
|
|
12
|
-
f: "ƒ",
|
|
13
|
-
F: "Ƒ",
|
|
14
|
-
g: "ɠ",
|
|
15
|
-
G: "Ɠ",
|
|
16
|
-
h: "ħ",
|
|
17
|
-
H: "Ħ",
|
|
18
|
-
i: "ī",
|
|
19
|
-
I: "Ī",
|
|
20
|
-
j: "ĵ",
|
|
21
|
-
J: "Ĵ",
|
|
22
|
-
k: "ķ",
|
|
23
|
-
K: "Ķ",
|
|
24
|
-
l: "ŀ",
|
|
25
|
-
L: "Ŀ",
|
|
26
|
-
m: "ḿ",
|
|
27
|
-
M: "Ḿ",
|
|
28
|
-
n: "ƞ",
|
|
29
|
-
N: "Ƞ",
|
|
30
|
-
o: "ǿ",
|
|
31
|
-
O: "Ǿ",
|
|
32
|
-
p: "ƥ",
|
|
33
|
-
P: "Ƥ",
|
|
34
|
-
q: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
90
|
-
r: "ɹ",
|
|
91
|
-
R: "ᴚ",
|
|
92
|
-
s: "s",
|
|
93
|
-
S: "S",
|
|
94
|
-
t: "ʇ",
|
|
95
|
-
T: "⊥",
|
|
96
|
-
u: "n",
|
|
97
|
-
U: "∩",
|
|
98
|
-
v: "ʌ",
|
|
99
|
-
V: "Ʌ",
|
|
100
|
-
w: "ʍ",
|
|
101
|
-
W: "M",
|
|
102
|
-
x: "x",
|
|
103
|
-
X: "X",
|
|
104
|
-
y: "ʎ",
|
|
105
|
-
Y: "⅄",
|
|
106
|
-
z: "z",
|
|
107
|
-
Z: "Z"
|
|
108
|
-
};
|
|
109
|
-
const strategies = {
|
|
110
|
-
accented: {
|
|
111
|
-
prefix: "",
|
|
112
|
-
postfix: "",
|
|
113
|
-
map: ACCENTED_MAP,
|
|
114
|
-
elongate: true
|
|
115
|
-
},
|
|
116
|
-
bidi: {
|
|
117
|
-
// Surround words with Unicode formatting marks forcing
|
|
118
|
-
// right-to-left directionality of characters
|
|
119
|
-
prefix: "\u202e",
|
|
120
|
-
postfix: "\u202c",
|
|
121
|
-
map: BIDI_MAP,
|
|
122
|
-
elongate: false
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
const pseudoLocalizeString = (string, { strategy = "accented" } = {}) => {
|
|
126
|
-
let opts = strategies[strategy];
|
|
127
|
-
let pseudoLocalizedText = "";
|
|
128
|
-
for (let character of string) {
|
|
129
|
-
if (character in opts.map) {
|
|
130
|
-
let char = character;
|
|
131
|
-
const cl = char.toLowerCase();
|
|
132
|
-
// duplicate "a", "e", "o" and "u" to emulate ~30% longer text
|
|
133
|
-
if (opts.elongate &&
|
|
134
|
-
(cl === "a" || cl === "e" || cl === "o" || cl === "u")) {
|
|
135
|
-
pseudoLocalizedText += opts.map[char] + opts.map[char];
|
|
136
|
-
}
|
|
137
|
-
else
|
|
138
|
-
pseudoLocalizedText += opts.map[char];
|
|
139
|
-
}
|
|
140
|
-
else
|
|
141
|
-
pseudoLocalizedText += character;
|
|
142
|
-
}
|
|
143
|
-
// If this string is from the DOM, it should already contain the pre- and postfix
|
|
144
|
-
if (pseudoLocalizedText.startsWith(opts.prefix) &&
|
|
145
|
-
pseudoLocalizedText.endsWith(opts.postfix)) {
|
|
146
|
-
return pseudoLocalizedText;
|
|
147
|
-
}
|
|
148
|
-
return opts.prefix + pseudoLocalizedText + opts.postfix;
|
|
149
|
-
};
|
|
150
|
-
export default pseudoLocalizeString;
|