forked from siddii/angular-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi18nService.js
More file actions
64 lines (51 loc) · 2.62 KB
/
Copy pathi18nService.js
File metadata and controls
64 lines (51 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var app = angular.module('timer');
app.factory('I18nService', function() {
var I18nService = function() {};
I18nService.prototype.language = 'en';
I18nService.prototype.fallback = 'en';
I18nService.prototype.timeHumanizer = {};
I18nService.prototype.init = function init(lang, fallback) {
var supported_languages = humanizeDuration.getSupportedLanguages();
this.fallback = (fallback !== undefined) ? fallback : 'en';
if (supported_languages.indexOf(fallback) === -1) {
this.fallback = 'en';
}
this.language = lang;
if (supported_languages.indexOf(lang) === -1) {
this.language = this.fallback;
}
//moment init
moment.locale(this.language); //@TODO maybe to remove, it should be handle by the user's application itself, and not inside the directive
//human duration init, using it because momentjs does not allow accurate time (
// momentJS: a few moment ago, human duration : 4 seconds ago
this.timeHumanizer = humanizeDuration.humanizer({
language: this.language,
halfUnit:false
});
};
/**
* get time with units from momentJS i18n
* @param {int} millis
* @returns {{millis: string, seconds: string, minutes: string, hours: string, days: string, months: string, years: string}}
*/
I18nService.prototype.getTimeUnits = function getTimeUnits(millis) {
var diffFromAlarm = Math.round(millis/1000) * 1000; //time in milliseconds, get rid of the last 3 ms value to avoid 2.12 seconds display
var time = {};
if (typeof this.timeHumanizer != 'undefined'){
time = {
'millis' : this.timeHumanizer(diffFromAlarm, { units: ["milliseconds"] }),
'seconds' : this.timeHumanizer(diffFromAlarm, { units: ["seconds"] }),
'minutes' : this.timeHumanizer(diffFromAlarm, { units: ["minutes", "seconds"] }) ,
'hours' : this.timeHumanizer(diffFromAlarm, { units: ["hours", "minutes", "seconds"] }) ,
'days' : this.timeHumanizer(diffFromAlarm, { units: ["days", "hours", "minutes", "seconds"] }) ,
'months' : this.timeHumanizer(diffFromAlarm, { units: ["months", "days", "hours", "minutes", "seconds"] }) ,
'years' : this.timeHumanizer(diffFromAlarm, { units: ["years", "months", "days", "hours", "minutes", "seconds"] })
};
}
else {
console.error('i18nService has not been initialized. You must call i18nService.init("en") for example');
}
return time;
};
return I18nService;
});