Skip to content

Commit b5beb20

Browse files
committed
fix(timeago): fixed a timeago display bug
Fixed display bug caused by the actual locale name not exactly matching the name in the locale support list of "timeago.js". (Example: When the locale is "zh", timeago will always have the fallback version "en" as the locale.) Signed-off-by: ketikai <[email protected]>
1 parent 66b952c commit b5beb20

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed
Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
// Script to ensure timeago keeps working when
2-
// used with mkdocs-material's instant loading feature
1+
// Script to ensure timeago keeps working when
2+
// used with mkdocs-material's instant loading feature
3+
4+
function getLocale(element) {
5+
var raw_locale = element.getAttribute('locale');
6+
var locale = {
7+
bn: 'bn_IN',
8+
en: 'en_US',
9+
hi: 'hi_IN',
10+
id: 'id_ID',
11+
nb: 'nb_NO',
12+
nn: 'nn_NO',
13+
pt: 'pt_BR',
14+
zh: 'zh_CN'
15+
}[raw_locale];
16+
return locale ? locale : raw_locale;
17+
}
318

419
if (typeof document$ !== "undefined") {
520
document$.subscribe(function() {
621
var nodes = document.querySelectorAll('.timeago');
722
if (nodes.length > 0) {
8-
var locale = nodes[0].getAttribute('locale');
9-
timeago.render(nodes, locale);
23+
var locale = getLocale(nodes[0]);
24+
timeago.render(nodes, locale);
1025
}
1126
})
1227
} else {
1328
var nodes = document.querySelectorAll('.timeago');
1429
if (nodes.length > 0) {
15-
var locale = nodes[0].getAttribute('locale');
16-
timeago.render(nodes, locale);
30+
var locale = getLocale(nodes[0]);
31+
timeago.render(nodes, locale);
1732
}
1833
}

0 commit comments

Comments
 (0)