-
-
Notifications
You must be signed in to change notification settings - Fork 861
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { warn, isPromise } from './util' | ||
|
||
let locales = Object.create(null) // locales store | ||
|
||
|
||
export default function (Vue) { | ||
/** | ||
* Register or retrieve a global locale definition. | ||
* | ||
* @param {String} id | ||
* @param {Object | Function | Promise} definition | ||
*/ | ||
|
||
Vue.locale = (id, definition) => { | ||
if (definition === undefined) { // gettter | ||
return locales[id] | ||
} else { // setter | ||
if (definition === null) { | ||
locales[id] = undefined | ||
delete locales[id] | ||
} else { | ||
setLocale(id, definition, (locale) => { | ||
if (locale) { | ||
locales[id] = locale | ||
} else { | ||
warn('failed set `' + id + '` locale') | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
function setLocale (id, definition, cb) { | ||
if (typeof definition === 'object') { // sync | ||
cb(definition) | ||
} else { | ||
let future = definition.call(this) | ||
if (typeof future === 'function') { | ||
if (future.resolved) { | ||
// cached | ||
cb(future.resolved) | ||
} else if (future.requested) { | ||
// pool callbacks | ||
future.pendingCallbacks.push(cb) | ||
} else { | ||
future.requested = true | ||
let cbs = future.pendingCallbacks = [cb] | ||
future((locale) => { // resolve | ||
future.resolved = locale | ||
for (let i = 0, l = cbs.length; i < l; i++) { | ||
cbs[i](locale) | ||
} | ||
}, () => { // reject | ||
cb() | ||
}) | ||
} | ||
} else if (isPromise(future)) { // promise | ||
future.then((locale) => { // resolve | ||
cb(locale) | ||
}, () => { // reject | ||
cb() | ||
}).catch((err) => { | ||
console.error(err) | ||
cb() | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import assert from 'power-assert' | ||
import Vue from 'vue' | ||
|
||
|
||
describe('asset', () => { | ||
const DELAY = 10 | ||
|
||
describe('sync', () => { | ||
it('should be translated', () => { | ||
Vue.locale('en', { | ||
message: { | ||
foo: 'foo' | ||
} | ||
}) | ||
assert(Vue.t('message.foo') === 'foo') | ||
}) | ||
}) | ||
|
||
|
||
describe('async', () => { | ||
describe('promise like function', () => { | ||
beforeEach((done) => { | ||
Vue.locale('en', () => { | ||
return (resolve, reject) => { | ||
setTimeout(() => { | ||
resolve({ message: { bar: 'bar' } }) | ||
}, DELAY) | ||
} | ||
}) | ||
Vue.nextTick(done) | ||
}) | ||
|
||
it('should be translated', (done) => { | ||
setTimeout(() => { | ||
assert(Vue.t('message.bar') === 'bar') | ||
done() | ||
}, DELAY + 5) | ||
}) | ||
}) | ||
|
||
describe('promise ', () => { | ||
beforeEach((done) => { | ||
Vue.locale('en', () => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve({ message: { buz: 'buz' } }) | ||
}, DELAY) | ||
}) | ||
}) | ||
Vue.nextTick(done) | ||
}) | ||
|
||
it('should be translated', (done) => { | ||
setTimeout(() => { | ||
assert(Vue.t('message.buz') === 'buz') | ||
done() | ||
}, DELAY + 5) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters