| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
( function ( $ ) { |
| 17 |
'use strict'; |
| 18 |
|
| 19 |
var I18N, |
| 20 |
slice = Array.prototype.slice; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
I18N = function ( options ) { |
| 26 |
|
| 27 |
this.options = $.extend( {}, I18N.defaults, options ); |
| 28 |
|
| 29 |
this.parser = this.options.parser; |
| 30 |
this.locale = this.options.locale; |
| 31 |
this.messageStore = this.options.messageStore; |
| 32 |
this.languages = {}; |
| 33 |
}; |
| 34 |
|
| 35 |
I18N.prototype = { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
localize: function ( messageKey ) { |
| 42 |
var localeParts, localePartIndex, locale, fallbackIndex, |
| 43 |
tryingLocale, message; |
| 44 |
|
| 45 |
locale = this.locale; |
| 46 |
fallbackIndex = 0; |
| 47 |
|
| 48 |
while ( locale ) { |
| 49 |
|
| 50 |
|
| 51 |
localeParts = locale.split( '-' ); |
| 52 |
localePartIndex = localeParts.length; |
| 53 |
|
| 54 |
do { |
| 55 |
tryingLocale = localeParts.slice( 0, localePartIndex ).join( '-' ); |
| 56 |
message = this.messageStore.get( tryingLocale, messageKey ); |
| 57 |
|
| 58 |
if ( message ) { |
| 59 |
return message; |
| 60 |
} |
| 61 |
|
| 62 |
localePartIndex--; |
| 63 |
} while ( localePartIndex ); |
| 64 |
|
| 65 |
if ( locale === this.options.fallbackLocale ) { |
| 66 |
break; |
| 67 |
} |
| 68 |
|
| 69 |
locale = ( $.i18n.fallbacks[ this.locale ] && |
| 70 |
$.i18n.fallbacks[ this.locale ][ fallbackIndex ] ) || |
| 71 |
this.options.fallbackLocale; |
| 72 |
$.i18n.log( 'Trying fallback locale for ' + this.locale + ': ' + locale + ' (' + messageKey + ')' ); |
| 73 |
|
| 74 |
fallbackIndex++; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
return ''; |
| 79 |
}, |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
destroy: function () { |
| 85 |
$.removeData( document, 'i18n' ); |
| 86 |
}, |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
load: function ( source, locale ) { |
| 131 |
var fallbackLocales, locIndex, fallbackLocale, sourceMap = {}; |
| 132 |
if ( !source && !locale ) { |
| 133 |
source = 'i18n/' + $.i18n().locale + '.json'; |
| 134 |
locale = $.i18n().locale; |
| 135 |
} |
| 136 |
if ( typeof source === 'string' && |
| 137 |
|
| 138 |
source.split( '?' )[ 0 ].split( '.' ).pop() !== 'json' |
| 139 |
) { |
| 140 |
|
| 141 |
|
| 142 |
sourceMap[ locale ] = source + '/' + locale + '.json'; |
| 143 |
fallbackLocales = ( $.i18n.fallbacks[ locale ] || [] ) |
| 144 |
.concat( this.options.fallbackLocale ); |
| 145 |
for ( locIndex = 0; locIndex < fallbackLocales.length; locIndex++ ) { |
| 146 |
fallbackLocale = fallbackLocales[ locIndex ]; |
| 147 |
sourceMap[ fallbackLocale ] = source + '/' + fallbackLocale + '.json'; |
| 148 |
} |
| 149 |
return this.load( sourceMap ); |
| 150 |
} else { |
| 151 |
return this.messageStore.load( source, locale ); |
| 152 |
} |
| 153 |
|
| 154 |
}, |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
parse: function ( key, parameters ) { |
| 164 |
var message = this.localize( key ); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
this.parser.language = $.i18n.languages[ $.i18n().locale ] || $.i18n.languages[ 'default' ]; |
| 169 |
if ( message === '' ) { |
| 170 |
message = key; |
| 171 |
} |
| 172 |
return this.parser.parse( message, parameters ); |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
$.i18n = function ( key, param1 ) { |
| 186 |
var parameters, |
| 187 |
i18n = $.data( document, 'i18n' ), |
| 188 |
options = typeof key === 'object' && key; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
if ( options && options.locale && i18n && i18n.locale !== options.locale ) { |
| 198 |
i18n.locale = options.locale; |
| 199 |
} |
| 200 |
|
| 201 |
if ( !i18n ) { |
| 202 |
i18n = new I18N( options ); |
| 203 |
$.data( document, 'i18n', i18n ); |
| 204 |
} |
| 205 |
|
| 206 |
if ( typeof key === 'string' ) { |
| 207 |
if ( param1 !== undefined ) { |
| 208 |
parameters = slice.call( arguments, 1 ); |
| 209 |
} else { |
| 210 |
parameters = []; |
| 211 |
} |
| 212 |
|
| 213 |
return i18n.parse( key, parameters ); |
| 214 |
} else { |
| 215 |
|
| 216 |
return i18n; |
| 217 |
} |
| 218 |
}; |
| 219 |
|
| 220 |
$.fn.i18n = function () { |
| 221 |
var i18n = $.data( document, 'i18n' ); |
| 222 |
|
| 223 |
if ( !i18n ) { |
| 224 |
i18n = new I18N(); |
| 225 |
$.data( document, 'i18n', i18n ); |
| 226 |
} |
| 227 |
|
| 228 |
return this.each( function () { |
| 229 |
var $this = $( this ), |
| 230 |
messageKey = $this.data( 'i18n' ), |
| 231 |
lBracket, rBracket, type, key; |
| 232 |
|
| 233 |
if ( messageKey ) { |
| 234 |
lBracket = messageKey.indexOf( '[' ); |
| 235 |
rBracket = messageKey.indexOf( ']' ); |
| 236 |
if ( lBracket !== -1 && rBracket !== -1 && lBracket < rBracket ) { |
| 237 |
type = messageKey.slice( lBracket + 1, rBracket ); |
| 238 |
key = messageKey.slice( rBracket + 1 ); |
| 239 |
if ( type === 'html' ) { |
| 240 |
$this.html( i18n.parse( key ) ); |
| 241 |
} else { |
| 242 |
$this.attr( type, i18n.parse( key ) ); |
| 243 |
} |
| 244 |
} else { |
| 245 |
$this.text( i18n.parse( messageKey ) ); |
| 246 |
} |
| 247 |
} else { |
| 248 |
$this.find( '[data-i18n]' ).i18n(); |
| 249 |
} |
| 250 |
} ); |
| 251 |
}; |
| 252 |
|
| 253 |
function getDefaultLocale() { |
| 254 |
var locale = $( 'html' ).attr( 'lang' ); |
| 255 |
if ( !locale ) { |
| 256 |
locale = navigator.language || navigator.userLanguage || ''; |
| 257 |
} |
| 258 |
return locale; |
| 259 |
} |
| 260 |
|
| 261 |
$.i18n.languages = {}; |
| 262 |
$.i18n.messageStore = $.i18n.messageStore || {}; |
| 263 |
$.i18n.parser = { |
| 264 |
|
| 265 |
parse: function ( message, parameters ) { |
| 266 |
return message.replace( /\$(\d+)/g, function ( str, match ) { |
| 267 |
var index = parseInt( match, 10 ) - 1; |
| 268 |
return parameters[ index ] !== undefined ? parameters[ index ] : '$' + match; |
| 269 |
} ); |
| 270 |
}, |
| 271 |
emitter: {} |
| 272 |
}; |
| 273 |
$.i18n.fallbacks = {}; |
| 274 |
$.i18n.debug = false; |
| 275 |
$.i18n.log = function ( ) { |
| 276 |
if ( window.console && $.i18n.debug ) { |
| 277 |
window.console.log.apply( window.console, arguments ); |
| 278 |
} |
| 279 |
}; |
| 280 |
|
| 281 |
I18N.defaults = { |
| 282 |
locale: getDefaultLocale(), |
| 283 |
fallbackLocale: 'en', |
| 284 |
parser: $.i18n.parser, |
| 285 |
messageStore: $.i18n.messageStore |
| 286 |
}; |
| 287 |
|
| 288 |
|
| 289 |
$.i18n.constructor = I18N; |
| 290 |
}( jQuery ) ); |