Skip to content

Commit 8dbd270

Browse files
committed
Localize heatmap
Signed-off-by: Yarden Shoham <[email protected]>
1 parent 2902d1e commit 8dbd270

File tree

7 files changed

+44
-16
lines changed

7 files changed

+44
-16
lines changed

models/activities/user_heatmap.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@ func getUserHeatmapData(user *user_model.User, team *organization.Team, doer *us
6969
OrderBy("timestamp").
7070
Find(&hdata)
7171
}
72+
73+
// GetTotalContributionsInHeatmap returns the total number of contributions in a heatmap
74+
func GetTotalContributionsInHeatmap(hdata []*UserHeatmapData) int64 {
75+
var total int64
76+
for _, v := range hdata {
77+
total += v.Contributions
78+
}
79+
return total
80+
}

options/locale/locale_en-US.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ footer = Footer
120120
footer.software = About Software
121121
footer.links = Links
122122

123+
[heatmap]
124+
number_of_contributions_in_the_last_12_months = %s contributions in the last 12 months
125+
no_contributions = No contributions
126+
less = Less
127+
more = More
128+
number_of_contributions_on_date = <b>%[1]s contributions</b> on %[2]s
129+
123130
[editor]
124131
buttons.heading.tooltip = Add heading
125132
buttons.bold.tooltip = Add bold text

routers/web/user/home.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func Dashboard(ctx *context.Context) {
107107
return
108108
}
109109
ctx.Data["HeatmapData"] = data
110+
ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data)
110111
}
111112

112113
feeds, count, err := activities_model.GetFeeds(ctx, activities_model.GetFeedsOptions{

routers/web/user/profile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func Profile(ctx *context.Context) {
7474
return
7575
}
7676
ctx.Data["HeatmapData"] = data
77+
ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data)
7778
}
7879

7980
if len(ctx.ContextUser.Description) != 0 {

templates/user/heatmap.tmpl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{{if .HeatmapData}}
2-
<div id="user-heatmap" data-heatmap-data="{{Json .HeatmapData}}">
2+
<div id="user-heatmap"
3+
data-heatmap-data="{{Json .HeatmapData}}"
4+
data-locale-total-contributions-html="{{$.locale.Tr "heatmap.number_of_contributions_in_the_last_12_months" (LocaleNumber .HeatmapTotalContributions)}}"
5+
data-locale-no-contributions="{{.locale.Tr "heatmap.no_contributions"}}"
6+
data-locale-more="{{.locale.Tr "heatmap.more"}}"
7+
data-locale-less="{{.locale.Tr "heatmap.less"}}"
8+
data-locale-contributions-on="{{.locale.Tr "heatmap.number_of_contributions_on_date"}}"
9+
>
310
<div slot="loading">
411
<div class="ui active centered inline indeterminate text loader" id="loading-heatmap">{{.locale.Tr "user.heatmap.loading"}}</div>
512
</div>

web_src/js/components/ActivityHeatmap.vue

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<template>
22
<div id="user-heatmap">
3-
<div class="total-contributions">
4-
{{ sum }} contributions in the last 12 months
5-
</div>
3+
<!-- eslint-disable-next-line vue/no-v-html (safely generated in the backend) -->
4+
<div class="total-contributions" v-html="locale.contributions_in_the_last_12_months_html"/>
65
<calendar-heatmap
76
:locale="locale"
87
:no-data-text="locale.no_contributions"
9-
:tooltip-unit="locale.contributions"
8+
:tooltip-formatter="(v) => tooltipFormatter(v, locale)"
109
:end-date="endDate"
1110
:values="values"
1211
:range-color="colorRange"
@@ -41,15 +40,6 @@ export default {
4140
],
4241
endDate: new Date(),
4342
}),
44-
computed: {
45-
sum() {
46-
let s = 0;
47-
for (let i = 0; i < this.values.length; i++) {
48-
s += this.values[i].count;
49-
}
50-
return s;
51-
}
52-
},
5343
mounted() {
5444
// work around issue with first legend color being rendered twice and legend cut off
5545
const legend = document.querySelector('.vch__external-legend-wrapper');
@@ -74,6 +64,16 @@ export default {
7464
7565
const newSearch = params.toString();
7666
window.location.search = newSearch.length ? `?${newSearch}` : '';
67+
},
68+
tooltipFormatter(v, locale) {
69+
// TODO: use the localized number as below (why is it throwing Uncaught DOMException: Failed to execute
70+
// 'attachShadow' on 'Element': Shadow root cannot be created on a host which already hosts a shadow tree.?)
71+
// const number = `<gitea-locale-number data-number="${v.count}">${v.count}</gitea-locale-number>`;
72+
const number = v.count;
73+
const datetime = v.date.toISOString();
74+
const fallback = v.date.toLocaleDateString();
75+
const date = `<relative-time format="datetime" year="numeric" month="short" day="numeric" weekday="" datetime="${datetime}">${fallback}</relative-time>`;
76+
return locale.contributions_on.replace('%[1]s', number).replace('%[2]s', date);
7777
}
7878
},
7979
};

web_src/js/features/heatmap.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ export function initHeatmap() {
2121
const locale = {
2222
months: new Array(12).fill().map((_, idx) => translateMonth(idx)),
2323
days: new Array(7).fill().map((_, idx) => translateDay(idx)),
24-
contributions: 'contributions',
25-
no_contributions: 'No contributions',
24+
contributions_in_the_last_12_months_html: el.getAttribute('data-locale-total-contributions-html'),
25+
no_contributions: el.getAttribute('data-locale-no-contributions'),
26+
more: el.getAttribute('data-locale-more'),
27+
less: el.getAttribute('data-locale-less'),
28+
contributions_on: el.getAttribute('data-locale-contributions-on'),
2629
};
2730

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

0 commit comments

Comments
 (0)