Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Latest commit

 

History

History
109 lines (96 loc) · 3.34 KB

.TO_LOCALE.md

File metadata and controls

109 lines (96 loc) · 3.34 KB

this feature has been dropped in favor of a dedicated module

Not Only Objects

The JavaScript environment could be enriched in many ways but it's very hard to define methods everybody agrees on. All Object.prototype entries described before are a common thing every library does some how so it's easy to spot the utility but there are many other options we could explore it's very hard to decide what will scale in terms of both performance, size, features, and reliability.

This is why eddy.js keeps things simple and try to avoid anything that might be superfluous for a developer but there is at least one more thing I'd like to suggest with this utility, and it's about internationalization/i18n.

String#toLocaleString([object])

This method is inspired by Objective-C Cocoa framework and local functionality keeping the behavior as simple, well defined, and performant as possible in both server side and client side, included old and low performant mobile devices.

The aim of this method is to return a translated copy of the meant string by generic String.language object or the string itself.

// two language objects with same keys
var
  eng = {
    hi: 'Hello'
  },
  ita = {
    hi: 'Ciao'
  }
;

// set default language
String.setLocale(eng);

// say hi
'hi'.toLocaleString(); // Hello

// change language
String.setLocale(ita);
'hi'.toLocaleString(); // Ciao

The optional object argument can be used to retrieve at runtime a different content for that specific string.

// two language objects with same keys
var eng = String.setLocale({
  hi: 'Hello ${user}'
  // `user` is the object key
});

// say hi to me
'hi'.toLocaleString({
  user: 'WebReflection'
}); // Hello WebReflection

Being the key retrieved at runtime it's possible to use getters too so that special keys can be computed on demand.

// two language objects with same keys
var eng = String.setLocale({
  hi: 'Hello ${user} and for the ${times} time'
});

var magic = Object.defineProperties({},{
  user: {value: 'WebReflection'},
  times: {get: function () {
    return ++this.__times;
  }},
  __times: {
    value: 0,
    writable: true
  }
});

'hi'.toLocaleString(magic);
// Hello WebReflection and for the 1 time

'hi'.toLocaleString(magic);
// Hello WebReflection and for the 2 time

This implementation is ridiculously simple on purpose since "nobody" is using toLocaleString in JS in any case but this is most likely everything you need without going too fancy with nested, complicated, or slow, string operations for a runtime output.

how it was

defineProperty(
  String.prototype,
  'toLocaleString',
  commonDescriptor((function(){
    var
      hasOwnProperty = {}.hasOwnProperty,
      re = /\$\{([^}]+?)\}/g,
      place = function ($0, $1) {
        return current[$1];
      },
      locale = create(null),
      current;
    defineProperty(
      String,
      'setLocale',
      commonDescriptor(function setLocale(language){
        for (var key in language) {
          if (hasOwnProperty.call(language, key)) {
            locale[key] = language[key];
          }
        }
        return language;
      })
    );
    return function toLocaleString(object) {
      var result;
      current = object;
      result = (locale[this] || this).replace(re, place);
      current = null;
      return result;
    };
  }()))
);