Created
November 19, 2010 10:51
-
-
Save tomash/706344 to your computer and use it in GitHub Desktop.
i18n database backend conflicting with globalize3
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
class AddTranslationsTable < I18n::Backend::Database::Migration | |
def self.up | |
create_table :translation_migrations do |t| | |
t.string :version | |
end | |
create_table :translations do |t| | |
t.string :locale | |
t.string :key | |
t.text :value | |
t.text :interpolations | |
t.boolean :is_proc, :default => false | |
end | |
add_index :translations, :key | |
end | |
def self.down | |
remove_index :translations, :key | |
drop_table :translations | |
drop_table :translation_migrations | |
end | |
end |
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
module I18n | |
module Backend | |
class Database < ActiveRecord | |
include Fallbacks | |
include Cache | |
def translate(locale, key, options = {}) | |
begin | |
super | |
rescue I18n::MissingTranslationData | |
key_parts = key.to_s.split(".") | |
if key_parts.size > 1 | |
translate(locale, (key_parts[0..-3] << key_parts[-1]).join("."), options) | |
else | |
raise(I18n::MissingTranslationData.new(locale, key, options)) | |
end | |
end | |
end | |
end | |
end | |
end | |
I18n::Backend::Database::Translation.class_eval do | |
after_save :clear_cache | |
def clear_cache | |
I18n.cache_store.clear | |
end | |
end |
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
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) | |
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Database.new, I18n::Backend::Simple.new) | |
lookup_store = :mem_cache_store | |
I18n.cache_store = ActiveSupport::Cache.lookup_store(lookup_store) | |
I18n.cache_namespace = Rails.env | |
module I18n | |
def self.name_for_locale(locale) | |
begin | |
I18n.backend.translate locale, "meta.language_name" | |
rescue I18n::MissingTranslationData | |
locale.to_s | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment