Skip to content

Commit 9d2dcc3

Browse files
committed
Support multiple locales concurrent
This commit extends the plugin to support loading and using mutliple locales at the same time. All strings are stored with their language code. The plugins gets a new `lang` setting denoting the default locale. For each HTML tag, the plugin will lookup `lang` attribute from the tag, the configured default lang, or fallback to english. It will use the `strings` object directly, if no lang key is found at all, therefore it supports loading and running with older or custom locale files that are directly assigning strings.
1 parent 030a88f commit 9d2dcc3

File tree

74 files changed

+204
-112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+204
-112
lines changed

jquery.timeago.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,37 @@
4646
localeTitle: false,
4747
cutoff: 0,
4848
autoDispose: true,
49+
lang: "en",
4950
strings: {
50-
prefixAgo: null,
51-
prefixFromNow: null,
52-
suffixAgo: "ago",
53-
suffixFromNow: "from now",
54-
inPast: "any moment now",
55-
seconds: "less than a minute",
56-
minute: "about a minute",
57-
minutes: "%d minutes",
58-
hour: "about an hour",
59-
hours: "about %d hours",
60-
day: "a day",
61-
days: "%d days",
62-
month: "about a month",
63-
months: "%d months",
64-
year: "about a year",
65-
years: "%d years",
66-
wordSeparator: " ",
67-
numbers: []
51+
en: {
52+
prefixAgo: null,
53+
prefixFromNow: null,
54+
suffixAgo: "ago",
55+
suffixFromNow: "from now",
56+
inPast: "any moment now",
57+
seconds: "less than a minute",
58+
minute: "about a minute",
59+
minutes: "%d minutes",
60+
hour: "about an hour",
61+
hours: "about %d hours",
62+
day: "a day",
63+
days: "%d days",
64+
month: "about a month",
65+
months: "%d months",
66+
year: "about a year",
67+
years: "%d years",
68+
wordSeparator: " ",
69+
numbers: []
70+
}
6871
}
6972
},
7073

71-
inWords: function(distanceMillis) {
74+
inWords: function(distanceMillis, lang) {
7275
if (!this.settings.allowPast && ! this.settings.allowFuture) {
7376
throw 'timeago allowPast and allowFuture settings can not both be set to false.';
7477
}
7578

76-
var $l = this.settings.strings;
79+
var $l = this.settings.strings[lang] || this.settings.strings[this.settings.lang] || this.settings.strings["en"] || this.settings.strings;
7780
var prefix = $l.prefixAgo;
7881
var suffix = $l.suffixAgo;
7982
if (this.settings.allowFuture) {
@@ -84,7 +87,7 @@
8487
}
8588

8689
if (!this.settings.allowPast && distanceMillis >= 0) {
87-
return this.settings.strings.inPast;
90+
return $l.inPast;
8891
}
8992

9093
var seconds = Math.abs(distanceMillis) / 1000;
@@ -194,7 +197,7 @@
194197

195198
if (!isNaN(data.datetime)) {
196199
if ( $s.cutoff === 0 || Math.abs(distance(data.datetime)) < $s.cutoff) {
197-
$(this).text(inWords(data.datetime));
200+
$(this).text(inWords(data.datetime, ($(this).attr('lang') ? $(this).attr('lang') : $t.settings.lang)));
198201
} else {
199202
if ($(this).attr('title').length > 0) {
200203
$(this).text($(this).attr('title'));
@@ -218,8 +221,8 @@
218221
return element.data("timeago");
219222
}
220223

221-
function inWords(date) {
222-
return $t.inWords(distance(date));
224+
function inWords(date, lang) {
225+
return $t.inWords(distance(date), lang);
223226
}
224227

225228
function distance(date) {

locales/jquery.timeago.af.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
}(function (jQuery) {
1010
// Afrikaans
11-
jQuery.timeago.settings.strings = {
11+
jQuery.timeago.settings.strings["af"] = {
1212
prefixAgo: null,
1313
prefixFromNow: null,
1414
suffixAgo: "gelede",

locales/jquery.timeago.am.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
}(function (jQuery) {
1010
// Amharic
11-
jQuery.timeago.settings.strings = {
11+
jQuery.timeago.settings.strings["am"] = {
1212
prefixAgo: null,
1313
prefixFromNow: null,
1414
suffixAgo: "በፊት",

locales/jquery.timeago.ar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
return a[plural=n===0 ? 0 : n===1 ? 1 : n===2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5];
1212
}
1313

14-
jQuery.timeago.settings.strings = {
14+
jQuery.timeago.settings.strings["ar"] = {
1515
prefixAgo: "منذ",
1616
prefixFromNow: "بعد",
1717
suffixAgo: null,

locales/jquery.timeago.az-short.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
}(function (jQuery) {
1010
// Azerbaijani shortened
11-
jQuery.timeago.settings.strings = {
11+
jQuery.timeago.settings.strings["az-short"] = {
1212
prefixAgo: null,
1313
prefixFromNow: null,
1414
suffixAgo: "",

locales/jquery.timeago.az.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
}(function (jQuery) {
1010
// Azerbaijani
11-
jQuery.timeago.settings.strings = {
11+
jQuery.timeago.settings.strings["az"] = {
1212
prefixAgo: null,
1313
prefixFromNow: null,
1414
suffixAgo: 'əvvəl',

locales/jquery.timeago.be.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424
}
2525

26-
jQuery.timeago.settings.strings = {
26+
jQuery.timeago.settings.strings["be"] = {
2727
prefixAgo: null,
2828
prefixFromNow: "праз",
2929
suffixAgo: "таму",

locales/jquery.timeago.bg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
}(function (jQuery) {
1010
// Bulgarian
11-
jQuery.timeago.settings.strings = {
11+
jQuery.timeago.settings.strings["bg"] = {
1212
prefixAgo: "преди",
1313
prefixFromNow: "след",
1414
suffixAgo: null,

locales/jquery.timeago.bs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
}
2121
};
2222

23-
jQuery.timeago.settings.strings = {
23+
jQuery.timeago.settings.strings["bs"] = {
2424
prefixAgo: "prije",
2525
prefixFromNow: "za",
2626
suffixAgo: null,

locales/jquery.timeago.ca.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
}(function (jQuery) {
1010
// Catalan
11-
jQuery.timeago.settings.strings = {
11+
jQuery.timeago.settings.strings["ca"] = {
1212
prefixAgo: "fa",
1313
prefixFromNow: "d'aquí",
1414
suffixAgo: null,

0 commit comments

Comments
 (0)