Skip to content
This repository was archived by the owner on Jul 6, 2021. It is now read-only.

Commit a6bac94

Browse files
committed
feat: allow to change locale globally
1 parent aeb3d55 commit a6bac94

File tree

7 files changed

+122
-79
lines changed

7 files changed

+122
-79
lines changed

README.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ CDN: [UNPKG](https://unpkg.com/vue-timeago/dist/) | [jsDelivr](https://cdn.jsdel
1717
For usages on version 4, please check out [this branch](https://github.com/egoist/vue-timeago/tree/4).
1818

1919
```js
20-
import VueTimeago from 'vue-timeago'
20+
import VueTimeago from "vue-timeago";
2121

2222
Vue.use(VueTimeago, {
23-
name: 'Timeago', // Component name, `Timeago` by default
24-
locale: 'en', // Default locale
23+
name: "Timeago", // Component name, `Timeago` by default
24+
locale: "en", // Default locale
2525
// We use `date-fns` under the hood
2626
// So you can use all locales from it
2727
locales: {
28-
'zh-CN': require('date-fns/locale/zh_cn'),
29-
'ja': require('date-fns/locale/ja'),
28+
"zh-CN": require("date-fns/locale/zh_cn"),
29+
ja: require("date-fns/locale/ja")
3030
}
31-
})
31+
});
3232
```
3333

3434
Then in your lovely component:
@@ -49,53 +49,53 @@ Then in your lovely component:
4949
## Plugin options
5050

5151
```js
52-
Vue.use(VueTimeago, pluginOptions)
52+
Vue.use(VueTimeago, pluginOptions);
5353
```
5454

5555
### locales
5656

57-
- __Type__: `{ [localeName: string]: any }`
57+
- **Type**: `{ [localeName: string]: any }`
5858

5959
An object of locales.
6060

6161
### locale
6262

63-
- __Type__: `string`
63+
- **Type**: `string`
6464

6565
The default locale name.
6666

6767
### converter
6868

69-
- __Type__: `(date, locale, converterOptions) => string`
69+
- **Type**: `(date, locale, converterOptions) => string`
7070

7171
A `converter` that formats regular dates in `xxx ago` or `in xxx` style.
7272

7373
Check out our [default converter](./src/converter.js) which uses [date-fns/distance_in_words_to_now](https://date-fns.org/v1.29.0/docs/distanceInWordsToNow) under the hood.
7474

7575
### converterOptions
7676

77-
- __Type__: `Object`
77+
- **Type**: `Object`
7878

7979
Provide an object which will be available as argument `converterOptions` in the `converter` we mentioned above.
8080

8181
Our default converter supports most options that [date-fns/distance_in_words_to_now](https://date-fns.org/v1.29.0/docs/distanceInWordsToNow) library supports, namely:
8282

83-
- __includeSeconds__: (default: `false`) distances less than a minute are more detailed
84-
- __addSuffix__: (default: `true`) result specifies if the second date is earlier or later than the first
83+
- **includeSeconds**: (default: `false`) distances less than a minute are more detailed
84+
- **addSuffix**: (default: `true`) result specifies if the second date is earlier or later than the first
8585

8686
## props
8787

8888
### datetime
8989

90-
- __Type__: `Date` `string` `number`
91-
- __Required__: `true`
90+
- **Type**: `Date` `string` `number`
91+
- **Required**: `true`
9292

9393
The datetime to be formatted .
9494

9595
### autoUpdate
9696

97-
- __Type__: `number` `boolean`
98-
- __Default__: `false`
97+
- **Type**: `number` `boolean`
98+
- **Default**: `false`
9999

100100
The period to update the component, in **seconds**.
101101

@@ -115,6 +115,21 @@ Just like the `converter` option in the plugin options, but this could override
115115

116116
Just like the `converterOptions` option in the plugin options, but this could override the global one.
117117

118+
## Recipes
119+
120+
### Update Locale Globally
121+
122+
```js
123+
Vue.use(VueTimeago, {
124+
locale: "en",
125+
locales: {
126+
"zh-CN": require("date-fns/locale/zh_cn")
127+
}
128+
});
129+
```
130+
131+
In your components you can use `this.$timeago.locale` to access the global locale, in this case it's `en`, the `<timeago>` component will get updated when you set it to another valid locale, e.g. `this.$timeago.locale = 'zh-CN'`.
132+
118133
## What about the good old [vue-timeago v3](https://github.com/egoist/vue-timeago/tree/3)?
119134

120135
The older version (700 bytes gzipped) is much smaller than the current version (2.8kB gzipped) that uses [`date-fns`](https://date-fns.org/).
@@ -134,5 +149,3 @@ yarn build
134149
## License
135150

136151
MIT © [EGOIST](https://github.com/egoist)
137-
138-

bili.config.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
1-
import { Config } from 'bili'
1+
import { Config } from "bili";
22

33
const config: Config = {
4-
input: 'src/index.js',
4+
input: "src/index.js",
55
output: {
6-
moduleName: 'VueTimeago',
7-
format: ['esm', 'umd', 'cjs'],
6+
moduleName: "VueTimeago",
7+
format: ["esm", "umd", "cjs"],
88
fileName({ format }, defaultFileName) {
9-
if (format === 'cjs') {
10-
return 'vue-timeago.cjs.js'
9+
if (format === "cjs") {
10+
return "vue-timeago.cjs.js";
1111
}
12-
if (format === 'umd') {
13-
return 'vue-timeago.js'
12+
if (format === "umd") {
13+
return "vue-timeago.js";
1414
}
15-
if (format === 'esm') {
16-
return 'vue-timeago.es.js'
15+
if (format === "esm") {
16+
return "vue-timeago.es.js";
1717
}
18-
return defaultFileName
18+
return defaultFileName;
1919
}
2020
},
21+
babel: {
22+
minimal: true
23+
},
2124
extendConfig(config, { format }) {
22-
if (format === 'umd') {
23-
config.output.minify = true
25+
if (format === "umd") {
26+
config.output.minify = true;
27+
config.env = Object.assign({}, config.env, {
28+
NODE_ENV: "production"
29+
});
2430
}
25-
return config
31+
return config;
2632
}
27-
}
33+
};
2834

29-
export default config
35+
export default config;

example/app.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<template>
22
<div id="app">
33
<div class="header">
4-
<select v-model="locale">
5-
<option value="en">en</option>
4+
<select v-model="$timeago.locale">
65
<option
76
v-for="name of localeNames"
87
:key="name"
@@ -13,10 +12,10 @@
1312
</div>
1413
<div class="main">
1514
<input type="text" v-model="datetime1">
16-
<timeago :datetime="datetime1" :locale="locale" :converter="converter" />
15+
<timeago :datetime="datetime1" :converter="converter" />
1716
<hr>
1817
<input type="text" v-model="datetime2">
19-
<timeago :datetime="datetime2" :locale="locale" :autoUpdate="autoUpdate ? 1 : 0" :converterOptions="{ includeSeconds: true }" /> <input type="checkbox" v-model="autoUpdate"> Auto Update in every second
18+
<timeago :datetime="datetime2" :autoUpdate="autoUpdate ? 1 : 0" :converterOptions="{ includeSeconds: true }" /> <input type="checkbox" v-model="autoUpdate"> Auto Update in every second
2019
</div>
2120
</div>
2221
</template>
@@ -29,7 +28,6 @@ export default {
2928
3029
data() {
3130
return {
32-
locale: 'en',
3331
autoUpdate: false,
3432
datetime1: '2022-02-12',
3533
datetime2: format(new Date(), 'YYYY-MM-DD HH:mm:ss'),

example/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ r.keys().forEach(v => {
1111
})
1212

1313
Vue.use(Timeago, {
14+
locale: 'en',
1415
locales,
1516
converter
1617
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"filter"
3131
],
3232
"devDependencies": {
33-
"bili": "^4.5.2",
33+
"bili": "^4.5.3",
3434
"commitizen": "^3.0.7",
3535
"cz-conventional-changelog": "^2.1.0",
3636
"eslint-config-prettier": "^4.1.0",

src/index.js

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import defaultConverter from './converter'
1+
import defaultConverter from "./converter";
22

33
export const createTimeago = (opts = {}) => {
4-
const locales = opts.locales || {}
5-
const name = opts.name || 'Timeago'
4+
const locales = opts.locales || {};
5+
const name = opts.name || "Timeago";
66

77
return {
88
name,
@@ -31,94 +31,119 @@ export const createTimeago = (opts = {}) => {
3131
data() {
3232
return {
3333
timeago: this.getTimeago()
34+
};
35+
},
36+
37+
computed: {
38+
localeName() {
39+
return this.locale || this.$timeago.locale;
3440
}
3541
},
3642

3743
mounted() {
38-
this.startUpdater()
44+
this.startUpdater();
3945
},
4046

4147
beforeDestroy() {
42-
this.stopUpdater()
48+
this.stopUpdater();
4349
},
4450

4551
render(h) {
4652
return h(
47-
'time',
53+
"time",
4854
{
4955
attrs: {
5056
datetime: new Date(this.datetime),
5157
title:
52-
typeof this.title === 'string' ?
53-
this.title :
54-
this.title === false ?
55-
null :
56-
this.timeago
58+
typeof this.title === "string"
59+
? this.title
60+
: this.title === false
61+
? null
62+
: this.timeago
5763
}
5864
},
5965
[this.timeago]
60-
)
66+
);
6167
},
6268

6369
methods: {
6470
getTimeago(datetime) {
65-
const converter = this.converter || opts.converter || defaultConverter
66-
return converter(datetime || this.datetime, locales[this.locale || opts.locale], this.converterOptions || {})
71+
const converter = this.converter || opts.converter || defaultConverter;
72+
return converter(
73+
datetime || this.datetime,
74+
locales[this.localeName],
75+
this.converterOptions || {}
76+
);
6777
},
6878

6979
convert(datetime) {
70-
this.timeago = this.getTimeago(datetime)
80+
this.timeago = this.getTimeago(datetime);
7181
},
7282

7383
startUpdater() {
7484
if (this.autoUpdate) {
75-
const autoUpdaye = this.autoUpdate === true ? 60 : this.autoUpdate
85+
const autoUpdaye = this.autoUpdate === true ? 60 : this.autoUpdate;
7686
this.updater = setInterval(() => {
77-
this.convert()
78-
}, autoUpdaye * 1000)
87+
this.convert();
88+
}, autoUpdaye * 1000);
7989
}
8090
},
8191

8292
stopUpdater() {
8393
if (this.updater) {
84-
clearInterval(this.updater)
85-
this.updater = null
94+
clearInterval(this.updater);
95+
this.updater = null;
8696
}
8797
}
8898
},
8999

90100
watch: {
91101
autoUpdate(newValue) {
92-
this.stopUpdater()
102+
this.stopUpdater();
93103
if (newValue) {
94-
this.startUpdater()
104+
this.startUpdater();
95105
}
96106
},
97107

98108
datetime() {
99-
this.convert()
109+
this.convert();
100110
},
101-
locale() {
102-
this.convert()
111+
localeName() {
112+
this.convert();
103113
},
104114
converter() {
105-
this.convert()
115+
this.convert();
106116
},
107117
converterOptions: {
108118
handler() {
109-
this.convert()
119+
this.convert();
110120
},
111121
deep: true
112122
}
113123
}
114-
}
115-
}
124+
};
125+
};
116126

117127
export const install = (Vue, opts) => {
118-
const Component = createTimeago(opts)
119-
Vue.component(Component.name, Component)
120-
}
128+
if (Vue.prototype.$timeago) {
129+
return;
130+
}
131+
132+
if (process.env.NODE_ENV === "development" && !Vue.observable) {
133+
console.warn(`[vue-timeago] Vue 2.6 or above is recommended.`);
134+
}
135+
136+
const $timeago = {
137+
locale: opts.locale
138+
};
139+
Vue.prototype.$timeago = Vue.observable
140+
? Vue.observable($timeago)
141+
: new Vue({ data: $timeago });
142+
143+
const Component = createTimeago(opts);
144+
Vue.component(Component.name, Component);
145+
};
121146

122-
export const converter = defaultConverter
147+
export const converter = defaultConverter;
123148

124-
export default install
149+
export default install;

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,10 +1578,10 @@ big.js@^3.1.3:
15781578
version "3.2.0"
15791579
resolved "https://registry.npmjs.org/big.js/-/big.js-3.2.0.tgz#a5fc298b81b9e0dca2e458824784b65c52ba588e"
15801580

1581-
bili@^4.5.2:
1582-
version "4.5.2"
1583-
resolved "https://registry.npmjs.org/bili/-/bili-4.5.2.tgz#55a141cba2a2dd813223fffe18efdb4434fadc75"
1584-
integrity sha512-G91DajUqNKU3qmcHXCwUaqHc4jaNkjO8IfYcTy3d3XVdXtU1oYNM3duDWvd6lbEzz19HfgjMxFArzaYoXOGz7Q==
1581+
bili@^4.5.3:
1582+
version "4.5.3"
1583+
resolved "https://registry.npmjs.org/bili/-/bili-4.5.3.tgz#76e3c4b04c87543c6a1ff44532749f7e96c06c36"
1584+
integrity sha512-9NosJzbXfPF3w7F0tEieudEKS77iBF1S+hnMBWofUf4jGzk1op7NjFZyAzz0ZXQVReGw7y+hERzlVgGfs1rWLA==
15851585
dependencies:
15861586
"@babel/core" "^7.2.2"
15871587
"@babel/plugin-proposal-object-rest-spread" "^7.3.1"

0 commit comments

Comments
 (0)