Created
May 29, 2016 23:55
-
-
Save sixtyfive/f9adf9bb2bac491f2e62819a838c3e8b to your computer and use it in GitHub Desktop.
Didn't love this all that much, but perhaps it'll get useful again some day...
This file contains hidden or 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
require 'i18n/backend/active_record' | |
# https://github.com/svenfuchs/i18n-active_record | |
if Translation.table_exists? | |
# Translation = I18n::Backend::ActiveRecord::Translation # Breaks Ransack :-( | |
I18n.backend = I18n::Backend::ActiveRecord.new | |
I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize) | |
I18n::Backend::Simple.send(:include, I18n::Backend::Memoize) | |
I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization) | |
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, I18n.backend) | |
end | |
# Try to emulate GNU gettext usage model | |
def _(translation_key, interpolations = {}) | |
locale = I18n.locale | |
begin | |
value = I18n.translate!(translation_key, interpolations, raise: true) | |
rescue | |
if t = Translation.find_by(key: translation_key, locale: locale) | |
t.update_attribute(:value, translation_key) if default? t | |
else | |
t = Translation.new(key: translation_key, interpolations: interpolations, locale: locale) | |
t.value = translation_key if default? t | |
t.save | |
end | |
value = t.value || translation_key | |
end | |
return value | |
end | |
def default?(t) | |
I18n.locale == :en && t.value.blank? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment