Skip to content

Commit

Permalink
Fix translated to use method's ? and ! as locale
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Sep 30, 2010
1 parent 2f59c89 commit 70d964f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions r18n-core/lib/r18n-core/translated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ def translations(*methods)
# translation :desciption, :type => 'markdown'
def translation(name, options = {})
if options[:methods]
@unlocalized_getters[name] = R18n::Utils.hash_map(options[:methods]) { |l, i| [ l.to_s, i.to_s ] }
@unlocalized_getters[name] = R18n::Utils.
hash_map(options[:methods]) { |l, i| [ l.to_s, i.to_s ] }
unless options[:no_write]
@unlocalized_setters[name] =R18n::Utils.hash_map(options[:methods]) { |l, i| [ l.to_s, i.to_s + '=' ] }
@unlocalized_setters[name] = R18n::Utils.
hash_map(options[:methods]) { |l, i| [ l.to_s, i.to_s + '=' ] }
end
end

Expand Down Expand Up @@ -163,7 +165,7 @@ def #{name}=(*params)
# didn’t set map in +translation+ option +methods+, it will be detect
# automatically.
def unlocalized_getters(method)
matcher = Regexp.new('^' + Regexp.escape(method.to_s) + '_(.*[^=])$')
matcher = Regexp.new('^' + Regexp.escape(method.to_s) + '_(\w+)$')
unless @unlocalized_getters.has_key? method
@unlocalized_getters[method] = {}
self.instance_methods.reject { |i| not i =~ matcher }.each do |i|
Expand All @@ -177,7 +179,7 @@ def unlocalized_getters(method)
# didn’t set map in +translation+ option +methods+, it will be detect
# automatically.
def unlocalized_setters(method)
matcher = Regexp.new('^' + Regexp.escape(method.to_s) + '_(.*)=$')
matcher = Regexp.new('^' + Regexp.escape(method.to_s) + '_(\w+)=$')
unless @unlocalized_setters.has_key? method
@unlocalized_setters[method] = {}
self.instance_methods.reject { |i| not i =~ matcher }.each do |i|
Expand Down
3 changes: 3 additions & 0 deletions r18n-core/spec/translated_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
@user_class = Class.new do
include R18n::Translated
attr_accessor :name_ru, :name_en

def name_ru?; end
def name_ru!; end
end
R18n.set(R18n::I18n.new('en'))
end
Expand Down
3 changes: 3 additions & 0 deletions r18n-rails/spec/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
@post.title_ru.should == 'Запись'
@post.title_en.should == 'Record'
@post.title.should == 'Запись'

Post.unlocalized_getters(:title).should == { 'ru' => 'title_ru',
'en' => 'title_en' }
end

end

0 comments on commit 70d964f

Please sign in to comment.