Created
January 18, 2016 18:06
-
-
Save mauritslamers/ea1370511d5a1a0995a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Original: | |
| loc: function(str) { | |
| // NB: This could be implemented as a wrapper to locWithDefault() but | |
| // it would add some overhead to deal with the arguments and adds stack | |
| // frames, so we are keeping the implementation separate. | |
| if (!SC.Locale.currentLocale) { SC.Locale.createCurrentLocale(); } | |
| var localized = SC.Locale.currentLocale.locWithDefault(str); | |
| if (SC.typeOf(localized) !== SC.T_STRING) { localized = str; } | |
| var args = SC.$A(arguments); | |
| args.shift(); // remove str param | |
| //to extend String.prototype | |
| if (args.length > 0 && args[0] && args[0].isSCArray) { args = args[0]; } | |
| // I looked up the performance of try/catch. IE and FF do not care so | |
| // long as the catch never happens. Safari and Chrome are affected rather | |
| // severely (10x), but this is a one-time cost per loc (the code being | |
| // executed is likely as expensive as this try/catch cost). | |
| // | |
| // Also, .loc() is not called SO much to begin with. So, the error handling | |
| // that this gives us is worth it. | |
| try { | |
| return SC.String.fmt(localized, args); | |
| } catch (e) { | |
| SC.error("Error processing string with key: " + str); | |
| SC.error("Localized String: " + localized); | |
| SC.error("Error: " + e); | |
| } | |
| }, | |
| */ | |
| SC.String.loc = function (str) { | |
| var compprop = SC.Function.property(function (str) { | |
| if (!SC.Locale.currentLocale) { SC.Locale.createCurrentLocale(); } | |
| var localized = SC.Locale.currentLocale.locWithDefault(str); | |
| if (SC.typeOf(localized) !== SC.T_STRING) { localized = str; } | |
| var args = SC.$A(arguments); | |
| args.shift(); // remove str param | |
| //to extend String.prototype | |
| if (args.length > 0 && args[0] && args[0].isSCArray) { args = args[0]; } | |
| // I looked up the performance of try/catch. IE and FF do not care so | |
| // long as the catch never happens. Safari and Chrome are affected rather | |
| // severely (10x), but this is a one-time cost per loc (the code being | |
| // executed is likely as expensive as this try/catch cost). | |
| // | |
| // Also, .loc() is not called SO much to begin with. So, the error handling | |
| // that this gives us is worth it. | |
| try { | |
| return SC.String.fmt(localized, args); | |
| } catch (e) { | |
| SC.error("Error processing string with key: " + str); | |
| SC.error("Localized String: " + localized); | |
| SC.error("Error: " + e); | |
| } | |
| }, ['SC.Locale.currentLocale']); | |
| return compprop.cacheable(); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment