1
0
mirror of synced 2024-11-22 21:16:10 +03:00

Added Translation object

This continues the work to get translations built into Select2.
This commit is contained in:
Kevin Brown 2014-10-31 23:03:03 -04:00
parent 20fcaa4449
commit 9bb993cee9
11 changed files with 467 additions and 41 deletions

2
dist/js/i18n/en.js vendored
View File

@ -1 +1 @@
(function(){$&&$.fn&&$.fn.select2&&$.fn.select2.amd&&(define=$.fn.select2.amd.define,require=$.fn.select2.amd.require),define("select2/i18n/en",[],function(){return{no_results:function(){return"No results found"}}}),require("jquery.select2"),$.fn.select2.amd={define:define,require:require}})();
window.$=window.$||{},function(){$&&$.fn&&$.fn.select2&&$.fn.select2.amd&&(define=$.fn.select2.amd.define,require=$.fn.select2.amd.require),define("select2/i18n/en",[],function(){return{no_results:function(){return"No results found"}}}),require("jquery.select2"),$.fn.select2.amd={define:define,require:require}}();

View File

@ -1,4 +1,4 @@
(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }define('select2/utils',[], function () {
window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }define('select2/utils',[], function () {
var Utils = {};
Utils.Extend = function (ChildClass, SuperClass) {
@ -837,6 +837,42 @@ define('select2/selection/placeholder',[
return Placeholder;
});
define('select2/translation',[
], function () {
function Translation (dict) {
this.dict = dict || {};
}
Translation.prototype.all = function () {
return this.dict;
};
Translation.prototype.get = function (key) {
return this.dict[key];
};
Translation.prototype.extend = function (translation) {
this.dict = $.extend({}, translation.all(), this.dict);
};
// Static functions
Translation._cache = {};
Translation.loadPath = function (path) {
if (!(path in Translation._cache)) {
var translations = require(path);
Translation._cache[path] = translations;
}
return new Translation(Translation._cache[path]);
};
return Translation;
});
define('select2/data/base',[
'../utils'
], function (Utils) {
@ -1345,7 +1381,16 @@ define('select2/dropdown/search',[
return Search;
});
define('select2/i18n/en',[],function () {
return {
'no_results': function () {
return 'No results found';
}
};
});
define('select2/defaults',[
'jquery',
'./results',
'./selection/single',
@ -1353,6 +1398,7 @@ define('select2/defaults',[
'./selection/placeholder',
'./utils',
'./translation',
'./data/select',
'./data/array',
@ -1360,18 +1406,20 @@ define('select2/defaults',[
'./data/tags',
'./dropdown',
'./dropdown/search'
], function (ResultsList,
'./dropdown/search',
'./i18n/en'
], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder,
Utils,
Utils, Translation,
SelectData, ArrayData, AjaxData, Tags,
Dropdown, Search) {
Dropdown, Search, EnglishTranslation) {
function Defaults () {
this.reset();
}
Defaults.prototype.apply = function (options) {
options = $.extend({}, options, this.defaults);
options = $.extend({}, this.defaults, options);
if (options.dataAdapter == null) {
if (options.ajax) {
@ -1413,11 +1461,42 @@ define('select2/defaults',[
}
}
if (typeof options.language === 'string') {
options.language = [options.language];
}
if ($.isArray(options.language)) {
var languages = new Translation();
var languageNames = options.language.concat(this.defaults.language);
for (var l = 0; l < languageNames.length; l++) {
var name = languageNames[l];
var language = {};
try {
// Try to load it with the original name
language = Translation.loadPath(name);
} catch (e) {
// If we couldn't load it, check if it wasn't the full path
name = 'select2/i18n/' + name;
language = Translation.loadPath(name);
}
languages.extend(language);
}
options.translations = languages;
} else {
options.translations = new Translations(options.language);
}
return options;
};
Defaults.prototype.reset = function () {
this.defaults = { };
this.defaults = {
language: ['select2/i18n/en']
};
};
var defaults = new Defaults();

View File

@ -1,4 +1,4 @@
(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }define('select2/utils',[], function () {
window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }define('select2/utils',[], function () {
var Utils = {};
Utils.Extend = function (ChildClass, SuperClass) {
@ -837,6 +837,42 @@ define('select2/selection/placeholder',[
return Placeholder;
});
define('select2/translation',[
], function () {
function Translation (dict) {
this.dict = dict || {};
}
Translation.prototype.all = function () {
return this.dict;
};
Translation.prototype.get = function (key) {
return this.dict[key];
};
Translation.prototype.extend = function (translation) {
this.dict = $.extend({}, translation.all(), this.dict);
};
// Static functions
Translation._cache = {};
Translation.loadPath = function (path) {
if (!(path in Translation._cache)) {
var translations = require(path);
Translation._cache[path] = translations;
}
return new Translation(Translation._cache[path]);
};
return Translation;
});
define('select2/data/base',[
'../utils'
], function (Utils) {
@ -1345,7 +1381,16 @@ define('select2/dropdown/search',[
return Search;
});
define('select2/i18n/en',[],function () {
return {
'no_results': function () {
return 'No results found';
}
};
});
define('select2/defaults',[
'jquery',
'./results',
'./selection/single',
@ -1353,6 +1398,7 @@ define('select2/defaults',[
'./selection/placeholder',
'./utils',
'./translation',
'./data/select',
'./data/array',
@ -1360,18 +1406,20 @@ define('select2/defaults',[
'./data/tags',
'./dropdown',
'./dropdown/search'
], function (ResultsList,
'./dropdown/search',
'./i18n/en'
], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder,
Utils,
Utils, Translation,
SelectData, ArrayData, AjaxData, Tags,
Dropdown, Search) {
Dropdown, Search, EnglishTranslation) {
function Defaults () {
this.reset();
}
Defaults.prototype.apply = function (options) {
options = $.extend({}, options, this.defaults);
options = $.extend({}, this.defaults, options);
if (options.dataAdapter == null) {
if (options.ajax) {
@ -1413,11 +1461,42 @@ define('select2/defaults',[
}
}
if (typeof options.language === 'string') {
options.language = [options.language];
}
if ($.isArray(options.language)) {
var languages = new Translation();
var languageNames = options.language.concat(this.defaults.language);
for (var l = 0; l < languageNames.length; l++) {
var name = languageNames[l];
var language = {};
try {
// Try to load it with the original name
language = Translation.loadPath(name);
} catch (e) {
// If we couldn't load it, check if it wasn't the full path
name = 'select2/i18n/' + name;
language = Translation.loadPath(name);
}
languages.extend(language);
}
options.translations = languages;
} else {
options.translations = new Translations(options.language);
}
return options;
};
Defaults.prototype.reset = function () {
this.defaults = { };
this.defaults = {
language: ['select2/i18n/en']
};
};
var defaults = new Defaults();

View File

@ -1,4 +1,4 @@
(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }/**
window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }/**
* @license almond 0.2.9 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/almond for details
@ -10372,6 +10372,42 @@ define('select2/selection/placeholder',[
return Placeholder;
});
define('select2/translation',[
], function () {
function Translation (dict) {
this.dict = dict || {};
}
Translation.prototype.all = function () {
return this.dict;
};
Translation.prototype.get = function (key) {
return this.dict[key];
};
Translation.prototype.extend = function (translation) {
this.dict = $.extend({}, translation.all(), this.dict);
};
// Static functions
Translation._cache = {};
Translation.loadPath = function (path) {
if (!(path in Translation._cache)) {
var translations = require(path);
Translation._cache[path] = translations;
}
return new Translation(Translation._cache[path]);
};
return Translation;
});
define('select2/data/base',[
'../utils'
], function (Utils) {
@ -10880,7 +10916,16 @@ define('select2/dropdown/search',[
return Search;
});
define('select2/i18n/en',[],function () {
return {
'no_results': function () {
return 'No results found';
}
};
});
define('select2/defaults',[
'jquery',
'./results',
'./selection/single',
@ -10888,6 +10933,7 @@ define('select2/defaults',[
'./selection/placeholder',
'./utils',
'./translation',
'./data/select',
'./data/array',
@ -10895,18 +10941,20 @@ define('select2/defaults',[
'./data/tags',
'./dropdown',
'./dropdown/search'
], function (ResultsList,
'./dropdown/search',
'./i18n/en'
], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder,
Utils,
Utils, Translation,
SelectData, ArrayData, AjaxData, Tags,
Dropdown, Search) {
Dropdown, Search, EnglishTranslation) {
function Defaults () {
this.reset();
}
Defaults.prototype.apply = function (options) {
options = $.extend({}, options, this.defaults);
options = $.extend({}, this.defaults, options);
if (options.dataAdapter == null) {
if (options.ajax) {
@ -10948,11 +10996,42 @@ define('select2/defaults',[
}
}
if (typeof options.language === 'string') {
options.language = [options.language];
}
if ($.isArray(options.language)) {
var languages = new Translation();
var languageNames = options.language.concat(this.defaults.language);
for (var l = 0; l < languageNames.length; l++) {
var name = languageNames[l];
var language = {};
try {
// Try to load it with the original name
language = Translation.loadPath(name);
} catch (e) {
// If we couldn't load it, check if it wasn't the full path
name = 'select2/i18n/' + name;
language = Translation.loadPath(name);
}
languages.extend(language);
}
options.translations = languages;
} else {
options.translations = new Translations(options.language);
}
return options;
};
Defaults.prototype.reset = function () {
this.defaults = { };
this.defaults = {
language: ['select2/i18n/en']
};
};
var defaults = new Defaults();

File diff suppressed because one or more lines are too long

93
dist/js/select2.js vendored
View File

@ -1,4 +1,4 @@
(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }/**
window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }/**
* @license almond 0.2.9 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/almond for details
@ -1265,6 +1265,42 @@ define('select2/selection/placeholder',[
return Placeholder;
});
define('select2/translation',[
], function () {
function Translation (dict) {
this.dict = dict || {};
}
Translation.prototype.all = function () {
return this.dict;
};
Translation.prototype.get = function (key) {
return this.dict[key];
};
Translation.prototype.extend = function (translation) {
this.dict = $.extend({}, translation.all(), this.dict);
};
// Static functions
Translation._cache = {};
Translation.loadPath = function (path) {
if (!(path in Translation._cache)) {
var translations = require(path);
Translation._cache[path] = translations;
}
return new Translation(Translation._cache[path]);
};
return Translation;
});
define('select2/data/base',[
'../utils'
], function (Utils) {
@ -1773,7 +1809,16 @@ define('select2/dropdown/search',[
return Search;
});
define('select2/i18n/en',[],function () {
return {
'no_results': function () {
return 'No results found';
}
};
});
define('select2/defaults',[
'jquery',
'./results',
'./selection/single',
@ -1781,6 +1826,7 @@ define('select2/defaults',[
'./selection/placeholder',
'./utils',
'./translation',
'./data/select',
'./data/array',
@ -1788,18 +1834,20 @@ define('select2/defaults',[
'./data/tags',
'./dropdown',
'./dropdown/search'
], function (ResultsList,
'./dropdown/search',
'./i18n/en'
], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder,
Utils,
Utils, Translation,
SelectData, ArrayData, AjaxData, Tags,
Dropdown, Search) {
Dropdown, Search, EnglishTranslation) {
function Defaults () {
this.reset();
}
Defaults.prototype.apply = function (options) {
options = $.extend({}, options, this.defaults);
options = $.extend({}, this.defaults, options);
if (options.dataAdapter == null) {
if (options.ajax) {
@ -1841,11 +1889,42 @@ define('select2/defaults',[
}
}
if (typeof options.language === 'string') {
options.language = [options.language];
}
if ($.isArray(options.language)) {
var languages = new Translation();
var languageNames = options.language.concat(this.defaults.language);
for (var l = 0; l < languageNames.length; l++) {
var name = languageNames[l];
var language = {};
try {
// Try to load it with the original name
language = Translation.loadPath(name);
} catch (e) {
// If we couldn't load it, check if it wasn't the full path
name = 'select2/i18n/' + name;
language = Translation.loadPath(name);
}
languages.extend(language);
}
options.translations = languages;
} else {
options.translations = new Translations(options.language);
}
return options;
};
Defaults.prototype.reset = function () {
this.defaults = { };
this.defaults = {
language: ['select2/i18n/en']
};
};
var defaults = new Defaults();

File diff suppressed because one or more lines are too long

View File

@ -257,7 +257,7 @@ $(".js-programmatic-close").on("click", function () { $example.select2("close");
</div>
</section>
<section id="tagss" class="row">
<section id="tags" class="row">
<div class="col-md-4">
<h1>Tagging support</h1>
@ -288,6 +288,40 @@ $(".js-programmatic-close").on("click", function () { $example.select2("close");
$(".js-example-tags").select2({
tags: true
})
</script>
</div>
</section>
<section id="language" class="row">
<div class="col-md-4">
<h1>Multiple languages</h1>
<p>
Select2 supports displaying the messages in different languages, as well
as provding your own custom messages that can be displayed.
</p>
<p>
<select class="js-example-language js-states form-control">
</select>
</p>
<p>
Note that when tagging is enabled the user can select from pre-existing
options or create a new tag by picking the first choice, which is what
the user has typed into the search box so far.
</p>
</div>
<div class="col-md-8">
<h2>Example code</h2>
<pre data-fill-from=".js-code-language"></pre>
<script type="text/x-example-code" class="js-code-language">
$(".js-example-language").select2({
language: "es"
})
</script>
</div>
</section>
@ -388,6 +422,8 @@ $.fn.select2.amd.require(["select2/core", "select2/utils"], function (Select2, U
var $tags = $(".js-example-tags");
var $language = $(".js-example-language");
$basicSingle.select2();
$basicMultiple.select2()
@ -414,5 +450,9 @@ $.fn.select2.amd.require(["select2/core", "select2/utils"], function (Select2, U
$tags.select2({
tags: true
});
$language.select2({
language: "en"
});
});
</script>

View File

@ -1,4 +1,4 @@
{
"start": "(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }",
"start": "window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }",
"end": "require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());"
}

View File

@ -1,4 +1,5 @@
define([
'jquery',
'./results',
'./selection/single',
@ -6,6 +7,7 @@ define([
'./selection/placeholder',
'./utils',
'./translation',
'./data/select',
'./data/array',
@ -13,18 +15,20 @@ define([
'./data/tags',
'./dropdown',
'./dropdown/search'
], function (ResultsList,
'./dropdown/search',
'./i18n/en'
], function ($, ResultsList,
SingleSelection, MultipleSelection, Placeholder,
Utils,
Utils, Translation,
SelectData, ArrayData, AjaxData, Tags,
Dropdown, Search) {
Dropdown, Search, EnglishTranslation) {
function Defaults () {
this.reset();
}
Defaults.prototype.apply = function (options) {
options = $.extend({}, options, this.defaults);
options = $.extend({}, this.defaults, options);
if (options.dataAdapter == null) {
if (options.ajax) {
@ -66,11 +70,42 @@ define([
}
}
if (typeof options.language === 'string') {
options.language = [options.language];
}
if ($.isArray(options.language)) {
var languages = new Translation();
var languageNames = options.language.concat(this.defaults.language);
for (var l = 0; l < languageNames.length; l++) {
var name = languageNames[l];
var language = {};
try {
// Try to load it with the original name
language = Translation.loadPath(name);
} catch (e) {
// If we couldn't load it, check if it wasn't the full path
name = 'select2/i18n/' + name;
language = Translation.loadPath(name);
}
languages.extend(language);
}
options.translations = languages;
} else {
options.translations = new Translations(options.language);
}
return options;
};
Defaults.prototype.reset = function () {
this.defaults = { };
this.defaults = {
language: ['select2/i18n/en']
};
};
var defaults = new Defaults();

35
src/js/select2/translation.js vendored Normal file
View File

@ -0,0 +1,35 @@
define([
], function () {
function Translation (dict) {
this.dict = dict || {};
}
Translation.prototype.all = function () {
return this.dict;
};
Translation.prototype.get = function (key) {
return this.dict[key];
};
Translation.prototype.extend = function (translation) {
this.dict = $.extend({}, translation.all(), this.dict);
};
// Static functions
Translation._cache = {};
Translation.loadPath = function (path) {
if (!(path in Translation._cache)) {
var translations = require(path);
Translation._cache[path] = translations;
}
return new Translation(Translation._cache[path]);
};
return Translation;
});