if (!window.I18n) { I18n = {}; }

I18n.defaultLocale = "en";

I18n.translations = {"lo":{"search":{"show_search_tips":"\u0e9b\u0eb4\u0e94\u0ec1\u0e99\u0eb0\u0e99\u0eb3\u0ea7\u0eb4\u0e97\u0eb5\u0e81\u0eb2\u0e99\u0e84\u0ebb\u0ec9\u0e99\u0eab\u0eb2","hide_search_tips":"\u0eaa\u0eb0\u0ec1\u0e94\u0e87\u0ec1\u0e99\u0eb0\u0e99\u0eb3\u0ea7\u0eb4\u0e97\u0eb5\u0e81\u0eb2\u0e99\u0e84\u0ebb\u0ec9\u0e99\u0eab\u0eb2","show_help":"\u0eaa\u0eb0\u0ec1\u0e94\u0e87\u0ec1\u0e99\u0eb0\u0e99\u0eb3","hide_help":"\u0e9b\u0eb4\u0e94\u0ec1\u0e99\u0eb0\u0e99\u0eb3"},"common":{"text":"\u0e9a\u0ebb\u0e94\u0ec0\u0ea5\u0eb7\u0ec8\u0ead\u0e87","texts":"\u0e9a\u0ebb\u0e94\u0ec0\u0ea5\u0eb7\u0ec8\u0ead\u0e87"},"favorites":{"your_search_has_been_saved":"\u0e81\u0eb2\u0e99\u0e84\u0ebb\u0ec9\u0e99\u0eab\u0eb2\u0e82\u0ead\u0e87\u0e97\u0ec8\u0eb2\u0e99\u0ec4\u0e94\u0ec9\u0eaa\u0ebb\u0ec8\u0e87\u0ec0\u0e82\u0ebb\u0ec9\u0eb2 \u0ec1\u0ea5\u0eb0 \u0ec0\u0e81\u0eb1\u0e9a\u0ea1\u0ec9\u0ebd\u0e99\u0ec4\u0ea7\u0ec9\u0ea2\u0eb9\u0ec8\u0eab\u0ec9\u0ead\u0e87\u0e81\u0eb2\u0e99\u0eaa\u0ec8\u0ea7\u0e99\u0e95\u0ebb\u0ea7\u0eae\u0ebd\u0e9a\u0eae\u0ec9\u0ead\u0e8d\u0ec1\u0ea5\u0ec9\u0ea7","your_text_has_been_saved":"\u0e9a\u0ebb\u0e94\u0ec0\u0ea5\u0eb7\u0ec8\u0ead\u0e87\u0e82\u0ead\u0e87\u0e97\u0ec8\u0eb2\u0e99\u0ec4\u0e94\u0ec9\u0eaa\u0ebb\u0ec8\u0e87\u0ec0\u0e82\u0ebb\u0ec9\u0eb2 \u0ec1\u0ea5\u0eb0 \u0ec0\u0e81\u0eb1\u0e9a\u0ea1\u0ec9\u0ebd\u0e99\u0ec4\u0ea7\u0ec9\u0ea2\u0eb9\u0ec8\u0eab\u0ec9\u0ead\u0e87\u0e81\u0eb2\u0e99\u0eaa\u0ec8\u0ea7\u0e99\u0e95\u0ebb\u0ea7\u0eae\u0ebd\u0e9a\u0eae\u0ec9\u0ead\u0e8d\u0ec1\u0ea5\u0ec9\u0ea7"},"show_text":{"showing_image_x_of_x":"\u0eaa\u0eb0\u0ec1\u0e94\u0e87\u0eae\u0eb9\u0e9a\u0e97\u0eb5\u0ec8 {{count}} \u0e88\u0eb2\u0e81\u0e88\u0eb3\u0e99\u0ea7\u0e99 {{totalcount}} \u0eae\u0eb9\u0e9a"}},"en":{"search":{"show_search_tips":"SHOW SEARCH TIPS","hide_search_tips":"HIDE SEARCH TIPS","show_help":"SHOW HELP","hide_help":"HIDE HELP"},"common":{"text":"text","texts":"texts"},"favorites":{"your_search_has_been_saved":"Your search has been saved.","your_text_has_been_saved":"Your text has been saved to your favourites"},"show_text":{"showing_image_x_of_x":"SHOWING IMAGE {{count}} OF {{totalcount}}"}}};

(function(){

  var interpolatePattern = /\{\{([^}]+)\}\}/g;

  //Replace {{foo} with obj.foo
  function interpolate(str, obj){
    return str.replace(interpolatePattern, function(){
      return obj[arguments[1]] || arguments[0];
    });
  };

  //Split "foo.bar" to ["foo", "bar"] if key is a string
  function keyToArray(key){
    if (!key) return [];
    if (typeof key != "string") return key;
    return key.split('.');
  };

  function locale(){
    return I18n.locale || I18n.defaultLocale;
  };

  function getLocaleFromCookie(){
    var cookies = document.cookie.split(/\s*;\s*/),
        i, pair, locale;
    for (i = 0; i < cookies.length; i++) {
      pair = cookies[i].split('=');
      if (pair[0] === 'locale') { locale = pair[1]; break; }
    }
    return locale;
  };


  I18n.init = function(){
    this.locale = getLocaleFromCookie();
  };

  //Works mostly the same as the Ruby equivalent, except there are
  //no symbols in JavaScript, so keys are always strings. The only time
  //this makes a difference is when differentiating between keys and values
  //in the defaultValue option. Strings starting with ":" will be considered
  //to be keys and used for lookup, while other strings are returned as-is.
  I18n.translate = function(key, opts){
    if (typeof key != "string") { //Bulk lookup
      var a = [], i;
      for (i=0; i<key.length; i++) {
        a.push(this.translate(key[i], opts));
      }
      return a;
    } else {
      opts = opts || {};
      opts.defaultValue = opts.defaultValue || null;
      key = keyToArray(opts.scope).concat(keyToArray(key));
      var value = this.lookup(key, opts.defaultValue);
      if (typeof value != "string" && value) value = this.pluralize(value, opts.count);
      if (typeof value == "string") value = interpolate(value, opts);
      return value;
    }
  };

  I18n.t = I18n.translate;

  //Looks up a translation using an array of strings where the last
  //is the key and any string before that define the scope. The current
  //locale is always prepended and does not need to be provided. The second
  //parameter is an array of strings used as defaults if the key can not be
  //found. If a key starts with ":" it is used as a key for lookup.
  //This method does not perform pluralization or interpolation.
  I18n.lookup = function(keys, defaults){
    var i = 0, value = this.translations[locale()];
    defaults = typeof defaults == "string" ? [defaults] : (defaults || []);
    while (keys[i]) {
      value = value && value[keys[i]];
      i++;
    }
    if (value){
      return value;
    } else {
      if (defaults.length == 0) {
        return null;
      } else if (defaults[0].substr(0,1) == ':') {
        return this.lookup(keys.slice(0,keys.length-1).concat(keyToArray(defaults[0].substr(1))), defaults.slice(1));
      } else {
        return defaults[0];
      }
    }
  };

  I18n.pluralize = function(value, count){
    if (!count) return value;
    return count == 1 ? value.one : value.other;
  };

})();

I18n.init();
