Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit a7a9838b5b69f1b657c75530614c8e1cb3cb6d3f 1 parent 49abcd9
@coderifous authored
View
44 build/jquery.localize.js
@@ -1,9 +1,6 @@
-// Generated by CoffeeScript 1.3.3
(function() {
var $, normaliseLang;
-
$ = jQuery;
-
normaliseLang = function(lang) {
lang = lang.replace(/_/, '-').toLowerCase();
if (lang.length > 3) {
@@ -11,11 +8,9 @@
}
return lang;
};
-
$.defaultLanguage = normaliseLang(navigator.language || navigator.userLanguage);
-
$.localize = function(pkg, options) {
- var defaultCallback, fileExtension, intermediateLangData, jsonCall, lang, loadLanguage, notifyDelegateLanguageLoaded, regexify, valueForKey, wrappedSet;
+ var applyValueToAttribute, defaultCallback, fileExtension, intermediateLangData, jsonCall, lang, loadLanguage, notifyDelegateLanguageLoaded, regexify, valueForKey, wrappedSet;
if (options == null) {
options = {};
}
@@ -84,27 +79,24 @@
value = valueForKey(key, data);
if (elem.is('input')) {
if (elem.is("[placeholder]")) {
- return elem.attr("placeholder", value);
+ elem.attr("placeholder", value);
} else {
- return elem.val(value);
+ elem.val(value);
}
} else if (elem.is('optgroup')) {
- return elem.attr("label", value);
+ elem.attr("label", value);
} else if (elem.is('img')) {
- value = valueForKey("" + key + ".alt", data);
- if (value != null) {
- elem.attr("alt", value);
- }
- value = valueForKey("" + key + ".src", data);
- if (value != null) {
- elem.attr("src", value);
- }
- value = valueForKey("" + key + ".title", data);
+ applyValueToAttribute(key, "alt", data, elem);
+ applyValueToAttribute(key, "src", data, elem);
+ } else if (!$.isPlainObject(value)) {
+ elem.html(value);
+ }
+ if ($.isPlainObject(value)) {
+ applyValueToAttribute(key, "title", data, elem);
+ value = valueForKey("" + key + ".text", data);
if (value != null) {
- return elem.attr("title", value);
+ return elem.text(value);
}
- } else {
- return elem.html(value);
}
});
};
@@ -125,6 +117,13 @@
}
return value;
};
+ applyValueToAttribute = function(key, attribute, data, elem) {
+ var value;
+ value = valueForKey("" + key + "." + attribute, data);
+ if (value != null) {
+ return elem.attr(attribute, value);
+ }
+ };
regexify = function(string_or_regex_or_array) {
var thing;
if (typeof string_or_regex_or_array === "string") {
@@ -149,9 +148,6 @@
}
return wrappedSet;
};
-
$.fn.localize = $.localize;
-
$.localize.data = {};
-
}).call(this);
View
19 src/jquery.localize.coffee
@@ -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
6 test/lang/test-ja.json
@@ -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"
+ }
}
View
6 test/localize_test.coffee
@@ -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

@grahams

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.
Something went wrong with that request. Please try again.