Created
May 2, 2010 20:17
-
-
Save jtsagata/387406 to your computer and use it in GitHub Desktop.
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
module TranslationHelper | |
# foo = ActionView::Base.new.extend TranslationHelper | |
# TODO: If translation is missing | |
# get a translation from google | |
# and then add it to database, marked as dirty (how?) | |
def translate(key, options = {}) | |
key = scope_key_by_partial(key) | |
options[:raise] = true | |
options[:request_locale] = options.has_key?(:locale) ? options[:locale] : I18n.locale | |
options[:span_class] ='translation_missing' | |
for fallback in I18n_fallbacks[options[:request_locale]] | |
begin | |
result = I18n.translate( key, options.merge(:locale=>fallback) ) | |
rescue I18n::MissingTranslationData | |
end | |
return translation_tag(result,fallback,key,options) unless result.nil? | |
end | |
options[:span_class] ='translation_absent' | |
translation = I18n.normalize_keys(options[:request_locale], key, options[:scope]).join(': ') | |
translation_tag(translation,key,nil,options) | |
end | |
alias :t :translate | |
private | |
def translation_tag(result, locale, key, options) | |
show_span = options.has_key?(:span) ? options[:span] : true | |
request_locale = options[:request_locale] | |
if request_locale.to_s.starts_with?(locale.to_s) | |
return result | |
else | |
keys = I18n.normalize_keys(request_locale, key, options[:scope]) | |
return show_span ? content_tag('span', result, | |
:class => options[:span_class], | |
:'data-key' => keys.join(', ')) : result | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment