Skip to content

Commit f3b1621

Browse files
author
hustcc
committed
opt locales testcase
1 parent c2da159 commit f3b1621

File tree

6 files changed

+63
-34
lines changed

6 files changed

+63
-34
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ in 2 years
3333
```
3434

3535

36-
# Usage
36+
# 1. Usage
3737

3838
**1. Install timeago.js**
3939

@@ -68,7 +68,7 @@ timeago.format('2016-06-12')
6868
```
6969

7070

71-
# Detailed Usage
71+
# 2. Detailed Usage
7272

7373
**1. Set relative date**
7474

@@ -158,11 +158,16 @@ timeago.format('2016-06-12', 'test_local');
158158
You can see [locales](locales) dir for more locales. Please submit a GitHub pull request for corrections or additional languages, and add the locale key into `tests/locales_test.js`.
159159

160160

161-
# Contribution
161+
# 3. Contributions
162162

163-
The website is base on [rmm5t/jquery-timeago](https://github.com/rmm5t/jquery-timeago) which is a nice and featured project but depends on jQuery.
163+
1. The website is base on [rmm5t/jquery-timeago](https://github.com/rmm5t/jquery-timeago) which is a nice and featured project but depends on jQuery.
164164

165+
2. **locale translations**: The library need the locale translations. You can:
165166

166-
# LICENSE
167+
- Open an issue to write the locale translations, how to ? see [here](https://github.com/hustcc/timeago.js/blob/master/locales/en.js).
168+
- Or pull a request, please **test** it before by exec `npm test` or `node tests/locales_test.js`.
169+
170+
171+
# 4. LICENSE
167172

168173
MIT
File renamed without changes.

locales/locales.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** locales table.
2+
* You can add the missing locale code
3+
* 1. please sort by dictionary.
4+
* 2. please ensure the accuracy.
5+
*/
6+
module.exports = ['ar', 'az', 'az', 'bg', 'bs', 'ca', 'cs', 'cy', 'da', 'de', 'dv', 'el', 'en', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'gl', 'he', 'hr', 'hu', 'hy', 'id', 'in_ID', 'is', 'it', 'ja', 'jv', 'ko', 'ky', 'lt', 'lv', 'mk', 'nl', 'no', 'pl', 'pt', 'pt_BR', 'pt', 'pt', 'ro', 'rs', 'ru', 'rw', 'si', 'sk', 'sl', 'sr', 'sv', 'th', 'tr', 'uk', 'uz', 'vi', 'zh_CN', 'zh_TW'];
File renamed without changes.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"gulp-uglify": "^1.5.3",
3030
"jshint": "^2.9.2",
3131
"gulp-rename": "^1.2.2",
32-
"tape": "^3.6.1"
32+
"tape": "^3.6.1",
33+
"pys": "^1.0.2"
3334
},
3435
"scripts": {
3536
"lint": "jshint src/timeago.js",

tests/locales_test.js

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,52 @@
33
var fs = require('fs');
44
var test = require('tape');
55
var timeago = require('..');
6+
var psy = require('pys');
7+
8+
// all the locales code, if missing, please add them.
9+
var all_locales = require('../locales/locales.js');
10+
11+
function test_locales(tobeTested) {
12+
test('Testing locales', function (t) {
13+
for (var i = 0; i < tobeTested.length ; i++) {
14+
var locale_name = psy(tobeTested[i])('0:-3');
15+
t.ok(all_locales.indexOf(locale_name) >= 0, 'locale [' + locale_name + ']');
16+
17+
console.log('\nTesting locales [' + locale_name + ']');
18+
19+
var locale = require('../locales/' + locale_name);
20+
// test locales
21+
var newTimeAgo = timeago('2016-06-23');
22+
newTimeAgo.register(locale_name, locale);
23+
t.equal(newTimeAgo.format('2016-06-22', locale_name), locale(1, 6)[0]);
24+
t.equal(newTimeAgo.format('2016-06-25', locale_name), locale(2, 7)[1].replace('%s', '2'));
25+
26+
// test default locale
27+
var newTimeAgo = timeago('2016-03-01 12:00:00', locale_name);
28+
newTimeAgo.register(locale_name, locale);
29+
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), locale(2, 7)[0].replace('%s', '2'));
30+
31+
// test setLocale
32+
var newTimeAgo = timeago('2016-03-01 12:00:00');
33+
newTimeAgo.register(locale_name, locale);
34+
newTimeAgo.setLocale(locale_name);
35+
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), locale(2, 7)[0].replace('%s', '2'));
36+
}
37+
t.end();
38+
});
39+
}
40+
41+
// read all the locales in `locales` dir
42+
fs.readdir('locales', function(err, files) {
43+
// rm locales.js file
44+
var index = files.indexOf('locales.js');
45+
if (index > -1) {
46+
files.splice(index, 1);
47+
}
48+
// test them
49+
test_locales(files);
50+
});
651

7-
var tobeTested = ['en', 'zh_CN', 'fr', 'nl_BE', 'pt_BR', 'da', 'in_ID', 'it', 'es', 'pl', 'zh_TW', 'sv', 'el_GR']; // when add a new locale, add locale's name here
8-
9-
test('Testing locales', function (t) {
10-
for (var i = 0; i < tobeTested.length ; i++) {
11-
var locale_name = tobeTested[i];
12-
13-
console.log('\nTesting locales [' + locale_name + ']');
14-
15-
var locale = require('../locales/' + locale_name);
16-
// test locales
17-
var newTimeAgo = timeago('2016-06-23');
18-
newTimeAgo.register(locale_name, locale);
19-
t.equal(newTimeAgo.format('2016-06-22', locale_name), locale(1, 6)[0]);
20-
t.equal(newTimeAgo.format('2016-06-25', locale_name), locale(2, 7)[1].replace('%s', '2'));
21-
22-
// test default locale
23-
var newTimeAgo = timeago('2016-03-01 12:00:00', locale_name);
24-
newTimeAgo.register(locale_name, locale);
25-
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), locale(2, 7)[0].replace('%s', '2'));
26-
27-
// test setLocale
28-
var newTimeAgo = timeago('2016-03-01 12:00:00');
29-
newTimeAgo.register(locale_name, locale);
30-
newTimeAgo.setLocale(locale_name);
31-
t.equal(newTimeAgo.format('2016-02-28 12:00:00'), locale(2, 7)[0].replace('%s', '2'));
32-
};
3352

34-
t.end();
35-
});
3653

3754

0 commit comments

Comments
 (0)