function _(str) {
  // Return the value, unless undefined (or if i18n doesn't exist)
  return (typeof i18n !== 'undefined') && i18n[str] || str;
};

(function($){
  $.fn.extend({
    /**
    * Like jQuery.html(), but insert the translation
    * this is a shortcut to $(el).html(_('string'));
    */
    _: function(str){
      return this.each(function(i, item){
        if ($(item).is('input')) {
          item.value = _(str);
        } else {
          $(item).html(_(str));
        }
      });
    },

    /**
    * Translate based on the text contents
    */
    _tr: function(){
      return this.each(function(i,item){
        if ($(item).is('input')) {
          item.value = _($.trim(item.value));
        } else {
          $(item)._($.trim($(item).text()));
        }
      });
    }
  });
})(jQuery);