aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Coleman <coleman.ian@gmail.com>2016-11-29 16:07:23 +1100
committerIan Coleman <coleman.ian@gmail.com>2016-11-29 16:21:45 +1100
commitc3c3df473e0e5114b75dbe835ada59a0e8066543 (patch)
treeecd6d6fe4f5f0795fec9418b82e6bae973dfe74a
parent60599f7ae9f59d6f0abadee1e0dd952ce4019d3d (diff)
downloadBIP39-c3c3df473e0e5114b75dbe835ada59a0e8066543.tar.gz
BIP39-c3c3df473e0e5114b75dbe835ada59a0e8066543.tar.zst
BIP39-c3c3df473e0e5114b75dbe835ada59a0e8066543.zip
Translation library does basic language switching
-rw-r--r--src/index.html2
-rw-r--r--src/js/phrases_en.js94
-rw-r--r--src/js/translate.js140
3 files changed, 236 insertions, 0 deletions
diff --git a/src/index.html b/src/index.html
index 9cb39ea..860e9d0 100644
--- a/src/index.html
+++ b/src/index.html
@@ -604,6 +604,8 @@
604 <script src="js/bitcoinjs-1-5-7.js"></script> 604 <script src="js/bitcoinjs-1-5-7.js"></script>
605 <script src="js/bitcoinjs-extensions.js"></script> 605 <script src="js/bitcoinjs-extensions.js"></script>
606 <script src="js/sjcl-bip39.js"></script> 606 <script src="js/sjcl-bip39.js"></script>
607 <script src="js/translate.js"></script>
608 <script src="js/phrases_en.js"></script>
607 <script src="js/wordlist_english.js"></script> 609 <script src="js/wordlist_english.js"></script>
608 <script src="js/wordlist_japanese.js"></script> 610 <script src="js/wordlist_japanese.js"></script>
609 <script src="js/wordlist_spanish.js"></script> 611 <script src="js/wordlist_spanish.js"></script>
diff --git a/src/js/phrases_en.js b/src/js/phrases_en.js
new file mode 100644
index 0000000..7181544
--- /dev/null
+++ b/src/js/phrases_en.js
@@ -0,0 +1,94 @@
1Translate.loadForeignPhrases("en",
2{
3 "<code>m/44'/0'/0'/0</code> generates public addresses": "<code>m/44'/0'/0'/0</code> generates public addresses",
4 "<code>m/44'/0'/0'/1</code> generates change addresses": "<code>m/44'/0'/0'/1</code> generates change addresses",
5 "<code>m/44'/0'/0'</code> generates extended keys for import / export": "<code>m/44'/0'/0'</code> generates extended keys for import / export",
6 "<code>m/44'/0'/1'</code> for the next account. Continue incrementing for more accounts (most use a single account).": "<code>m/44'/0'/1'</code> for the next account. Continue incrementing for more accounts (most use a single account).",
7 "Accepts binary, base 6, 6-sided dice, base 10, hexadecimal, cards": "Accepts binary, base 6, 6-sided dice, base 10, hexadecimal, cards",
8 "Account": "Account",
9 "Address": "Address",
10 "Alternatively, download the file from the repository": "Alternatively, download the file from the repository",
11 "BIP32 Derivation Path": "BIP32 Derivation Path",
12 "BIP32 Extended Key": "BIP32 Extended Key",
13 "BIP32 Extended Key (addresses only)": "BIP32 Extended Key (addresses only)",
14 "BIP32 Root Key": "BIP32 Root Key",
15 "BIP39 Mnemonic": "BIP39 Mnemonic",
16 "BIP39 Passphrase (optional)": "BIP39 Passphrase (optional)",
17 "BIP39 Seed": "BIP39 Seed",
18 "Base 10": "Base 10",
19 "Base 6": "Base 6",
20 "Be careful - it can be easy to make mistakes if you don't know what you're doing.": "Be careful - it can be easy to make mistakes if you don't know what you're doing.",
21 "Binary": "Binary",
22 "Bitcoin Core": "Bitcoin Core",
23 "Bits Per Event": "Bits Per Event",
24 "Block Explorers": "Block Explorers",
25 "Can be used with": "Can be used with",
26 "Card": "Card",
27 "Coin": "Coin",
28 "Dice": "Dice",
29 "Do <strong>NOT</strong> use phrases from books, lyrics from songs, your birthday or steet address, keyboard mashing, or anything you <i>think</i> is random, because chances are overwhelming it isn't random enough for the needs of this tool.": "Do <strong>NOT</strong> use phrases from books, lyrics from songs, your birthday or steet address, keyboard mashing, or anything you <i>think</i> is random, because chances are overwhelming it isn't random enough for the needs of this tool.",
30 "Double-click that file to open it in a browser on any offline computer.": "Double-click that file to open it in a browser on any offline computer.",
31 "Entropy": "Entropy",
32 "Entropy Type": "Entropy Type",
33 "Entropy is an advanced feature. Your mnemonic may be insecure if this feature is used incorrectly.": "Entropy is an advanced feature. Your mnemonic may be insecure if this feature is used incorrectly.",
34 "Entropy values must be sourced from a <a href=\"https://en.wikipedia.org/wiki/Random_number_generation\" target=\"_blank\">strong source of randomness</a>.": "Entropy values must be sourced from a <a href=\"https://en.wikipedia.org/wiki/Random_number_generation\" target=\"_blank\">strong source of randomness</a>.",
35 "Event Count": "Event Count",
36 "External / Internal": "External / Internal",
37 "Filtered Entropy": "Filtered Entropy",
38 "For more info see the <a href=\"http://www.mycelium.com/\" target=\"_blank\">Mycelium Wallet homepage</a>": "For more info see the <a href=\"http://www.mycelium.com/\" target=\"_blank\">Mycelium Wallet homepage</a>",
39 "For more info see the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki\" target=\"_blank\">BIP39 spec</a>.": "For more info see the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki\" target=\"_blank\">BIP39 spec</a>.",
40 "For more info see the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki\" target=\"_blank\">BIP44 spec</a>.": "For more info see the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki\" target=\"_blank\">BIP44 spec</a>.",
41 "For more info see the <a href=\"https://github.com/bitcoin/bitcoin/pull/8035\" target=\"_blank\">Bitcoin Core BIP32 implementation</a>": "For more info see the <a href=\"https://github.com/bitcoin/bitcoin/pull/8035\" target=\"_blank\">Bitcoin Core BIP32 implementation</a>",
42 "For more info see the BIP32 spec": "For more info see the BIP32 spec",
43 "From entropy length (3 words per 32 bits)": "From entropy length (3 words per 32 bits)",
44 "Generate a random mnemonic, or enter your own below": "Generate a random mnemonic, or enter your own below",
45 "Get the source code from the repository": "Get the source code from the repository",
46 "Hex": "Hex",
47 "Hierarchical Deterministic Wallets": "Hierarchical Deterministic Wallets",
48 "If cryptographic randomness isn't available in your browser, this page will show a warning and the generate button will not work.": "If cryptographic randomness isn't available in your browser, this page will show a warning and the generate button will not work.",
49 "In that case you might choose to use your own source of entropy.": "In that case you might choose to use your own source of entropy.",
50 "In your browser, select file save-as, and save this page as a file.": "In your browser, select file save-as, and save this page as a file.",
51 "Libraries": "Libraries",
52 "Mnemonic Language": "Mnemonic Language",
53 "Mnemonic Length": "Mnemonic Length",
54 "Mnemonic code for generating deterministic keys": "Mnemonic code for generating deterministic keys",
55 "More info": "More info",
56 "Multi-Account Hierarchy for Deterministic Wallets": "Multi-Account Hierarchy for Deterministic Wallets",
57 "Mycelium Wallet": "Mycelium Wallet",
58 "Note these addreses are derived from the BIP32 Extended Key": "Note these addreses are derived from the BIP32 Extended Key",
59 "Offline Usage": "Offline Usage",
60 "Only enter the <code>xpub</code> extended key into block explorer search fields, never the <code>xprv</code> key.": "Only enter the <code>xpub</code> extended key into block explorer search fields, never the <code>xprv</code> key.",
61 "Path": "Path",
62 "Private Key": "Private Key",
63 "Private Keys": "Private Keys",
64 "Public Key": "Public Key",
65 "Purpose": "Purpose",
66 "Raw Binary": "Raw Binary",
67 "Read more": "Read more",
68 "Read more at the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki\" target=\"_blank\">official BIP32 spec</a>": "Read more at the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki\" target=\"_blank\">official BIP32 spec</a>",
69 "Read more at the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki\">official BIP39 spec</a>": "Read more at the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki\">official BIP39 spec</a>",
70 "Read more at the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki\" target=\"_blank\">official BIP44 spec</a>": "Read more at the <a href=\"https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki\" target=\"_blank\">official BIP44 spec</a>",
71 "See the demo at <a href=\"http://bip32.org/\" target=\"_blank\">bip32.org</a>": "See the demo at <a href=\"http://bip32.org/\" target=\"_blank\">bip32.org</a>",
72 "Show": "Show",
73 "Show more rows": "Show more rows",
74 "Strength": "Strength",
75 "Supply my own source of entropy": "Supply my own source of entropy",
76 "The built in random generator can generally be trusted more than your own intuition about randomness.": "The built in random generator can generally be trusted more than your own intuition about randomness.",
77 "The random mnemonic generator on this page uses a <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues\" target=\"_blank\">cryptographically secure random number generator</a>.": "The random mnemonic generator on this page uses a <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues\" target=\"_blank\">cryptographically secure random number generator</a>.",
78 "This means flipping a fair coin, rolling a fair dice, noise measurements etc.": "This means flipping a fair coin, rolling a fair dice, noise measurements etc.",
79 "This project is 100% open-source code": "This project is 100% open-source code",
80 "Toggle": "Toggle",
81 "Total Bits": "Total Bits",
82 "Use hardened addresses": "Use hardened addresses",
83 "Use path <code>m/0'/0'</code> with hardened addresses.": "Use path <code>m/0'/0'</code> with hardened addresses.",
84 "Use path <code>m/44'/0'/0'</code>.": "Use path <code>m/44'/0'/0'</code>.",
85 "Use private keys at <a href=\"https://web.archive.org/web/20150707020924/https://brainwallet.org/\" target=\"_blank\">brainwallet.org</a>.": "Use private keys at <a href=\"https://web.archive.org/web/20150707020924/https://brainwallet.org/\" target=\"_blank\">brainwallet.org</a>.",
86 "Valid entropy values include:": "Valid entropy values include:",
87 "Warning": "Warning",
88 "Word Count": "Word Count",
89 "You are not a good source of entropy.": "You are not a good source of entropy.",
90 "You can enter an existing BIP39 mnemonic, or generate a new random one. Typing your own twelve words will probably not work how you expect, since the words require a particular structure (the last word is a checksum).": "You can enter an existing BIP39 mnemonic, or generate a new random one. Typing your own twelve words will probably not work how you expect, since the words require a particular structure (the last word is a checksum).",
91 "You can use this tool without having to be online.": "You can use this tool without having to be online.",
92 "words": "words"
93}
94);
diff --git a/src/js/translate.js b/src/js/translate.js
new file mode 100644
index 0000000..89512df
--- /dev/null
+++ b/src/js/translate.js
@@ -0,0 +1,140 @@
1// An extremely basic translation library
2//
3// Example usage:
4//
5// Set some html to be translated. Do this using the 'data-translate' attribute:
6//
7// <div data-translate>Test</div>
8// <div data-translate-html><em>keep em tag</em></div>
9// <input data-translate-placeholder placeholder="Example text">
10//
11// Obtain all the phrases to be translated via js debug console:
12//
13// Translate.phrasesAsJson();
14//
15// Use that template to translate the phrases into another language.
16// Leave the key the same. Change the value to the new language.
17//
18// Create a js file to load the new phrases. In this example for Spanish,
19// es.js will contain the following code:
20//
21// Translate.loadForeignPhrases("es", {
22// "Test": "Test in Spanish",
23// "<em>keep em tag</em>": "<em>keep em tag in Spanish</em>",
24// "Example text": "Example text in Spanish"
25// });
26//
27// In your UI put a listener for clicking on the Spanish button:
28//
29// mySpanishButton.addEventListener("click", function() {
30// Translate.setLanguage("es");
31// });
32// myEnglishButton.addEventListener("click", function() {
33// Translate.setLanguage("en");
34// });
35
36Translate = new (function() {
37
38 var defaultLanguage = "en";
39
40 var allPhrases = {};
41 allPhrases[defaultLanguage] = {};
42
43 // Node types
44
45 var text = {
46 selector: "[data-translate]",
47 getKey: function() {
48 return this.textContent.trim().replace(/\s+/g, " ");
49 },
50 setPhrase: function(p) {
51 this.textContent = p;
52 },
53 }
54
55 var html = {
56 selector: "[data-translate-html]",
57 getKey: function() {
58 return this.innerHTML.trim().replace(/\s+/g, " ");
59 },
60 setPhrase: function(p) {
61 this.innerHTML = p;
62 },
63 }
64
65 var placeholder = {
66 selector: "[data-translate-placeholder]",
67 getKey: function() {
68 return this.getAttribute("placeholder").trim().replace(/\s+/g, " ");
69 },
70 setPhrase: function(p) {
71 this.setAttribute("placeholder", p);
72 },
73 }
74
75 // Get elements to be translated
76 var allEls = getEls(text)
77 .concat(getEls(html))
78 .concat(getEls(placeholder));
79
80 // Provides access to phrases from a non-default language.
81 // See phrases_en.js for example usage.
82 this.loadForeignPhrases = function(language, phrases) {
83 allPhrases[language] = phrases;
84 }
85
86 // Displays a different language, eg "en" or "fr"
87 this.setLanguage = function(language) {
88 for (var i=0; i<allEls.length; i++) {
89 var el = allEls[i];
90 var key = el.key;
91 if (!(language in allPhrases)) {
92 console.log(language + " not in allPhrases");
93 return;
94 }
95 if (!(key in allPhrases[language])) {
96 console.log(language + " does not contain phrase: " + key);
97 return;
98 }
99 var phrase = allPhrases[language][key];
100 el.setPhrase(phrase);
101 }
102 }
103
104 // Converts the phrases to a key-pair json file.
105 // This is a good way to export phrases for use in translation tools.
106 this.phrasesAsJson = function(language) {
107 var keys = [];
108 for (var i=0; i<allEls.length; i++) {
109 var el = allEls[i];
110 var key = el.key;
111 keys.push(key);
112 }
113 keys.sort();
114 var output = {};
115 for (var i=0; i<keys.length; i++) {
116 var key = keys[i];
117 var translated = "";
118 if (language in allPhrases && key in allPhrases[language]) {
119 translated = allPhrases[language][key];
120 }
121 output[key] = translated;
122 }
123 return JSON.stringify(output, null, 2);
124 }
125
126 function getEls(nodeType) {
127 var nodes = document.querySelectorAll(nodeType.selector);
128 var els = [];
129 for (var i=0; i<nodes.length; i++) {
130 var node = nodes[i];
131 node.getKey = nodeType.getKey;
132 node.setPhrase = nodeType.setPhrase;
133 node.key = node.getKey();
134 allPhrases[defaultLanguage][node.key] = node.key;
135 els.push(node);
136 }
137 return els;
138 }
139
140})();