Created
April 23, 2010 16:38
-
-
Save nathany/376775 to your computer and use it in GitHub Desktop.
allow default options for Rails i18n
This file contains 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
# override I18n.translate to support default_options | |
module I18n | |
mattr_accessor :default_options | |
self.default_options = {} | |
class << self | |
def translate_with_default_options(key, options = {}) | |
translate_without_default_options(key, default_options.merge(options)) | |
end | |
alias_method_chain :translate, :default_options | |
alias :t :translate_with_default_options | |
end | |
end |
This file contains 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
// javascript_i18n http://jah.pl/projects/javascript-i18n.html with modifications for default options | |
// I18n is slightly modified version of babilu.js from Tore Darell | |
var I18n = I18n || (function() { | |
// Replace {{foo}} with obj.foo | |
function interpolate(string, object) { | |
return string.replace(/\{\{([^}]+)\}\}/g, function() { | |
return object[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('.'); | |
}; | |
// 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. | |
function lookup(keys, defaults) { | |
var i = 0, value = I18n.translations; | |
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 lookup(keys.slice(0, keys.length - 1).concat(keyToArray(defaults[0].substr(1))), defaults.slice(1)); | |
} else { | |
return defaults[0]; | |
} | |
} | |
}; | |
// Returns other when 0 given | |
function pluralize(value, count) { | |
if(count === undefined) return value; | |
return count === 1 ? value.one : value.other; | |
}; | |
// added default options to merge with translate options -Nathan | |
var default_options = {}; | |
function set_default_options(options) { | |
default_options = options; | |
} | |
// 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. | |
function translate(key, options) { | |
if(typeof key != "string") { | |
// Bulk lookup | |
var a = [], i; | |
for(i = 0; i < key.length; i++) { | |
a.push(translate(key[i], options)); | |
} | |
return a; | |
} else { | |
options = options || {}; | |
options.defaultValue = options.defaultValue || null; | |
key = keyToArray(options.scope).concat(keyToArray(key)); | |
var value = lookup(key, options.defaultValue); | |
if(typeof value !== "string" && value) { | |
value = pluralize(value, options.count); | |
} | |
if(typeof value === "string") { | |
value = interpolate(value, jQuery.extend({}, default_options, options)); | |
} | |
return value; | |
} | |
} | |
return { | |
set_default_options: set_default_options, | |
translate: translate, | |
t: translate | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment