diff options
Diffstat (limited to 'vendor/plugins/globalize2/lib/globalize')
5 files changed, 82 insertions, 15 deletions
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 | |||
| 51 | translation(value, meta) | 51 | translation(value, meta) |
| 52 | end | 52 | end |
| 53 | else | 53 | else |
| 54 | raise "unexpected translation type: #{result.inspect}" | 54 | result |
| 55 | # raise "unexpected translation type: #{result.inspect}" | ||
| 55 | end | 56 | end |
| 56 | end | 57 | end |
| 57 | end | 58 | 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 @@ | |||
| 1 | # A simple exception handler that behaves like the default exception handler | ||
| 2 | # but also raises on missing translations. | ||
| 3 | # | ||
| 4 | # Useful for identifying missing translations during testing. | ||
| 5 | # | ||
| 6 | # E.g. | ||
| 7 | # | ||
| 8 | # require 'globalize/i18n/missing_translations_raise_handler | ||
| 9 | # I18n.exception_handler = :missing_translations_raise_handler | ||
| 10 | module I18n | ||
| 11 | class << self | ||
| 12 | def missing_translations_raise_handler(exception, locale, key, options) | ||
| 13 | raise exception | ||
| 14 | end | ||
| 15 | end | ||
| 16 | |||
| 17 | # self.exception_handler = :missing_translations_raise_handler | ||
| 18 | end | ||
| 19 | |||
| 20 | I18n.exception_handler = :missing_translations_raise_handler | ||
| 21 | |||
| 22 | ActionView::Helpers::TranslationHelper.module_eval do | ||
| 23 | def translate(key, options = {}) | ||
| 24 | I18n.translate(key, options) | ||
| 25 | end | ||
| 26 | alias :t :translate | ||
| 27 | 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' | |||
| 6 | module Globalize | 6 | module Globalize |
| 7 | module Model | 7 | module Model |
| 8 | module ActiveRecord | 8 | module ActiveRecord |
| 9 | class << self | 9 | class << self |
| 10 | def create_proxy_class(klass) | 10 | def create_proxy_class(klass) |
| 11 | Object.const_set "#{klass.name}Translation", Class.new(::ActiveRecord::Base){ | 11 | Object.const_set "#{klass.name}Translation", Class.new(::ActiveRecord::Base){ |
| 12 | belongs_to "#{klass.name.underscore}".intern | 12 | belongs_to "#{klass.name.underscore}".intern |
| @@ -24,10 +24,10 @@ module Globalize | |||
| 24 | def define_accessors(klass, attr_names) | 24 | def define_accessors(klass, attr_names) |
| 25 | attr_names.each do |attr_name| | 25 | attr_names.each do |attr_name| |
| 26 | klass.send :define_method, attr_name, lambda { | 26 | klass.send :define_method, attr_name, lambda { |
| 27 | globalize.fetch I18n.locale, attr_name | 27 | globalize.fetch self.class.locale, attr_name |
| 28 | } | 28 | } |
| 29 | klass.send :define_method, "#{attr_name}=", lambda {|val| | 29 | klass.send :define_method, "#{attr_name}=", lambda {|val| |
| 30 | globalize.stash I18n.locale, attr_name, val | 30 | globalize.stash self.class.locale, attr_name, val |
| 31 | self[attr_name] = val | 31 | self[attr_name] = val |
| 32 | } | 32 | } |
| 33 | end | 33 | 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 @@ | |||
| 1 | module Globalize | 1 | module Globalize |
| 2 | module Model | 2 | module Model |
| 3 | class AttributeStash < Hash | 3 | class AttributeStash < Hash |
| 4 | def contains?(locale, attr_name) | ||
| 5 | locale = locale.to_sym | ||
| 6 | self[locale] ||= {} | ||
| 7 | self[locale].has_key? attr_name | ||
| 8 | end | ||
| 9 | |||
| 4 | def read(locale, attr_name) | 10 | def read(locale, attr_name) |
| 5 | locale = locale.to_sym | 11 | locale = locale.to_sym |
| 6 | self[locale] ||= {} | 12 | self[locale] ||= {} |
| @@ -17,13 +23,16 @@ module Globalize | |||
| 17 | class Adapter | 23 | class Adapter |
| 18 | def initialize(record) | 24 | def initialize(record) |
| 19 | @record = record | 25 | @record = record |
| 26 | |||
| 27 | # TODO what exactly are the roles of cache and stash | ||
| 20 | @cache = AttributeStash.new | 28 | @cache = AttributeStash.new |
| 21 | @stash = AttributeStash.new | 29 | @stash = AttributeStash.new |
| 22 | end | 30 | end |
| 23 | 31 | ||
| 24 | def fetch(locale, attr_name) | 32 | def fetch(locale, attr_name) |
| 25 | # locale = I18n.locale | 33 | # locale = I18n.locale |
| 26 | @cache.read(locale, attr_name) || begin | 34 | is_cached = @cache.contains?(locale, attr_name) |
| 35 | is_cached ? @cache.read(locale, attr_name) : begin | ||
| 27 | value = fetch_attribute locale, attr_name | 36 | value = fetch_attribute locale, attr_name |
| 28 | @cache.write locale, attr_name, value if value && value.locale == locale | 37 | @cache.write locale, attr_name, value if value && value.locale == locale |
| 29 | value | 38 | value |
| @@ -47,6 +56,7 @@ module Globalize | |||
| 47 | # Clears the cache | 56 | # Clears the cache |
| 48 | def clear | 57 | def clear |
| 49 | @cache.clear | 58 | @cache.clear |
| 59 | @stash.clear | ||
| 50 | end | 60 | end |
| 51 | 61 | ||
| 52 | private | 62 | private |
| @@ -61,6 +71,7 @@ module Globalize | |||
| 61 | # Check the @globalize_set_translations cache first to see if we've just changed the | 71 | # Check the @globalize_set_translations cache first to see if we've just changed the |
| 62 | # attribute and not saved yet. | 72 | # attribute and not saved yet. |
| 63 | fallbacks.each do |fallback| | 73 | fallbacks.each do |fallback| |
| 74 | # TODO should we be checking stash or just cache? | ||
| 64 | result = @stash.read(fallback, attr_name) || begin | 75 | result = @stash.read(fallback, attr_name) || begin |
| 65 | translation = translations.detect {|tr| tr.locale == fallback } | 76 | translation = translations.detect {|tr| tr.locale == fallback } |
| 66 | translation && translation.send(attr_name) | 77 | 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 | |||
| 19 | 19 | ||
| 20 | # Only set up once per class | 20 | # Only set up once per class |
| 21 | unless included_modules.include? InstanceMethods | 21 | unless included_modules.include? InstanceMethods |
| 22 | class_inheritable_accessor :globalize_options | 22 | class_inheritable_accessor :globalize_options, :globalize_proxy |
| 23 | |||
| 23 | include InstanceMethods | 24 | include InstanceMethods |
| 24 | extend ClassMethods | 25 | extend ClassMethods |
| 26 | alias_method_chain :reload, :globalize | ||
| 25 | 27 | ||
| 26 | proxy_class = Globalize::Model::ActiveRecord.create_proxy_class(self) | 28 | self.globalize_proxy = Globalize::Model::ActiveRecord.create_proxy_class(self) |
| 27 | has_many :globalize_translations, :class_name => proxy_class.name, :extend => Extensions | 29 | has_many :globalize_translations, :class_name => globalize_proxy.name, :extend => Extensions |
| 28 | 30 | ||
| 29 | after_save :update_globalize_record | 31 | after_save :update_globalize_record |
| 30 | |||
| 31 | def i18n_attr(attribute_name) | ||
| 32 | self.name.underscore + "_translations.#{attribute_name}" | ||
| 33 | end | ||
| 34 | end | 32 | end |
| 35 | 33 | ||
| 36 | self.globalize_options = options | 34 | self.globalize_options = options |
| @@ -41,6 +39,14 @@ module Globalize | |||
| 41 | extend Callbacks | 39 | extend Callbacks |
| 42 | Callbacks.instance_methods.each {|cb| send cb } | 40 | Callbacks.instance_methods.each {|cb| send cb } |
| 43 | end | 41 | end |
| 42 | |||
| 43 | def locale=(locale) | ||
| 44 | @@locale = locale | ||
| 45 | end | ||
| 46 | |||
| 47 | def locale | ||
| 48 | (defined?(@@locale) && @@locale) || I18n.locale | ||
| 49 | end | ||
| 44 | end | 50 | end |
| 45 | 51 | ||
| 46 | # Dummy Callbacks module. Extensions to Globalize2 can insert methods into here | 52 | # Dummy Callbacks module. Extensions to Globalize2 can insert methods into here |
| @@ -59,13 +65,13 @@ module Globalize | |||
| 59 | def method_missing(method, *args) | 65 | def method_missing(method, *args) |
| 60 | if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym) | 66 | if method.to_s =~ /^find_by_(\w+)$/ && globalize_options[:translated_attributes].include?($1.to_sym) |
| 61 | find(:first, :joins => :globalize_translations, | 67 | find(:first, :joins => :globalize_translations, |
| 62 | :conditions => [i18n_attr($1)+" = ? AND "+i18n_attr('locale')+" IN (?)", | 68 | :conditions => [ "#{i18n_attr($1)} = ? AND #{i18n_attr('locale')} IN (?)", |
| 63 | args.first,I18n.fallbacks[I18n.locale].map{|tag| tag.to_s}]) | 69 | args.first,I18n.fallbacks[I18n.locale].map{|tag| tag.to_s}]) |
| 64 | else | 70 | else |
| 65 | super | 71 | super |
| 66 | end | 72 | end |
| 67 | end | 73 | end |
| 68 | 74 | ||
| 69 | def create_translation_table!(fields) | 75 | def create_translation_table!(fields) |
| 70 | translated_fields = self.globalize_options[:translated_attributes] | 76 | translated_fields = self.globalize_options[:translated_attributes] |
| 71 | translated_fields.each do |f| | 77 | translated_fields.each do |f| |
| @@ -94,9 +100,27 @@ module Globalize | |||
| 94 | translation_table_name = self.name.underscore + '_translations' | 100 | translation_table_name = self.name.underscore + '_translations' |
| 95 | self.connection.drop_table translation_table_name | 101 | self.connection.drop_table translation_table_name |
| 96 | end | 102 | end |
| 103 | |||
| 104 | private | ||
| 105 | |||
| 106 | def i18n_attr(attribute_name) | ||
| 107 | self.base_class.name.underscore + "_translations.#{attribute_name}" | ||
| 108 | end | ||
| 97 | end | 109 | end |
| 98 | 110 | ||
| 99 | module InstanceMethods | 111 | module InstanceMethods |
| 112 | def reload_with_globalize | ||
| 113 | globalize.clear | ||
| 114 | |||
| 115 | # clear all globalized attributes | ||
| 116 | # TODO what's the best way to handle this? | ||
| 117 | self.class.globalize_options[:translated_attributes].each do |attr| | ||
| 118 | @attributes.delete attr.to_s | ||
| 119 | end | ||
| 120 | |||
| 121 | reload_without_globalize | ||
| 122 | end | ||
| 123 | |||
| 100 | def globalize | 124 | def globalize |
| 101 | @globalize ||= Adapter.new self | 125 | @globalize ||= Adapter.new self |
| 102 | end | 126 | end |
| @@ -104,6 +128,10 @@ module Globalize | |||
| 104 | def update_globalize_record | 128 | def update_globalize_record |
| 105 | globalize.update_translations! | 129 | globalize.update_translations! |
| 106 | end | 130 | end |
| 131 | |||
| 132 | def translated_locales | ||
| 133 | globalize_translations.scoped(:select => 'DISTINCT locale').map {|gt| gt.locale.to_sym } | ||
| 134 | end | ||
| 107 | end | 135 | end |
| 108 | end | 136 | end |
| 109 | end | 137 | end |
