i18n handle label_tag calls
Change-Id: I83081d9521621f2ec66d2184130ddc3415c0e9bb Reviewed-on: https://gerrit.instructure.com/4291 Reviewed-by: Jon Jensen <jon@instructure.com> Tested-by: Hudson <hudson@instructure.com>
This commit is contained in:
parent
95de74728e
commit
0cc58830c2
|
@ -13,6 +13,20 @@ module I18nUtilities
|
|||
text_or_key = t('labels.' + text_or_key.to_s, default_value) if default_value
|
||||
t("before_label_wrapper", "%{text}:", :text => text_or_key)
|
||||
end
|
||||
|
||||
def _label_symbol_translation(method, text, options)
|
||||
if text.is_a?(Hash)
|
||||
options = text
|
||||
text = nil
|
||||
end
|
||||
text = method if text.nil? && method.is_a?(Symbol)
|
||||
if text.is_a?(Symbol)
|
||||
text = 'labels.#{text}' unless text.to_s =~ /\A#/
|
||||
text = t(text, options.delete(:en))
|
||||
end
|
||||
text = before_label(text) if options[:before]
|
||||
return text, options
|
||||
end
|
||||
end
|
||||
|
||||
ActionView::Base.send(:include, I18nUtilities)
|
||||
|
@ -29,21 +43,21 @@ ActionView::Helpers::FormHelper.module_eval do
|
|||
end
|
||||
|
||||
def label_with_symbol_translation(object_name, method, text = nil, options = {})
|
||||
if text.is_a?(Hash)
|
||||
options = text
|
||||
text = nil
|
||||
end
|
||||
text = method if text.nil? && method.is_a?(Symbol)
|
||||
if text.is_a?(Symbol)
|
||||
text = 'labels.#{text}' unless text.to_s =~ /\A#/
|
||||
text = t(text, options.delete(:en))
|
||||
end
|
||||
text = before_label(text) if options[:before]
|
||||
text, options = _label_symbol_translation(method, text, options)
|
||||
label_without_symbol_translation(object_name, method, text, options)
|
||||
end
|
||||
alias_method_chain :label, :symbol_translation
|
||||
end
|
||||
|
||||
ActionView::Helpers::InstanceTag.send(:include, I18nUtilities)
|
||||
ActionView::Helpers::FormTagHelper.class_eval do
|
||||
def label_tag_with_symbol_translation(method, text = nil, options = {})
|
||||
text, options = _label_symbol_translation(method, text, options)
|
||||
label_tag_without_symbol_translation(method, text, options)
|
||||
end
|
||||
alias_method_chain :label_tag, :symbol_translation
|
||||
end
|
||||
|
||||
ActionView::Helpers::FormBuilder.class_eval do
|
||||
def blabel(method, text = nil, options = {})
|
||||
if text.is_a?(Hash)
|
||||
|
|
|
@ -19,7 +19,7 @@ class I18nExtractor < SexpProcessor
|
|||
end
|
||||
|
||||
TRANSLATE_CALLS = [:t, :ot, :mt, :translate, :before_label]
|
||||
LABEL_CALLS = [:label, :blabel]
|
||||
LABEL_CALLS = [:label, :blabel, :label_tag, :_label_symbol_translation]
|
||||
ALL_CALLS = TRANSLATE_CALLS + LABEL_CALLS + [:label_with_symbol_translation]
|
||||
|
||||
def process_call(exp)
|
||||
|
@ -110,9 +110,11 @@ class I18nExtractor < SexpProcessor
|
|||
# label :bar, :foo, :foo_key, :en => "Foo"
|
||||
# f.label :foo, :en => "Foo"
|
||||
# f.label :foo, :foo_key, :en => "Foo"
|
||||
# label_tag :foo, :en => "Foo"
|
||||
# label_tag :foo, :foo_key, :en => "Foo"
|
||||
def process_label_call(receiver, method, args)
|
||||
args.shift
|
||||
args.shift unless receiver # remove object_name arg
|
||||
args.shift unless receiver || method.to_s == 'label_tag' # remove object_name arg
|
||||
|
||||
inferred = false
|
||||
default = nil
|
||||
|
@ -218,4 +220,4 @@ class I18nExtractor < SexpProcessor
|
|||
hash[key] = default
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -89,31 +89,37 @@ describe I18nExtractor do
|
|||
it "should interpret symbol names as the key" do
|
||||
extract("label :thing, :the_foo, :foo, :en => 'Foo'").should == {'labels' => {"foo" => "Foo"}}
|
||||
extract("f.label :the_foo, :foo, :en => 'Foo'").should == {'labels' => {"foo" => "Foo"}}
|
||||
extract("label_tag :the_foo, :foo, :en => 'Foo'").should == {'labels' => {"foo" => "Foo"}}
|
||||
end
|
||||
|
||||
it "should infer the key from the method if not provided" do
|
||||
extract("label :thing, :the_foo, :en => 'Foo'").should == {'labels' => {"the_foo" => "Foo"}}
|
||||
extract("f.label :the_foo, :en => 'Foo'").should == {'labels' => {"the_foo" => "Foo"}}
|
||||
extract("label_tag :the_foo, :en => 'Foo'").should == {'labels' => {"the_foo" => "Foo"}}
|
||||
end
|
||||
|
||||
it "should skip label calls with non-symbol keys (i.e. just a standard label)" do
|
||||
extract("label :thing, :the_foo, 'foo'").should == {}
|
||||
extract("f.label :the_foo, 'foo'").should == {}
|
||||
extract("label_tag :thing, 'foo'").should == {}
|
||||
end
|
||||
|
||||
it "should complain if a label call has a non-symbol key and a default" do
|
||||
lambda{ extract "label :thing, :the_foo, 'foo', :en => 'Foo'" }.should raise_error /invalid translation key/
|
||||
lambda{ extract "f.label :the_foo, 'foo', :en => 'Foo'" }.should raise_error /invalid translation key/
|
||||
lambda{ extract "label_tag :the_foo, 'foo', :en => 'Foo'" }.should raise_error /invalid translation key/
|
||||
end
|
||||
|
||||
it "should not auto-scope absolute keys" do
|
||||
extract("label :thing, :the_foo, :'#foo', :en => 'Foo'", '').should == {"foo" => "Foo"}
|
||||
extract("f.label :the_foo, :'#foo', :en => 'Foo'", '').should == {"foo" => "Foo"}
|
||||
extract("label_tag :the_foo, :'#foo', :en => 'Foo'", '').should == {"foo" => "Foo"}
|
||||
end
|
||||
|
||||
it "should complain if no default is provided" do
|
||||
lambda{ extract "label :thing, :the_foo, :foo" }.should raise_error 'invalid/missing en default nil'
|
||||
lambda{ extract "f.label :the_foo, :foo" }.should raise_error 'invalid/missing en default nil'
|
||||
lambda{ extract "label_tag :the_foo, :foo" }.should raise_error 'invalid/missing en default nil'
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue