Skip to content

Commit de95f88

Browse files
committed
Ported to CoffeeScript
* src/jquery.localize.coffee is where the plugin src lives. * build/jquery.localize.js is generated from the coffee, checked in for convenience. * the included builder script runs coffee in watcher-mode and directs output to the build/ directory. * tests updated to point at build/jquery.localize.js
1 parent d220619 commit de95f88

11 files changed

+249
-155
lines changed

README.markdown

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@ Synopsis
66

77
* Lazily loads JSON translation files based on a simple naming convention.
88
* By default, applies the translations to your document based on simple attribute convention.
9-
* Recently updated for jQuery 1.5
9+
* Recently updated for jQuery 1.5 (tests use jQuery 1.5.2)
1010

1111
Basic Usage
1212
-----------
1313

14+
Step 0. Load the jquery-localize plugin on your page.
15+
-----------------------------------------------------
16+
17+
It's the file located at `build/jquery.localize.js`
18+
1419
Step 1. Use the "rel" attribute on tags whose content you want to be translated
15-
--------------------------------------------------------------------------
20+
-------------------------------------------------------------------------------
1621

1722
Somewhere in your html:
1823

1924
<h1 rel="localize[greeting]"> Hello! </h1>
2025

2126
Step 2. Provide a JSON language file that has translations:
22-
----------------------------------------------------------
27+
-----------------------------------------------------------
2328

2429
example-fr.json:
2530

build/README

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Files in this directory are generated.
2+
3+
Do not edit any files in this directory.
4+
5+
These files are only checked in to git for the convenience of people that want
6+
to use the javascript library without having to download the repository and
7+
build it on their machine.

build/jquery.localize.js

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

builder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coffee -wco build src/jquery.localize.coffee

src/jquery.localize.coffee

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Copyright (c) Jim Garvin (http://github.com/coderifous), 2008.
2+
# Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
3+
# Written by Jim Garvin (@coderifous) for use on LMGTFY.com.
4+
# http://github.com/coderifous/jquery-localize
5+
# Based off of Keith Wood's Localisation jQuery plugin.
6+
# http://keith-wood.name/localisation.html
7+
8+
$ = jQuery
9+
10+
# Ensures language code is in the format aa-AA.
11+
normaliseLang = (lang) ->
12+
lang = lang.replace(/_/, '-').toLowerCase()
13+
if lang.length > 3
14+
lang = lang.substring(0, 3) + lang.substring(3).toUpperCase()
15+
lang
16+
17+
# Mozilla uses .language, IE uses .userLanguage
18+
$.defaultLanguage = normaliseLang(navigator.language || navigator.userLanguage)
19+
20+
$.localize = (pkg, options = {}) ->
21+
wrappedSet = this
22+
intermediateLangData = {}
23+
24+
loadLanguage = (pkg, lang, level = 1) ->
25+
switch level
26+
when 1
27+
intermediateLangData = {}
28+
if options.loadBase
29+
file = pkg + '.json'
30+
jsonCall(file, pkg, lang, level)
31+
else
32+
loadLanguage(pkg, lang, 2)
33+
when 2
34+
if lang.length >= 2
35+
file = "#{pkg}-#{lang.substring(0, 2)}.json"
36+
jsonCall(file, pkg, lang, level)
37+
when 3
38+
if lang.length >= 5
39+
file = "#{pkg}-#{lang.substring(0, 5)}.json"
40+
jsonCall(file, pkg, lang, level)
41+
42+
jsonCall = (file, pkg, lang, level) ->
43+
file = "#{options.pathPrefix}/#{file}" if options.pathPrefix?
44+
successFunc = (d) ->
45+
$.extend(intermediateLangData, d)
46+
notifyDelegateLanguageLoaded(intermediateLangData)
47+
loadLanguage(pkg, lang, level + 1)
48+
ajaxOptions =
49+
url: file
50+
dataType: "json"
51+
async: false
52+
timeout: if options.timeout? then options.timeout else 500
53+
success: successFunc
54+
# hack to work with serving from local file system.
55+
# local file:// urls won't work in chrome:
56+
# http://code.google.com/p/chromium/issues/detail?id=40787
57+
if window.location.protocol == "file:"
58+
ajaxOptions.error = (xhr) -> successFunc($.parseJSON(xhr.responseText))
59+
$.ajax(ajaxOptions)
60+
61+
defaultCallback = (data) ->
62+
$.localize.data[pkg] = data
63+
wrappedSet.each ->
64+
elem = $(this)
65+
key = elem.attr("rel").match(/localize\[(.*?)\]/)[1]
66+
value = valueForKey(key, data)
67+
if elem.is('input')
68+
elem.val(value)
69+
else if elem.is('optgroup')
70+
elem.attr("label", value)
71+
else
72+
elem.html(value)
73+
74+
notifyDelegateLanguageLoaded = (data) ->
75+
if options.callback?
76+
options.callback(data, defaultCallback)
77+
else
78+
defaultCallback(data)
79+
80+
valueForKey = (key, data) ->
81+
keys = key.split(/\./)
82+
value = data
83+
for key in keys
84+
value = if value? then value[key] else null
85+
value
86+
87+
regexify = (string_or_regex_or_array) ->
88+
if typeof(string_or_regex_or_array) == "string"
89+
"^" + string_or_regex_or_array + "$"
90+
else if string_or_regex_or_array.length?
91+
(regexify(thing) for thing in string_or_regex_or_array).join("|")
92+
else
93+
string_or_regex_or_array
94+
95+
lang = normaliseLang(if options.language then options.language else $.defaultLanguage)
96+
loadLanguage(pkg, lang, 1) unless (options.skipLanguage && lang.match(regexify(options.skipLanguage)))
97+
98+
$.fn.localize = $.localize
99+
$.localize.data = {}

0 commit comments

Comments
 (0)