ember-i18n-1.4.1.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright (C) 2011 by James A. Rosen; Zendesk, Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. (function(window) {
  20. var I18n, assert, findTemplate, get, set, isBinding, lookupKey, pluralForm;
  21. get = Ember.Handlebars.get || Ember.Handlebars.getPath || Ember.getPath;
  22. set = Ember.set;
  23. function warn(msg) { Ember.Logger.warn(msg); }
  24. if (typeof CLDR !== "undefined" && CLDR !== null) pluralForm = CLDR.pluralForm;
  25. if (pluralForm == null) {
  26. warn("CLDR.pluralForm not found. Em.I18n will not support count-based inflection.");
  27. }
  28. lookupKey = function(key, hash) {
  29. var firstKey, idx, remainingKeys;
  30. if (hash[key] != null) { return hash[key]; }
  31. if ((idx = key.indexOf('.')) !== -1) {
  32. firstKey = key.substr(0, idx);
  33. remainingKeys = key.substr(idx + 1);
  34. hash = hash[firstKey];
  35. if (hash) { return lookupKey(remainingKeys, hash); }
  36. }
  37. };
  38. assert = Ember.assert != null ? Ember.assert : window.ember_assert;
  39. findTemplate = function(key, setOnMissing) {
  40. assert("You must provide a translation key string, not %@".fmt(key), typeof key === 'string');
  41. var result = lookupKey(key, I18n.translations);
  42. if (setOnMissing) {
  43. if (result == null) {
  44. result = I18n.translations[key] = I18n.compile("Missing translation: " + key);
  45. warn("Missing translation: " + key);
  46. }
  47. }
  48. if ((result != null) && !jQuery.isFunction(result)) {
  49. result = I18n.translations[key] = I18n.compile(result);
  50. }
  51. return result;
  52. };
  53. function eachTranslatedAttribute(object, fn) {
  54. var isTranslatedAttribute = /(.+)Translation$/,
  55. isTranslatedAttributeMatch;
  56. for (var key in object) {
  57. isTranslatedAttributeMatch = key.match(isTranslatedAttribute);
  58. if (isTranslatedAttributeMatch) {
  59. fn.call(object, isTranslatedAttributeMatch[1], I18n.t(object[key]));
  60. }
  61. }
  62. }
  63. I18n = {
  64. compile: Handlebars.compile,
  65. translations: {},
  66. template: function(key, count) {
  67. var interpolatedKey, result, suffix;
  68. if ((count != null) && (pluralForm != null)) {
  69. suffix = pluralForm(count);
  70. interpolatedKey = "%@.%@".fmt(key, suffix);
  71. result = findTemplate(interpolatedKey, false);
  72. }
  73. return result != null ? result : result = findTemplate(key, true);
  74. },
  75. t: function(key, context) {
  76. var template;
  77. if (context == null) context = {};
  78. template = I18n.template(key, context.count);
  79. return template(context);
  80. },
  81. TranslateableProperties: Em.Mixin.create({
  82. init: function() {
  83. var result = this._super.apply(this, arguments);
  84. eachTranslatedAttribute(this, function(attribute, translation) {
  85. set(this, attribute, translation);
  86. });
  87. return result;
  88. }
  89. }),
  90. TranslateableAttributes: Em.Mixin.create({
  91. didInsertElement: function() {
  92. var result = this._super.apply(this, arguments);
  93. eachTranslatedAttribute(this, function(attribute, translation) {
  94. this.$().attr(attribute, translation);
  95. });
  96. return result;
  97. }
  98. })
  99. };
  100. Ember.I18n = I18n;
  101. isBinding = /(.+)Binding$/;
  102. // CRUFT: in v2, which requires Ember 1.0+, Ember.uuid will always be
  103. // available, so this function can be cleaned up.
  104. var uniqueElementId = (function(){
  105. var id = Ember.uuid || 0;
  106. return function() {
  107. var elementId = 'i18n-' + id++;
  108. return elementId;
  109. };
  110. })();
  111. Handlebars.registerHelper('t', function(key, options) {
  112. var attrs, context, data, elementID, result, tagName, view;
  113. context = this;
  114. attrs = options.hash;
  115. data = options.data;
  116. view = data.view;
  117. tagName = attrs.tagName || 'span';
  118. delete attrs.tagName;
  119. elementID = uniqueElementId();
  120. Em.keys(attrs).forEach(function(property) {
  121. var bindPath, currentValue, invoker, isBindingMatch, normalized, normalizedPath, observer, propertyName, root, _ref;
  122. isBindingMatch = property.match(isBinding);
  123. if (isBindingMatch) {
  124. propertyName = isBindingMatch[1];
  125. bindPath = attrs[property];
  126. currentValue = get(context, bindPath, options);
  127. attrs[propertyName] = currentValue;
  128. invoker = null;
  129. normalized = Ember.Handlebars.normalizePath(context, bindPath, data);
  130. _ref = [normalized.root, normalized.path], root = _ref[0], normalizedPath = _ref[1];
  131. observer = function() {
  132. var elem, newValue;
  133. if (view.get('state') !== 'inDOM') {
  134. Em.removeObserver(root, normalizedPath, invoker);
  135. return;
  136. }
  137. newValue = get(context, bindPath, options);
  138. elem = view.$("#" + elementID);
  139. attrs[propertyName] = newValue;
  140. return elem.html(I18n.t(key, attrs));
  141. };
  142. invoker = function() {
  143. return Em.run.once(observer);
  144. };
  145. return Em.addObserver(root, normalizedPath, invoker);
  146. }
  147. });
  148. result = '<%@ id="%@">%@</%@>'.fmt(tagName, elementID, I18n.t(key, attrs), tagName);
  149. return new Handlebars.SafeString(result);
  150. });
  151. Handlebars.registerHelper('translateAttr', function(options) {
  152. var attrs, result;
  153. attrs = options.hash;
  154. result = [];
  155. Em.keys(attrs).forEach(function(property) {
  156. var translatedValue;
  157. translatedValue = I18n.t(attrs[property]);
  158. return result.push('%@="%@"'.fmt(property, translatedValue));
  159. });
  160. return new Handlebars.SafeString(result.join(' '));
  161. });
  162. }).call(undefined, this);