Skip to content

Commit

Permalink
Added locale support
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzhixin committed Aug 21, 2019
1 parent ea6a9e2 commit 6edd483
Show file tree
Hide file tree
Showing 15 changed files with 386 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
}
},
{
'files': ['tools/template.js'],
'files': ['src/locale/*.js', 'tools/*.js'],
// These are being overwritten for some reason, despite `node: true`
'globals': {
require: 'readonly',
Expand Down
20 changes: 20 additions & 0 deletions docs/_i18n/en/documentation/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ Option format:

**Example:** <a href="../examples#basic-data.html">Basic Data</a> and <a href="./examples#optgroup-data.html">Optgroup Data</a>.

## locale

- **Type:** `String`

- **Detail:**

Sets the locale to use (i.e. `'zh-CN'`). Locale files must be pre-loaded.
Allows for fallback locales, if loaded, in the following order:

* First tries for the locale as specified,
* Then tries the locale with '_' translated to '-' and the region code upper cased,
* Then tries the short locale code (i.e. `'zh'` instead of `'zh-CN'`),
* And finally will use the last locale file loaded (or the default locale if no locales loaded).

If left `undefined` or an empty string, use the last locale loaded (or `'en-US'` if no locale files loaded).

- **Default:** `undefined`

**Example:** <a href="../examples#locale.html">The Locale</a>

## selectAll

**Type:** Boolean
Expand Down
22 changes: 22 additions & 0 deletions docs/_i18n/zh-cn/documentation/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ Option 格式:

**例子:** <a href="../examples#basic-data.html">Basic Data</a> 和 <a href="./examples#optgroup-data.html">Optgroup Data</a>

## locale

- **标签属性:** `data-locale`

- **类型:** `String`

- **详细描述:**

设置要使用的多语言(例如 `'zh-CN'`)。设置的多语言文件必须预先加载。
允许同时加载多个多语言文件,假如已加载,按照以下的顺序:

* 首先尝试指定的多语言,
* 然后尝试将 '_' 翻译为 '-' 并将区域代码设置为大写的多语言,
* 然后尝试短的多语言代码(即 `'zh'` 而不是 `'zh-CN'`),
* 最后将使用加载的最后一个多语言文件(如果没有加载多语言,则使用默认的多语言)。

如果保留 `undefined` 或空字符串,请使用最后加载的多语言(如果没有加载多语言文件,则使用 'en-US')。

- **默认:** `undefined`

**例子:** <a href="../examples#locale.html">The Locale</a>

## selectAll

**类型:** Boolean
Expand Down
1 change: 1 addition & 0 deletions docs/_includes/example-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<li><a href="../examples#two-select.html">Two Select</a></li>
<li><a href="../examples#basic-data.html">Basic Data</a></li>
<li><a href="../examples#optgroup-data.html">Optgroup Data</a></li>
<li><a href="../examples#locale.html">The Locale</a></li>
<li role="separator" class="divider"></li>
<li class="dropdown-header">Options</li>
<li><a href="../examples#placeholder.html">The Placeholder</a></li>
Expand Down
61 changes: 61 additions & 0 deletions docs/examples/locale.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<script>
init({
title: 'The Locale',
desc: 'Use <code>locale: true</code> to set the locale.',
links: ['multiple-select.css'],
scripts: [
'multiple-select.js',
'https://unpkg.com/[email protected]/dist/multiple-select-locale-all.min.js'
]
})
</script>

<style>
select {
width: 500px;
}
.mb20 {
margin-bottom: 20px;
}
</style>

<div class="mb20">
<select id="locale">
<option value="en-US">English</option>
<option value="zh-CN">简体中文</option>
<option value="zh-TW">繁體中文</option>
</select>
</div>

<div>
<select id="select" multiple="multiple">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</div>

<script>
function updateLocale(locale) {
$('#select').multipleSelect('destroy').multipleSelect({
locale: locale
})
}

function mounted() {
$('#locale').multipleSelect({
single: true
}).change(function () {
updateLocale($(this).val())
}).change()
}
</script>
39 changes: 20 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,39 @@
"docs": "http://multiple-select.wenzhixin.net.cn/documentation",
"download": "https://github.com/wenzhixin/multiple-select/archive/master.zip",
"engines": {},
"dependencies": {},
"devDependencies": {
"@babel/preset-env": "^7.4.5",
"@mysticatea/eslint-plugin": "^10.0.3",
"@babel/preset-env": "^7.5.5",
"@mysticatea/eslint-plugin": "^11.0.0",
"axe-testcafe": "^3.0.0",
"core-js": "^3.1.4",
"core-js": "^3.2.1",
"cssmin-cli": "^0.0.5",
"eslint": "^6.0.1",
"eslint-config-ash-nazg": "^7.0.1",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-compat": "^3.2.0",
"eslint": "^6.2.1",
"eslint-config-ash-nazg": "^8.7.0",
"eslint-config-standard": "^14.0.0",
"eslint-plugin-compat": "^3.3.0",
"eslint-plugin-eslint-comments": "^3.1.2",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-jsdoc": "^10.0.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsdoc": "^15.8.3",
"eslint-plugin-markdown": "^1.0.0",
"eslint-plugin-no-use-extend-native": "^0.4.1",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"eslint-plugin-standard": "^4.0.1",
"eslint-plugin-testcafe": "^0.2.1",
"eslint-plugin-unicorn": "^9.1.1",
"eslint-plugin-unicorn": "^10.0.0",
"getopts": "^2.2.5",
"glob": "^7.1.4",
"headr": "^0.0.4",
"npm-run-all": "^4.1.5",
"rollup": "^1.16.3",
"rollup": "^1.19.4",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-babel-minify": "^8.0.0",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-inject": "^3.0.0",
"rollup-plugin-babel-minify": "^9.0.0",
"rollup-plugin-commonjs": "^10.0.2",
"rollup-plugin-inject": "^3.0.1",
"rollup-plugin-multi-entry": "^2.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"sass": "^1.22.1",
"testcafe": "^1.2.1",
"typescript": "^3.5.2"
"sass": "^1.22.10",
"testcafe": "^1.4.1",
"typescript": "^3.5.3"
}
}
42 changes: 41 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import glob from 'glob'
import babel from 'rollup-plugin-babel'
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import minify from 'rollup-plugin-babel-minify'
import inject from 'rollup-plugin-inject'
// import vue from 'rollup-plugin-vue'
import multiEntry from 'rollup-plugin-multi-entry'

let found
const env = process.argv.find(flag => {
Expand Down Expand Up @@ -74,4 +75,43 @@ if (!dev) {
})
}

out = 'dist/multiple-select-locale-all.js'
if (production) {
out = out.replace(/.js$/, '.min.js')
}
config.push({
input: 'src/locale/**/*.js',
output: {
name: 'MultipleSelect',
file: out,
format: 'umd',
globals
},
external,
plugins: [
multiEntry(),
...plugins
]
})

const files = glob.sync('src/locale/**/*.js')

for (const file of files) {
out = `dist/${file.replace('src/', '')}`
if (production) {
out = out.replace(/.js$/, '.min.js')
}
config.push({
input: file,
output: {
name: 'MultipleSelect',
file: out,
format: 'umd',
globals
},
external,
plugins
})
}

export default config
Loading

0 comments on commit 6edd483

Please sign in to comment.