From f4c73763e5b3d65f3377fb1238a94122a1c272a4 Mon Sep 17 00:00:00 2001 From: hukl Date: Wed, 25 Feb 2009 22:37:22 +0100 Subject: updated globalize2 --- .../globalize2/lib/globalize/backend/static.rb | 3 +- .../i18n/missing_translations_raise_handler.rb | 27 ++++++++++++ .../lib/globalize/model/active_record.rb | 6 +-- .../lib/globalize/model/active_record/adapter.rb | 13 +++++- .../globalize/model/active_record/translated.rb | 48 +++++++++++++++++----- 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb (limited to 'vendor/plugins/globalize2/lib') diff --git a/vendor/plugins/globalize2/lib/globalize/backend/static.rb b/vendor/plugins/globalize2/lib/globalize/backend/static.rb index 3903517c..fb9e1fe2 100644 --- a/vendor/plugins/globalize2/lib/globalize/backend/static.rb +++ b/vendor/plugins/globalize2/lib/globalize/backend/static.rb @@ -51,7 +51,8 @@ module Globalize translation(value, meta) end else - raise "unexpected translation type: #{result.inspect}" + result + # raise "unexpected translation type: #{result.inspect}" end end end diff --git a/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb b/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb new file mode 100644 index 00000000..e32be280 --- /dev/null +++ b/vendor/plugins/globalize2/lib/globalize/i18n/missing_translations_raise_handler.rb @@ -0,0 +1,27 @@ +# A simple exception handler that behaves like the default exception handler +# but also raises on missing translations. +# +# Useful for identifying missing translations during testing. +# +# E.g. +# +# require 'globalize/i18n/missing_translations_raise_handler +# I18n.exception_handler = :missing_translations_raise_handler +module I18n + class << self + def missing_translations_raise_handler(exception, locale, key, options) + raise exception + end + end + +# self.exception_handler = :missing_translations_raise_handler +end + +I18n.exception_handler = :missing_translations_raise_handler + +ActionView::Helpers::TranslationHelper.module_eval do + def translate(key, options = {}) + I18n.translate(key, options) + end + alias :t :translate +end diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record.rb index 450729c6..96458425 100644 --- a/vendor/plugins/globalize2/lib/globalize/model/active_record.rb +++ b/vendor/plugins/globalize2/lib/globalize/model/active_record.rb @@ -6,7 +6,7 @@ require 'globalize/model/active_record/translated' module Globalize module Model module ActiveRecord - class << self + class << self def create_proxy_class(klass) Object.const_set "#{klass.name}Translation", Class.new(::ActiveRecord::Base){ belongs_to "#{klass.name.underscore}".intern @@ -24,10 +24,10 @@ module Globalize def define_accessors(klass, attr_names) attr_names.each do |attr_name| klass.send :define_method, attr_name, lambda { - globalize.fetch I18n.locale, attr_name + globalize.fetch self.class.locale, attr_name } klass.send :define_method, "#{attr_name}=", lambda {|val| - globalize.stash I18n.locale, attr_name, val + globalize.stash self.class.locale, attr_name, val self[attr_name] = val } end diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb index 12d7564b..d1c8db81 100644 --- a/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb +++ b/vendor/plugins/globalize2/lib/globalize/model/active_record/adapter.rb @@ -1,6 +1,12 @@ module Globalize module Model class AttributeStash < Hash + def contains?(locale, attr_name) + locale = locale.to_sym + self[locale] ||= {} + self[locale].has_key? attr_name + end + def read(locale, attr_name) locale = locale.to_sym self[locale] ||= {} @@ -17,13 +23,16 @@ module Globalize class Adapter def initialize(record) @record = record + + # TODO what exactly are the roles of cache and stash @cache = AttributeStash.new @stash = AttributeStash.new end def fetch(locale, attr_name) # locale = I18n.locale - @cache.read(locale, attr_name) || begin + is_cached = @cache.contains?(locale, attr_name) + is_cached ? @cache.read(locale, attr_name) : begin value = fetch_attribute locale, attr_name @cache.write locale, attr_name, value if value && value.locale == locale value @@ -47,6 +56,7 @@ module Globalize # Clears the cache def clear @cache.clear + @stash.clear end private @@ -61,6 +71,7 @@ module Globalize # Check the @globalize_set_translations cache first to see if we've just changed the # attribute and not saved yet. fallbacks.each do |fallback| + # TODO should we be checking stash or just cache? result = @stash.read(fallback, attr_name) || begin translation = translations.detect {|tr| tr.locale == fallback } translation && translation.send(attr_name) diff --git a/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb b/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb index 710cde5d..c7ae9e99 100644 --- a/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb +++ b/vendor/plugins/globalize2/lib/globalize/model/active_record/translated.rb @@ -19,18 +19,16 @@ module Globalize # Only set up once per class unless included_modules.include? InstanceMethods - class_inheritable_accessor :globalize_options + class_inheritable_accessor :globalize_options, :globalize_proxy + include InstanceMethods extend ClassMethods + alias_method_chain :reload, :globalize - proxy_class = Globalize::Model::ActiveRecord.create_proxy_class(self) - has_many :globalize_translations, :class_name => proxy_class.name, :extend => Extensions + self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self) + has_many :globalize_translations, :class_name => globalize_proxy.name, :extend => Extensions - after_save :update_globalize_record - - def i18n_attr(attribute_name) - self.name.underscore + "_translations.#{attribute_name}" - end + after_save :update_globalize_record end self.globalize_options = options @@ -41,6 +39,14 @@ module Globalize extend Callbacks Callbacks.instance_methods.each {|cb| send cb } end + + def locale=(locale) + @@locale = locale + end + + def locale + (defined?(@@locale) && @@locale) || I18n.locale + end end # Dummy Callbacks module. Extensions to Globalize2 can insert methods into here @@ -59,13 +65,13 @@ module Globalize def method_missing(method, *args) if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym) find(:first, :joins => :globalize_translations, - :conditions => [i18n_attr($1)+" = ? AND "+i18n_attr('locale')+" IN (?)", + :conditions => [ "#{i18n_attr($1)} = ? AND #{i18n_attr('locale')} IN (?)", args.first,I18n.fallbacks[I18n.locale].map{|tag| tag.to_s}]) else super end end - + def create_translation_table!(fields) translated_fields = self.globalize_options[:translated_attributes] translated_fields.each do |f| @@ -94,9 +100,27 @@ module Globalize translation_table_name = self.name.underscore + '_translations' self.connection.drop_table translation_table_name end + + private + + def i18n_attr(attribute_name) + self.base_class.name.underscore + "_translations.#{attribute_name}" + end end module InstanceMethods + def reload_with_globalize + globalize.clear + + # clear all globalized attributes + # TODO what's the best way to handle this? + self.class.globalize_options[:translated_attributes].each do |attr| + @attributes.delete attr.to_s + end + + reload_without_globalize + end + def globalize @globalize ||= Adapter.new self end @@ -104,6 +128,10 @@ module Globalize def update_globalize_record globalize.update_translations! end + + def translated_locales + globalize_translations.scoped(:select => 'DISTINCT locale').map {|gt| gt.locale.to_sym } + end end end end -- cgit v1.3