From 2e11e7b6da2462f9ad253dffa7e3a6d8115239c9 Mon Sep 17 00:00:00 2001 From: "R. Mark Volkmann" Date: Thu, 4 Aug 2016 10:08:46 -0500 Subject: [PATCH 01/12] added support for using key if no translation is found --- app-localize-behavior.html | 29 ++++++++++++++++++++++------- demo/index.html | 2 +- demo/x-translate.html | 1 + test/basic.html | 18 ++++++++++++++++++ test/x-translate.html | 1 + 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/app-localize-behavior.html b/app-localize-behavior.html index 7873016..638d0e2 100644 --- a/app-localize-behavior.html +++ b/app-localize-behavior.html @@ -156,6 +156,15 @@ value: function() { return {} } }, + /** + * If true, will use the provided key when + * the translation does not exist for that key. + */ + useKeyIfMissing: { + type: Boolean, + value: false + }, + /** * Translates a string to the current `language`. Any parameters to the * string should be passed in order, as follows: @@ -210,20 +219,26 @@ // Cache the key/value pairs for the same language, so that we don't // do extra work if we're just reusing strings across an application. - var messageKey = key + resources[language][key]; - var msg = proto.__localizationCache.messages[messageKey]; + var translatedValue = resources[language][key]; + + if (!translatedValue) { + return this.useKeyIfMissing ? key : ''; + } + + var messageKey = key + translatedValue; + var translatedMessage = proto.__localizationCache.messages[messageKey]; - if (!msg) { - msg = new IntlMessageFormat(resources[language][key], language, formats); - proto.__localizationCache.messages[messageKey] = msg; + if (!translatedMessage) { + translatedMessage = new IntlMessageFormat(translatedValue, language, formats); + proto.__localizationCache.messages[messageKey] = translatedMessage; } var args = {}; - for (var i = 1; i < arguments.length; i+=2) { + for (var i = 1; i < arguments.length; i += 2) { args[arguments[i]] = arguments[i+1]; } - return msg.format(args); + return translatedMessage.format(args); }; }, diff --git a/demo/index.html b/demo/index.html index a25f461..38ed627 100644 --- a/demo/index.html +++ b/demo/index.html @@ -27,7 +27,7 @@ - + diff --git a/demo/x-translate.html b/demo/x-translate.html index cd90baa..0be07c1 100644 --- a/demo/x-translate.html +++ b/demo/x-translate.html @@ -27,6 +27,7 @@

{{localize('header_1')}}

{{localize('greeting')}}
+
{{localize('missing')}}
{{localize('intro', 'name', 'Batman')}}
{{localize('cats', 'numCats', 10000, 'pctBlack', 0.42)}}
{{localize('sale', 'start', 150, 'time', 15, 'price', 10)}}
diff --git a/test/basic.html b/test/basic.html index 61bccaa..5f99ddc 100644 --- a/test/basic.html +++ b/test/basic.html @@ -37,6 +37,12 @@ + + + +