]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/commitdiff
Translation library does basic language switching
authorIan Coleman <coleman.ian@gmail.com>
Tue, 29 Nov 2016 05:07:23 +0000 (16:07 +1100)
committerIan Coleman <coleman.ian@gmail.com>
Tue, 29 Nov 2016 05:21:45 +0000 (16:21 +1100)
src/index.html
src/js/phrases_en.js [new file with mode: 0644]
src/js/translate.js [new file with mode: 0644]

index 9cb39ea1288122536203e036dbde965968287318..860e9d044bb6a39a9c0642322c076c981aae7437 100644 (file)
         <script src="js/bitcoinjs-1-5-7.js"></script>
         <script src="js/bitcoinjs-extensions.js"></script>
         <script src="js/sjcl-bip39.js"></script>
+        <script src="js/translate.js"></script>
+        <script src="js/phrases_en.js"></script>
         <script src="js/wordlist_english.js"></script>
         <script src="js/wordlist_japanese.js"></script>
         <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 (file)
index 0000000..7181544
--- /dev/null
@@ -0,0 +1,94 @@
+Translate.loadForeignPhrases("en",
+{
+  "<code>m/44'/0'/0'/0</code> generates public addresses": "<code>m/44'/0'/0'/0</code> generates public addresses",
+  "<code>m/44'/0'/0'/1</code> generates change addresses": "<code>m/44'/0'/0'/1</code> generates change addresses",
+  "<code>m/44'/0'/0'</code> generates extended keys for import / export": "<code>m/44'/0'/0'</code> generates extended keys for import / export",
+  "<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).",
+  "Accepts binary, base 6, 6-sided dice, base 10, hexadecimal, cards": "Accepts binary, base 6, 6-sided dice, base 10, hexadecimal, cards",
+  "Account": "Account",
+  "Address": "Address",
+  "Alternatively, download the file from the repository": "Alternatively, download the file from the repository",
+  "BIP32 Derivation Path": "BIP32 Derivation Path",
+  "BIP32 Extended Key": "BIP32 Extended Key",
+  "BIP32 Extended Key (addresses only)": "BIP32 Extended Key (addresses only)",
+  "BIP32 Root Key": "BIP32 Root Key",
+  "BIP39 Mnemonic": "BIP39 Mnemonic",
+  "BIP39 Passphrase (optional)": "BIP39 Passphrase (optional)",
+  "BIP39 Seed": "BIP39 Seed",
+  "Base 10": "Base 10",
+  "Base 6": "Base 6",
+  "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.",
+  "Binary": "Binary",
+  "Bitcoin Core": "Bitcoin Core",
+  "Bits Per Event": "Bits Per Event",
+  "Block Explorers": "Block Explorers",
+  "Can be used with": "Can be used with",
+  "Card": "Card",
+  "Coin": "Coin",
+  "Dice": "Dice",
+  "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.",
+  "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.",
+  "Entropy": "Entropy",
+  "Entropy Type": "Entropy Type",
+  "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.",
+  "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>.",
+  "Event Count": "Event Count",
+  "External / Internal": "External / Internal",
+  "Filtered Entropy": "Filtered Entropy",
+  "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>",
+  "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>.",
+  "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>.",
+  "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>",
+  "For more info see the BIP32 spec": "For more info see the BIP32 spec",
+  "From entropy length (3 words per 32 bits)": "From entropy length (3 words per 32 bits)",
+  "Generate a random mnemonic, or enter your own below": "Generate a random mnemonic, or enter your own below",
+  "Get the source code from the repository": "Get the source code from the repository",
+  "Hex": "Hex",
+  "Hierarchical Deterministic Wallets": "Hierarchical Deterministic Wallets",
+  "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.",
+  "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.",
+  "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.",
+  "Libraries": "Libraries",
+  "Mnemonic Language": "Mnemonic Language",
+  "Mnemonic Length": "Mnemonic Length",
+  "Mnemonic code for generating deterministic keys": "Mnemonic code for generating deterministic keys",
+  "More info": "More info",
+  "Multi-Account Hierarchy for Deterministic Wallets": "Multi-Account Hierarchy for Deterministic Wallets",
+  "Mycelium Wallet": "Mycelium Wallet",
+  "Note these addreses are derived from the BIP32 Extended Key": "Note these addreses are derived from the BIP32 Extended Key",
+  "Offline Usage": "Offline Usage",
+  "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.",
+  "Path": "Path",
+  "Private Key": "Private Key",
+  "Private Keys": "Private Keys",
+  "Public Key": "Public Key",
+  "Purpose": "Purpose",
+  "Raw Binary": "Raw Binary",
+  "Read more": "Read more",
+  "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>",
+  "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>",
+  "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>",
+  "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>",
+  "Show": "Show",
+  "Show more rows": "Show more rows",
+  "Strength": "Strength",
+  "Supply my own source of entropy": "Supply my own source of entropy",
+  "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.",
+  "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>.",
+  "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.",
+  "This project is 100% open-source code": "This project is 100% open-source code",
+  "Toggle": "Toggle",
+  "Total Bits": "Total Bits",
+  "Use hardened addresses": "Use hardened addresses",
+  "Use path <code>m/0'/0'</code> with hardened addresses.": "Use path <code>m/0'/0'</code> with hardened addresses.",
+  "Use path <code>m/44'/0'/0'</code>.": "Use path <code>m/44'/0'/0'</code>.",
+  "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>.",
+  "Valid entropy values include:": "Valid entropy values include:",
+  "Warning": "Warning",
+  "Word Count": "Word Count",
+  "You are not a good source of entropy.": "You are not a good source of entropy.",
+  "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).",
+  "You can use this tool without having to be online.": "You can use this tool without having to be online.",
+  "words": "words"
+}
+);
diff --git a/src/js/translate.js b/src/js/translate.js
new file mode 100644 (file)
index 0000000..89512df
--- /dev/null
@@ -0,0 +1,140 @@
+// An extremely basic translation library
+//
+// Example usage:
+//
+// Set some html to be translated. Do this using the 'data-translate' attribute:
+//
+//   <div data-translate>Test</div>
+//   <div data-translate-html><em>keep em tag</em></div>
+//   <input data-translate-placeholder placeholder="Example text">
+//
+// Obtain all the phrases to be translated via js debug console:
+//
+//   Translate.phrasesAsJson();
+//
+// Use that template to translate the phrases into another language.
+// Leave the key the same. Change the value to the new language.
+//
+// Create a js file to load the new phrases. In this example for Spanish,
+// es.js will contain the following code:
+//
+//   Translate.loadForeignPhrases("es", {
+//       "Test": "Test in Spanish",
+//       "<em>keep em tag</em>": "<em>keep em tag in Spanish</em>",
+//       "Example text": "Example text in Spanish"
+//   });
+//
+// In your UI put a listener for clicking on the Spanish button:
+//
+//   mySpanishButton.addEventListener("click", function() {
+//       Translate.setLanguage("es");
+//   });
+//   myEnglishButton.addEventListener("click", function() {
+//       Translate.setLanguage("en");
+//   });
+
+Translate = new (function() {
+
+    var defaultLanguage = "en";
+
+    var allPhrases = {};
+    allPhrases[defaultLanguage] = {};
+
+    // Node types
+
+    var text = {
+        selector: "[data-translate]",
+        getKey: function() {
+            return this.textContent.trim().replace(/\s+/g, " ");
+        },
+        setPhrase: function(p) {
+            this.textContent = p;
+        },
+    }
+
+    var html = {
+        selector: "[data-translate-html]",
+        getKey: function() {
+            return this.innerHTML.trim().replace(/\s+/g, " ");
+        },
+        setPhrase: function(p) {
+            this.innerHTML = p;
+        },
+    }
+
+    var placeholder = {
+        selector: "[data-translate-placeholder]",
+        getKey: function() {
+            return this.getAttribute("placeholder").trim().replace(/\s+/g, " ");
+        },
+        setPhrase: function(p) {
+            this.setAttribute("placeholder", p);
+        },
+    }
+
+    // Get elements to be translated
+    var allEls = getEls(text)
+        .concat(getEls(html))
+        .concat(getEls(placeholder));
+
+    // Provides access to phrases from a non-default language.
+    // See phrases_en.js for example usage.
+    this.loadForeignPhrases = function(language, phrases) {
+        allPhrases[language] =  phrases;
+    }
+
+    // Displays a different language, eg "en" or "fr"
+    this.setLanguage = function(language) {
+        for (var i=0; i<allEls.length; i++) {
+            var el = allEls[i];
+            var key = el.key;
+            if (!(language in allPhrases)) {
+                console.log(language + " not in allPhrases");
+                return;
+            }
+            if (!(key in allPhrases[language])) {
+                console.log(language + " does not contain phrase: " + key);
+                return;
+            }
+            var phrase = allPhrases[language][key];
+            el.setPhrase(phrase);
+        }
+    }
+
+    // Converts the phrases to a key-pair json file.
+    // This is a good way to export phrases for use in translation tools.
+    this.phrasesAsJson = function(language) {
+        var keys = [];
+        for (var i=0; i<allEls.length; i++) {
+            var el = allEls[i];
+            var key = el.key;
+            keys.push(key);
+        }
+        keys.sort();
+        var output = {};
+        for (var i=0; i<keys.length; i++) {
+            var key = keys[i];
+            var translated = "";
+            if (language in allPhrases && key in allPhrases[language]) {
+                translated = allPhrases[language][key];
+            }
+            output[key] = translated;
+        }
+        return JSON.stringify(output, null, 2);
+    }
+
+    function getEls(nodeType) {
+        var nodes = document.querySelectorAll(nodeType.selector);
+        var els = [];
+        for (var i=0; i<nodes.length; i++) {
+            var node = nodes[i];
+            node.getKey = nodeType.getKey;
+            node.setPhrase = nodeType.setPhrase;
+            node.key = node.getKey();
+            allPhrases[defaultLanguage][node.key] = node.key;
+            els.push(node);
+        }
+        return els;
+    }
+
+})();