Skip to content

Commit a7a9838

Browse files
committed
Added support for localizing the title of any element. (not just images)
1 parent 49abcd9 commit a7a9838

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

build/jquery.localize.js

Lines changed: 20 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.localize.coffee

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,16 @@ $.localize = (pkg, options = {}) ->
7474
else if elem.is('optgroup')
7575
elem.attr("label", value)
7676
else if elem.is('img')
77-
value = valueForKey("#{key}.alt", data)
78-
elem.attr("alt", value) if value?
79-
value = valueForKey("#{key}.src", data)
80-
elem.attr("src", value) if value?
81-
value = valueForKey("#{key}.title", data)
82-
elem.attr("title", value) if value?
83-
else
77+
applyValueToAttribute(key, "alt", data, elem)
78+
applyValueToAttribute(key, "src", data, elem)
79+
else unless $.isPlainObject(value)
8480
elem.html(value)
8581

82+
if $.isPlainObject(value)
83+
applyValueToAttribute(key, "title", data, elem)
84+
value = valueForKey("#{key}.text", data)
85+
elem.text(value) if value?
86+
8687
notifyDelegateLanguageLoaded = (data) ->
8788
if options.callback?
8889
options.callback(data, defaultCallback)
@@ -96,6 +97,10 @@ $.localize = (pkg, options = {}) ->
9697
value = if value? then value[key] else null
9798
value
9899

100+
applyValueToAttribute = (key, attribute, data, elem) ->
101+
value = valueForKey("#{key}.#{attribute}", data)
102+
elem.attr(attribute, value) if value?
103+
99104
regexify = (string_or_regex_or_array) ->
100105
if typeof(string_or_regex_or_array) == "string"
101106
"^" + string_or_regex_or_array + "$"

test/lang/test-ja.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@
1010
"title": "A Round Ruby"
1111
}
1212
},
13-
"basic": "basic success"
13+
"basic": "basic success",
14+
"with_title": {
15+
"text": "with_title text success",
16+
"title": "with_title title success"
17+
}
1418
}

test/localize_test.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ test "basic tag text substitution with nested key", ->
3636
t.localize("test", @testOpts)
3737
equals t.text(), "nested success"
3838

39+
test "basic tag text substitution for special title key", ->
40+
t = localizableTagWithDataLocalize("p", "with_title", text: "with_title element fail", title: "with_title title fail")
41+
t.localize("test", @testOpts)
42+
equals t.text(), "with_title text success"
43+
equals t.attr("title"), "with_title title success"
44+
3945
test "input tag value substitution", ->
4046
t = localizableTagWithRel("input", "test.input", val: "input fail")
4147
t.localize("test", @testOpts)

0 commit comments

Comments
 (0)