Skip to content

Commit ea351b1

Browse files
committed
Added default support for HTML5's custom data attributes (data-localize).
Closes #14
1 parent ddc81b9 commit ea351b1

9 files changed

+76
-61
lines changed

README.markdown

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
It's the file located at `build/jquery.localize.js`
1616

17-
## Step 1. Use the "rel" attribute on tags whose content you want to be translated
17+
## Step 1. Mark up tags whose content you want to be translated
1818

1919
Somewhere in your html:
2020

21-
<h1 rel="localize[greeting]"> Hello! </h1>
21+
<h1 data-localize="greeting"> Hello! </h1>
2222

2323
## Step 2. Provide a JSON language file that has translations:
2424

@@ -31,10 +31,10 @@ example-fr.json:
3131
## Step 3. Use the localize plugin.
3232

3333
// In a browser where the language is set to French
34-
$("[rel*=localize]").localize("example")
34+
$("[data-localize]").localize("example")
3535

3636
// You can also override the language detection, and pass in a language code
37-
$("[rel*=localize]").localize("example", { language: "fr" })
37+
$("[data-localize]").localize("example", { language: "fr" })
3838

3939
# Gory Details
4040

@@ -44,7 +44,7 @@ The first argument of the localize method is the name of the language pack. You
4444

4545
Here's an example of loading several language packs:
4646

47-
$("[rel*=localize]")
47+
$("[data-localize]")
4848
.localize("header")
4949
.localize("sidebar")
5050
.localize("footer")
@@ -72,30 +72,30 @@ You can tell the localize plugin to always skip certain languages using the skip
7272

7373
# using a string will skip ONLY if the language code matches exactly
7474
# this would prevent loading only if the language was "en-US"
75-
$("[rel*=localize]").localize("example", { skipLanguage: "en-US" })
75+
$("[data-localize]").localize("example", { skipLanguage: "en-US" })
7676

7777
# using a regex will skip if the regex matches
7878
# this would prevent loading of any english language translations
79-
$("[rel*=localize]").localize("example", { skipLanguage: /^en/ })
79+
$("[data-localize]").localize("example", { skipLanguage: /^en/ })
8080

8181
# using an array of strings will skip if any of the strings matches exactly
82-
$("[rel*=localize]").localize("example", { skipLanguage: ["en", "en-US"] })
82+
$("[data-localize]").localize("example", { skipLanguage: ["en", "en-US"] })
8383

8484
## Applying the language file
8585

86-
If you rely on the default callback and follow the "rel" attribute conventions then the changes will be applied for you.
86+
If you rely on the default callback and use the "data-localize" attribute then the changes will be applied for you.
8787

8888
## Examples:
8989

9090
**HTML:**
9191

92-
<p rel="localize[title]">Tracker Pro XT Deluxe</p>
93-
<p rel="localize[search.placeholder]">Search...</p>
94-
<p rel="localize[search.button]">Go!</p>
95-
<p rel="localize[footer.disclaimer]">Use at your own risk.</p>
96-
<p rel="localize[menu.dashboard]">Dashboard</p>
97-
<p rel="localize[menu.list]">Bug List</p>
98-
<p rel="localize[menu.logout]">Logout</p>
92+
<p data-localize="title">Tracker Pro XT Deluxe</p>
93+
<p data-localize="search.placeholder">Search...</p>
94+
<p data-localize="search.button">Go!</p>
95+
<p data-localize="footer.disclaimer">Use at your own risk.</p>
96+
<p data-localize="menu.dashboard">Dashboard</p>
97+
<p data-localize="menu.list">Bug List</p>
98+
<p data-localize="menu.logout">Logout</p>
9999

100100
**application-es.json (fake spanish)**
101101

@@ -117,13 +117,13 @@ If you rely on the default callback and follow the "rel" attribute conventions t
117117

118118
**Localize it!**
119119

120-
$("[rel*=localize]").localize("application", { language: "es" })
120+
$("[data-localize]").localize("application", { language: "es" })
121121

122122
## Callbacks
123123

