Skip to content

Commit

Permalink
Localize time units on activity heatmap (#21570)
Browse files Browse the repository at this point in the history
  • Loading branch information
5 people authored Oct 28, 2022
1 parent 76e9a4f commit 125e3e3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
8 changes: 4 additions & 4 deletions web_src/js/components/ActivityHeatmap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export default {
type: Array,
default: () => [],
},
locale: {
type: Object,
default: () => {},
}
},
data: () => ({
colorRange: [
Expand All @@ -36,10 +40,6 @@ export default {
'var(--color-primary-dark-4)',
],
endDate: new Date(),
locale: {
contributions: 'contributions',
no_contributions: 'No contributions',
},
}),
computed: {
sum() {
Expand Down
11 changes: 9 additions & 2 deletions web_src/js/features/heatmap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createApp} from 'vue';
import ActivityHeatmap from '../components/ActivityHeatmap.vue';

import {translateMonth, translateDay} from '../utils.js';
export default function initHeatmap() {
const el = document.getElementById('user-heatmap');
if (!el) return;
Expand All @@ -17,7 +17,14 @@ export default function initHeatmap() {
return {date: new Date(v), count: heatmap[v]};
});

const View = createApp(ActivityHeatmap, {values});
const locale = {
months: new Array(12).fill().map((_, idx) => translateMonth(idx)),
days: new Array(7).fill().map((_, idx) => translateDay(idx)),
contributions: 'contributions',
no_contributions: 'No contributions',
};

const View = createApp(ActivityHeatmap, {values, locale});

View.mount(el);
} catch (err) {
Expand Down
15 changes: 15 additions & 0 deletions web_src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,18 @@ export function prettyNumber(num, locale = 'en-US') {
export function parseUrl(str) {
return new URL(str, str.startsWith('http') ? undefined : window.location.origin);
}

// return current locale chosen by user
function getCurrentLocale() {
return document.documentElement.lang;
}

// given a month (0-11), returns it in the documents language
export function translateMonth(month) {
return new Date(Date.UTC(2022, month, 12)).toLocaleString(getCurrentLocale(), {month: 'short'});
}

// given a weekday (0-6, Sunday to Saturday), returns it in the documents language
export function translateDay(day) {
return new Date(Date.UTC(2022, 7, day)).toLocaleString(getCurrentLocale(), {weekday: 'short'});
}
24 changes: 23 additions & 1 deletion web_src/js/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect, test} from 'vitest';
import {
basename, extname, isObject, uniq, stripTags, joinPaths, parseIssueHref,
prettyNumber, parseUrl,
prettyNumber, parseUrl, translateMonth, translateDay
} from './utils.js';

test('basename', () => {
Expand Down Expand Up @@ -109,3 +109,25 @@ test('parseUrl', () => {
expect(parseUrl('https://localhost/path?search').search).toEqual('?search');
expect(parseUrl('https://localhost/path?search#hash').hash).toEqual('#hash');
});

test('translateMonth', () => {
const originalLang = document.documentElement.lang;
document.documentElement.lang = 'en-US';
expect(translateMonth(0)).toEqual('Jan');
expect(translateMonth(4)).toEqual('May');
document.documentElement.lang = 'es-ES';
expect(translateMonth(5)).toEqual('jun');
expect(translateMonth(6)).toEqual('jul');
document.documentElement.lang = originalLang;
});

test('translateDay', () => {
const originalLang = document.documentElement.lang;
document.documentElement.lang = 'fr-FR';
expect(translateDay(1)).toEqual('lun.');
expect(translateDay(5)).toEqual('ven.');
document.documentElement.lang = 'pl-PL';
expect(translateDay(1)).toEqual('pon.');
expect(translateDay(5)).toEqual('pt.');
document.documentElement.lang = originalLang;
});

0 comments on commit 125e3e3

Please sign in to comment.