|
| 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