Permalink
Browse files

Added support for localizing the title of any element. (not just images)

  • Loading branch information...
1 parent 49abcd9 commit a7a9838b5b69f1b657c75530614c8e1cb3cb6d3f @coderifous committed Jun 15, 2012
Showing with 43 additions and 32 deletions.
  1. +20 −24 build/jquery.localize.js
  2. +12 −7 src/jquery.localize.coffee
  3. +5 −1 test/lang/test-ja.json
  4. +6 −0 test/localize_test.coffee
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -74,15 +74,16 @@ $.localize = (pkg, options = {}) ->
else if elem.is('optgroup')
elem.attr("label", value)
else if elem.is('img')
- value = valueForKey("#{key}.alt", data)
- elem.attr("alt", value) if value?
- value = valueForKey("#{key}.src", data)
- elem.attr("src", value) if value?
- value = valueForKey("#{key}.title", data)
- elem.attr("title", value) if value?
- else
+ applyValueToAttribute(key, "alt", data, elem)
+ applyValueToAttribute(key, "src", data, elem)
+ else unless $.isPlainObject(value)
elem.html(value)
+ if $.isPlainObject(value)
+ applyValueToAttribute(key, "title", data, elem)
+ value = valueForKey("#{key}.text", data)
+ elem.text(value) if value?
+
notifyDelegateLanguageLoaded = (data) ->
if options.callback?
options.callback(data, defaultCallback)
@@ -96,6 +97,10 @@ $.localize = (pkg, options = {}) ->
value = if value? then value[key] else null
value
+ applyValueToAttribute = (key, attribute, data, elem) ->
+ value = valueForKey("#{key}.#{attribute}", data)
+ elem.attr(attribute, value) if value?
+
regexify = (string_or_regex_or_array) ->
if typeof(string_or_regex_or_array) == "string"
"^" + string_or_regex_or_array + "$"
View
@@ -10,5 +10,9 @@
"title": "A Round Ruby"
}
},
- "basic": "basic success"
+ "basic": "basic success",
+ "with_title": {
+ "text": "with_title text success",
+ "title": "with_title title success"
+ }
}
@@ -36,6 +36,12 @@ test "basic tag text substitution with nested key", ->
t.localize("test", @testOpts)
equals t.text(), "nested success"
+test "basic tag text substitution for special title key", ->
+ t = localizableTagWithDataLocalize("p", "with_title", text: "with_title element fail", title: "with_title title fail")
+ t.localize("test", @testOpts)
+ equals t.text(), "with_title text success"
+ equals t.attr("title"), "with_title title success"
+
test "input tag value substitution", ->
t = localizableTagWithRel("input", "test.input", val: "input fail")
t.localize("test", @testOpts)

1 comment on commit a7a9838

Contributor

grahams commented on a7a9838 Oct 23, 2012

I feel like I'm missing something obvious, but is there a way to set both the value and the title of an input type=button?

Please sign in to comment.