124124
You can provide a callback if you want to augment or replace the default callback provided by the plugin. Your callback should take at least 1 argument: the language data (contents of your json file). It can optionally accept a second argument, which is a reference to the default callback function. This is handy if you still want the default behavior, but also need to do something else with the language data.
125125

126-
$("[rel*=localize]").localize("application", {
126+
$("[data-localize]").localize("application", {
127127
language: "es",
128128
callback: function(data, defaultCallback){
129129
data.title = data.title + currentBugName();

build/jquery.localize.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.localize.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ $.localize = (pkg, options = {}) ->
6363
$.localize.data[pkg] = data
6464
wrappedSet.each ->
6565
elem = $(this)
66-
key = elem.attr("rel").match(/localize\[(.*?)\]/)[1]
66+
key = elem.data("localize")
67+
key ||= elem.attr("rel").match(/localize\[(.*?)\]/)[1]
6768
value = valueForKey(key, data)
6869
if elem.is('input')
6970
if elem.is("[placeholder]")

test/examples/absolute_prefix_path.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
<body>
1414
<h1>Test localization...</h1>
15-
<p rel="localize[test.nested]">puts 2 + 2</p>
16-
<p rel="localize[basic]">It failed :(</p>
15+
<p data-localize="test.nested">puts 2 + 2</p>
16+
<p data-localize="basic">It failed :(</p>
1717
<script type="text/javascript" charset="utf-8">
1818
$(function(){
1919
var opts = { language: "ja", pathPrefix: "/test/lang", skipLanguage: "en-US" };
20-
$("[rel*=localize]").localize("test", opts)
20+
$("[data-localize]").localize("test", opts)
2121
})
2222
</script>
2323
</body>

test/examples/basic_usage.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212

1313
<body>
1414
<h1>Test localization...</h1>
15-
<p rel="localize[test.nested]">puts 2 + 2</p>
16-
<input rel="localize[test.input]" type="text" value="fail" />
15+
<p data-localize="test.nested">puts 2 + 2</p>
16+
<input data-localize="test.input" type="text" value="fail" />
1717
<select>
18-
<optgroup rel="localize[test.optgroup]" label="Fail">
19-
<option rel="localize[test.option]" value="1">Fail</option>
18+
<optgroup data-localize="test.optgroup" label="Fail">
19+
<option data-localize="test.option" value="1">Fail</option>
2020
</optgroup>
2121
</select>
2222
<p>
23-
<img src="ruby_square.gif" alt="a square ruby" rel="localize[test.ruby_image]"/>
23+
<img src="ruby_square.gif" alt="a square ruby" data-localize="test.ruby_image"/>
2424
Ruby image should be round.
2525
</p>
26-
<p rel="localize[basic]">It failed :(</p>
26+
<p data-localize="basic">It failed :(</p>
2727
<script type="text/javascript" charset="utf-8">
2828
$(function(){
2929
var opts = { language: "ja", pathPrefix: "../lang", skipLanguage: "en-US" };
30-
$("[rel*=localize]").localize("test", opts)
30+
$("[data-localize]").localize("test", opts)
3131
})
3232
</script>
3333
</body>

test/examples/custom_callback_usage.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212

1313
<body>
1414
<h1>Test localization...</h1>
15-
<p rel="localize[test.nested]">puts 2 + 2</p>
16-
<p rel="localize[basic]">It failed :(</p>
15+
<p data-localize="test.nested">puts 2 + 2</p>
16+
<p data-localize="basic">It failed :(</p>
17+
<p data-localize="message">Optional callback never happened.</p>
1718
<script type="text/javascript" charset="utf-8">
1819
$(function(){
1920
var opts = { language: "ja", pathPrefix: "../lang", skipLanguage: "en-US" };
2021
opts.callback = function(data, defaultCallback) {
2122
data.message = "Optional call back works."
22-
$("[rel*=basic]").css({color: "#FF8833"})
23+
$("[data-localize]").css({color: "#FF8833"})
2324
defaultCallback(data);
2425
}
25-
$("[rel*=localize]").localize("test", opts)
26+
$("[data-localize]").localize("test", opts)
2627
})
2728
</script>
2829
</body>

test/examples/language_and_country.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
<body>
1414
<h1>Test localization...</h1>
15-
<p rel="localize[message]">It failed :(</p>
15+
<p data-localize="message">It failed :(</p>
1616
<script type="text/javascript" charset="utf-8">
1717
$(function(){
1818
var opts = { language: "ja-XX", pathPrefix: "../lang", skipLanguage: "en-US" };
19-
$("[rel*=localize]").localize("test", opts)
19+
$("[data-localize]").localize("test", opts)
2020
})
2121
</script>
2222
</body>

test/examples/skip_language.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212

1313
<body>
1414
<h1>Test localization...</h1>
15-
<p rel="localize[en_message]">Should not load en lang file.</p>
16-
<p rel="localize[en_us_message]">Should not load en-US lang file.</p>
15+
<p data-localize="en_message">Should not load en lang file.</p>
16+
<p data-localize="en_us_message">Should not load en-US lang file.</p>
1717
<script type="text/javascript" charset="utf-8">
1818
$(function(){
1919
var opts = { language: "en", pathPrefix: "lang", skipLanguage: "en" };
20-
$("[rel*=localize]").localize("test", opts)
20+
$("[data-localize]").localize("test", opts)
2121

2222
var opts = { language: "en-US", pathPrefix: "lang", skipLanguage: /^en/ };
23-
$("[rel*=localize]").localize("test", opts)
23+
$("[data-localize]").localize("test", opts)
2424

2525
var opts = { language: "en", pathPrefix: "lang", skipLanguage: ["en", "en-US"] };
26-
$("[rel*=localize]").localize("test", opts)
26+
$("[data-localize]").localize("test", opts)
2727

2828
var opts = { language: "en-US", pathPrefix: "lang", skipLanguage: ["en", "en-US"] };
29-
$("[rel*=localize]").localize("test", opts)
29+
$("[data-localize]").localize("test", opts)
3030
})
3131
</script>
3232
</body>

test/localize_test.coffee

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,64 @@
1-
localizableTag = (tag, localizeKey, attributes) ->
1+
localizableTagWithRel = (tag, localizeKey, attributes) ->
22
t = $("<#{tag}>").attr("rel", "localize[#{localizeKey}]")
3+
applyTagAttributes(t, attributes)
4+
5+
localizableTagWithDataLocalize = (tag, localizeKey, attributes) ->
6+
t = $("<#{tag}>").attr("data-localize", localizeKey)
7+
applyTagAttributes(t, attributes)
8+
9+
applyTagAttributes = (tag, attributes) ->
310
if attributes.text?
4-
t.text(attributes.text)
11+
tag.text(attributes.text)
512
delete attributes.text
613
if attributes.val?
7-
t.val(attributes.val)
14+
tag.val(attributes.val)
815
delete attributes.val
9-
t.attr(k,v) for k, v of attributes
10-
t
16+
tag.attr(k,v) for k, v of attributes
17+
tag
1118

1219
module "Basic Usage"
1320

1421
setup ->
1522
@testOpts = language: "ja", pathPrefix: "lang"
1623

1724
test "basic tag text substitution", ->
18-
t = localizableTag("p", "basic", text: "basic fail")
25+
t = localizableTagWithRel("p", "basic", text: "basic fail")
26+
t.localize("test", @testOpts)
27+
equals t.text(), "basic success"
28+
29+
test "basic tag text substitution using data-localize instead of rel", ->
30+
t = localizableTagWithDataLocalize("p", "basic", text: "basic fail")
1931
t.localize("test", @testOpts)
2032
equals t.text(), "basic success"
2133

2234
test "basic tag text substitution with nested key", ->
23-
t = localizableTag("p", "test.nested", text: "nested fail")
35+
t = localizableTagWithRel("p", "test.nested", text: "nested fail")
2436
t.localize("test", @testOpts)
2537
equals t.text(), "nested success"
2638

2739
test "input tag value substitution", ->
28-
t = localizableTag("input", "test.input", val: "input fail")
40+
t = localizableTagWithRel("input", "test.input", val: "input fail")
2941
t.localize("test", @testOpts)
3042
equals t.val(), "input success"
3143

3244
test "input tag placeholder substitution", ->
33-
t = localizableTag("input", "test.input", placeholder: "placeholder fail")
45+
t = localizableTagWithRel("input", "test.input", placeholder: "placeholder fail")
3446
t.localize("test", @testOpts)
3547
equals t.attr("placeholder"), "input success"
3648

3749
test "image tag src and alt substitution", ->
38-
t = localizableTag("img", "test.ruby_image", src: "ruby_square.gif", alt: "a square ruby")
50+
t = localizableTagWithRel("img", "test.ruby_image", src: "ruby_square.gif", alt: "a square ruby")
3951
t.localize("test", @testOpts)
4052
equals t.attr("src"), "ruby_round.gif"
4153
equals t.attr("alt"), "a round ruby"
4254

4355
test "chained call", ->
44-
t = localizableTag("p", "basic", text: "basic fail")
56+
t = localizableTagWithRel("p", "basic", text: "basic fail")
4557
t.localize("test", @testOpts).localize("test", @testOpts)
4658
equals t.text(), "basic success"
4759

4860
test "alternative file extension", ->
49-
t = localizableTag("p", "basic", text: "basic fail")
61+
t = localizableTagWithRel("p", "basic", text: "basic fail")
5062
t.localize("test", $.extend({ fileExtension: "foo" }, @testOpts))
5163
equals t.text(), "basic success foo"
5264

@@ -73,7 +85,7 @@ module "Options"
7385

7486
test "pathPrefix loads lang files from custom path", ->
7587
opts = language: "fo", pathPrefix: "/test/lang/custom"
76-
t = localizableTag("p", "path_prefix", text: "pathPrefix fail")
88+
t = localizableTagWithRel("p", "path_prefix", text: "pathPrefix fail")
7789
t.localize("test", opts)
7890
equals t.text(), "pathPrefix success"
7991

@@ -82,37 +94,37 @@ test "custom callback is fired", ->
8294
opts.callback = (data, defaultCallback) ->
8395
data.custom_callback = "custom callback success"
8496
defaultCallback(data)
85-
t = localizableTag("p", "custom_callback", text: "custom callback fail")
97+
t = localizableTagWithRel("p", "custom_callback", text: "custom callback fail")
8698
t.localize("test", opts)
8799
equals t.text(), "custom callback success"
88100

89101
test "language with country code", ->
90102
opts = language: "ja-XX", pathPrefix: "lang"
91-
t = localizableTag("p", "message", text: "country code fail")
103+
t = localizableTagWithRel("p", "message", text: "country code fail")
92104
t.localize("test", opts)
93105
equals t.text(), "country code success"
94106

95107
module "Language optimization"
96108

97109
test "skipping language using string match", ->
98110
opts = language: "en", pathPrefix: "lang", skipLanguage: "en"
99-
t = localizableTag("p", "en_message", text: "en not loaded")
111+
t = localizableTagWithRel("p", "en_message", text: "en not loaded")
100112
t.localize("test", opts)
101113
equals t.text(), "en not loaded"
102114

103115
test "skipping language using regex match", ->
104116
opts = language: "en-US", pathPrefix: "lang", skipLanguage: /^en/
105-
t = localizableTag("p", "en_us_message", text: "en-US not loaded")
117+
t = localizableTagWithRel("p", "en_us_message", text: "en-US not loaded")
106118
t.localize("test", opts)
107119
equals t.text(), "en-US not loaded"
108120

109121
test "skipping language using array match", ->
110122
opts = language: "en", pathPrefix: "lang", skipLanguage: ["en", "en-US"]
111-
t = localizableTag("p", "en_message", text: "en not loaded")
123+
t = localizableTagWithRel("p", "en_message", text: "en not loaded")
112124
t.localize("test", opts)
113125
equals t.text(), "en not loaded"
114126

115127
opts = language: "en-US", pathPrefix: "lang", skipLanguage: ["en", "en-US"]
116-
t = localizableTag("p", "en_us_message", text: "en-US not loaded")
128+
t = localizableTagWithRel("p", "en_us_message", text: "en-US not loaded")
117129
t.localize("test", opts)
118130
equals t.text(), "en-US not loaded"

0 commit comments

Comments
 (0)