Skip to content

Commit 125e3e3

Browse files
yardenshohamGustedsilverwindlunnytechknowlogick
authored
Localize time units on activity heatmap (#21570)
Previously, the months and days were hardcoded into English * Closes #15541 ## Screenshots ### English ![image](https://user-images.githubusercontent.com/20454870/197410352-1b28a637-ce19-41ae-b4e5-27955555b082.png) ### German ![image](https://user-images.githubusercontent.com/20454870/197410455-f243ca84-807f-476e-b8ed-c24e827bfc2d.png) ### Spanish ![image](https://user-images.githubusercontent.com/20454870/197410366-55202ca5-08f9-4152-8f9d-d5eeebd532ef.png) ### Italian ![image](https://user-images.githubusercontent.com/20454870/197410385-75f754dd-e845-4444-8a04-472a8f45b617.png) ### Portuguese This one has a bit of overflow ![image](https://user-images.githubusercontent.com/20454870/197410414-b91f962e-77e9-4cc7-990b-01c0fc0cbd0b.png) Signed-off-by: Yarden Shoham <[email protected]> Co-authored-by: Gusted <[email protected]> Co-authored-by: silverwind <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 76e9a4f commit 125e3e3

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

Diff for: web_src/js/components/ActivityHeatmap.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export default {
2525
type: Array,
2626
default: () => [],
2727
},
28+
locale: {
29+
type: Object,
30+
default: () => {},
31+
}
2832
},
2933
data: () => ({
3034
colorRange: [
@@ -36,10 +40,6 @@ export default {
3640
'var(--color-primary-dark-4)',
3741
],
3842
endDate: new Date(),
39-
locale: {
40-
contributions: 'contributions',
41-
no_contributions: 'No contributions',
42-
},
4343
}),
4444
computed: {
4545
sum() {

Diff for: web_src/js/features/heatmap.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {createApp} from 'vue';
22
import ActivityHeatmap from '../components/ActivityHeatmap.vue';
3-
3+
import {translateMonth, translateDay} from '../utils.js';
44
export default function initHeatmap() {
55
const el = document.getElementById('user-heatmap');
66
if (!el) return;
@@ -17,7 +17,14 @@ export default function initHeatmap() {
1717
return {date: new Date(v), count: heatmap[v]};
1818
});
1919

20-
const View = createApp(ActivityHeatmap, {values});
20+
const locale = {
21+
months: new Array(12).fill().map((_, idx) => translateMonth(idx)),
22+
days: new Array(7).fill().map((_, idx) => translateDay(idx)),
23+
contributions: 'contributions',
24+
no_contributions: 'No contributions',
25+
};
26+
27+
const View = createApp(ActivityHeatmap, {values, locale});
2128

2229
View.mount(el);
2330
} catch (err) {

Diff for: web_src/js/utils.js

+15
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,18 @@ export function prettyNumber(num, locale = 'en-US') {
7070
export function parseUrl(str) {
7171
return new URL(str, str.startsWith('http') ? undefined : window.location.origin);
7272
}
73+
74+
// return current locale chosen by user
75+
function getCurrentLocale() {
76+
return document.documentElement.lang;
77+
}
78+
79+
// given a month (0-11), returns it in the documents language
80+
export function translateMonth(month) {
81+
return new Date(Date.UTC(2022, month, 12)).toLocaleString(getCurrentLocale(), {month: 'short'});
82+
}
83+
84+
// given a weekday (0-6, Sunday to Saturday), returns it in the documents language
85+
export function translateDay(day) {
86+
return new Date(Date.UTC(2022, 7, day)).toLocaleString(getCurrentLocale(), {weekday: 'short'});
87+
}

Diff for: web_src/js/utils.test.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {expect, test} from 'vitest';
22
import {
33
basename, extname, isObject, uniq, stripTags, joinPaths, parseIssueHref,
4-
prettyNumber, parseUrl,
4+
prettyNumber, parseUrl, translateMonth, translateDay
55
} from './utils.js';
66

77
test('basename', () => {
@@ -109,3 +109,25 @@ test('parseUrl', () => {
109109
expect(parseUrl('https://localhost/path?search').search).toEqual('?search');
110110
expect(parseUrl('https://localhost/path?search#hash').hash).toEqual('#hash');
111111
});
112+
113+
test('translateMonth', () => {
114+
const originalLang = document.documentElement.lang;
115+
document.documentElement.lang = 'en-US';
116+
expect(translateMonth(0)).toEqual('Jan');
117+
expect(translateMonth(4)).toEqual('May');
118+
document.documentElement.lang = 'es-ES';
119+
expect(translateMonth(5)).toEqual('jun');
120+
expect(translateMonth(6)).toEqual('jul');
121+
document.documentElement.lang = originalLang;
122+
});
123+
124+
test('translateDay', () => {
125+
const originalLang = document.documentElement.lang;
126+
document.documentElement.lang = 'fr-FR';
127+
expect(translateDay(1)).toEqual('lun.');
128+
expect(translateDay(5)).toEqual('ven.');
129+
document.documentElement.lang = 'pl-PL';
130+
expect(translateDay(1)).toEqual('pon.');
131+
expect(translateDay(5)).toEqual('pt.');
132+
document.documentElement.lang = originalLang;
133+
});

0 commit comments

Comments
 (0)