-
-
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.
⭐ new(number): i18n-n functional component (#541) by @bponomarenko
- Loading branch information
1 parent
1bc3bde
commit b33579d
Showing
14 changed files
with
655 additions
and
33 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
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 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>number custom formatting</title> | ||
<script src="../../node_modules/vue/dist/vue.min.js"></script> | ||
<script src="../../dist/vue-i18n.min.js"></script> | ||
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.ja,Intl.~locale.en"></script> | ||
</head> | ||
<body> | ||
<div id="app"> | ||
<select v-model="locale"> | ||
<option value="en-US">en-US</option> | ||
<option value="ja-JP">ja-JP</option> | ||
</select> | ||
<p> | ||
{{ $t('money') }}: | ||
<i18n-n :value="money" format="currency"> | ||
<template slot="decimal" slot-scope="props"><!-- do not render decimal separator --></template> | ||
<span slot="fraction" slot-scope="props" style="vertical-align: super">{{ props.fraction }}</span> | ||
</i18n-n> | ||
</p> | ||
</div> | ||
<script> | ||
var messages = { | ||
'en-US': { | ||
money: 'Money' | ||
}, | ||
'ja-JP': { | ||
money: 'お金' | ||
} | ||
} | ||
var numberFormats = { | ||
'en-US': { | ||
currency: { | ||
style: 'currency', currency: 'USD' | ||
} | ||
}, | ||
'ja-JP': { | ||
currency: { | ||
style: 'currency', currency: 'JPY', currencyDisplay: 'symbol' | ||
} | ||
} | ||
} | ||
|
||
Vue.use(VueI18n) | ||
|
||
var initial = 'ja-JP' | ||
var i18n = new VueI18n({ | ||
locale: initial, | ||
messages: messages, | ||
numberFormats: numberFormats | ||
}) | ||
|
||
new Vue({ | ||
i18n: i18n, | ||
data: { | ||
locale: initial, | ||
money: 1000 | ||
}, | ||
watch: { | ||
locale: function (val) { | ||
this.$i18n.locale = val | ||
} | ||
}, | ||
el: '#app' | ||
}) | ||
</script> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* @flow */ | ||
|
||
import { warn } from './util' | ||
import { warn } from '../util' | ||
|
||
export default { | ||
name: 'i18n', | ||
|
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,63 @@ | ||
/* @flow */ | ||
|
||
import { warn, isObject, numberFormatKeys } from '../util' | ||
|
||
export default { | ||
name: 'i18n-n', | ||
functional: true, | ||
props: { | ||
tag: { | ||
type: String, | ||
default: 'span' | ||
}, | ||
value: { | ||
type: Number, | ||
required: true | ||
}, | ||
format: { | ||
type: [String, Object] | ||
}, | ||
locale: { | ||
type: String | ||
} | ||
}, | ||
render (h: Function, { props, parent, data }: Object) { | ||
const i18n = parent.$i18n | ||
|
||
if (!i18n) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
warn('Cannot find VueI18n instance!') | ||
} | ||
return null | ||
} | ||
|
||
let key: ?string = null | ||
let options: ?NumberFormatOptions = null | ||
|
||
if (typeof props.format === 'string') { | ||
key = props.format | ||
} else if (isObject(props.format)) { | ||
if (props.format.key) { | ||
key = props.format.key | ||
} | ||
|
||
// Filter out number format options only | ||
options = Object.keys(props.format).reduce((acc, prop) => { | ||
if (numberFormatKeys.includes(prop)) { | ||
return Object.assign({}, acc, { [prop]: props.format[prop] }) | ||
} | ||
return acc | ||
}, null) | ||
} | ||
|
||
const locale: Locale = props.locale || i18n.locale | ||
const parts: NumberFormatToPartsResult = i18n._ntp(props.value, locale, key, options) | ||
|
||
const values = parts.map((part, index) => { | ||
const slot: ?Function = data.scopedSlots && data.scopedSlots[part.type] | ||
return slot ? slot({ [part.type]: part.value, index, parts }) : part.value | ||
}) | ||
|
||
return h(props.tag, values) | ||
} | ||
} |
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,11 @@ | ||
module.exports = { | ||
component: function (browser) { | ||
browser | ||
.url('http://localhost:8080/examples/number-formatting/') | ||
.waitForElementVisible('#app', 1000) | ||
.assert.containsText('p', 'お金: ¥1,000') | ||
.click('select option[value=en-US]') | ||
.assert.attributeContains('p', 'innerHTML', 'Money: $1,000<span style="vertical-align: super">00</span>') | ||
.end() | ||
} | ||
} |
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
Oops, something went wrong.