This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter.i18n.js
74 lines (68 loc) · 1.88 KB
/
filter.i18n.js
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
65
66
67
68
69
70
71
72
73
74
/**
* Translation for strings in your entire angular application
* It depends on
*
* You need a javascript opject with your translations like this:
* lang = {
* 'Name': 'Name'
* ,'City': 'Ort'
* ,'Population': 'Einwohner'
* }
* You could include it as a file dependig on e.g. url (http://domain.tld/de/) or session
*
* Usage in template:
* {{'My Text'|i18n}}
*
* Usage in controller/directrive/widget:
* angular.filter.i18n('My Text');
*
*/
angular.filter('i18n', function(string) {
// FIXME allow injection of $xhr service
var log_untranslated = false;
var placeholders = [];
for(var i=1; i < arguments.length; i++) {
if(typeof(arguments[i]) == 'object') {
angular.forEach(arguments[i], function(item) {
placeholders.push(item);
})
}
else {
placeholders.push(arguments[i]);
}
}
var translated = lang[string]; // lang ist from the language file, e.g. de_DE.js
if (translated === undefined) {
if (log_untranslated == true) {
// here we could track unreanslated strings by sending them to the server...
}
return sprintf(string, placeholders);
}
return sprintf(translated, placeholders);
});
/**
* formats date _object_ to localized string by using date.js
* See http://www.datejs.com/
*
* Usage in template:
* Date only: {created_at|i18nDate}}
* Date and time: {created_at|i18nDate:true}}
*
* Usage in controller:
* angular.filter.i18nDate(self.created_at);
*/
angular.filter('i18nDate', function(date,time) {
var time = time || false;
if(typeof date === 'string') {
var date = Date.parse(date);
}
if(!date) {
return;
}
if(time) {
return date.toString(angular.filter.i18n('M/d/yyyy HH:mm'));
}
else {
return date.toString(angular.filter.i18n('M/d/yyyy'));
}
});