Skip to content

Commit 038a182

Browse files
committed
Added initial localization for eng and fin.
1 parent d197a0c commit 038a182

File tree

7 files changed

+199
-35
lines changed

7 files changed

+199
-35
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/raspi/kallu
22

33
go 1.18
4+
5+
require golang.org/x/text v0.3.7

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
2+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=

locales/base/iface.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package base
2+
3+
import "time"
4+
5+
type Locale interface {
6+
GetWeekDays() map[time.Weekday]string // mon-sun
7+
GetMonths() map[time.Month]string // jan-dec
8+
GetWeek() string // `week` localized
9+
}

locales/en/en.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package en
2+
3+
import (
4+
"github.com/raspi/kallu/locales/base"
5+
"time"
6+
)
7+
8+
// Check implementation
9+
var _ base.Locale = English{}
10+
11+
type English struct {
12+
}
13+
14+
func (f English) GetWeek() string {
15+
return `week`
16+
}
17+
18+
func (f English) GetWeekDays() map[time.Weekday]string {
19+
return map[time.Weekday]string{
20+
time.Monday: `mon`,
21+
time.Tuesday: `tue`,
22+
time.Wednesday: `wed`,
23+
time.Thursday: `thu`,
24+
time.Friday: `fri`,
25+
time.Saturday: `sat`,
26+
time.Sunday: `sun`,
27+
}
28+
}
29+
30+
func (f English) GetMonths() map[time.Month]string {
31+
return map[time.Month]string{
32+
time.January: `january`, // 1
33+
time.February: `february`,
34+
time.March: `march`,
35+
time.April: `april`,
36+
time.May: `may`,
37+
time.June: `june`, // 6
38+
time.July: `july`,
39+
time.August: `august`,
40+
time.September: `september`,
41+
time.October: `october`,
42+
time.November: `november`,
43+
time.December: `december`, // 12
44+
}
45+
}

locales/fi/fi.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package fi
2+
3+
import (
4+
"github.com/raspi/kallu/locales/base"
5+
"time"
6+
)
7+
8+
// Check implementation
9+
var _ base.Locale = Finnish{}
10+
11+
type Finnish struct {
12+
}
13+
14+
func (f Finnish) GetWeek() string {
15+
return `vko`
16+
}
17+
18+
func (f Finnish) GetWeekDays() map[time.Weekday]string {
19+
return map[time.Weekday]string{
20+
time.Monday: `ma`,
21+
time.Tuesday: `ti`,
22+
time.Wednesday: `ke`,
23+
time.Thursday: `to`,
24+
time.Friday: `pe`,
25+
time.Saturday: `la`,
26+
time.Sunday: `su`,
27+
}
28+
}
29+
30+
func (f Finnish) GetMonths() map[time.Month]string {
31+
return map[time.Month]string{
32+
time.January: `tammikuu`,
33+
time.February: `helmikuu`,
34+
time.March: `maaliskuu`,
35+
time.April: `huhtikuu`,
36+
time.May: `toukokuu`,
37+
time.June: `kesäkuu`,
38+
time.July: `heinäkuu`,
39+
time.August: `elokuu`,
40+
time.September: `syyskuu`,
41+
time.October: `lokakuu`,
42+
time.November: `marraskuu`,
43+
time.December: `joulukuu`,
44+
}
45+
}

main.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"github.com/raspi/kallu/locales/base"
7+
"github.com/raspi/kallu/locales/en"
8+
"github.com/raspi/kallu/locales/fi"
69
"github.com/raspi/kallu/month"
10+
"golang.org/x/text/language"
711
"os"
812
"strings"
913
"time"
@@ -78,6 +82,35 @@ func main() {
7882

7983
flag.Parse()
8084

85+
locale := language.English
86+
for _, k := range os.Environ() {
87+
arr := strings.SplitAfter(k, `=`)
88+
key, value := strings.TrimRight(arr[0], `=`), arr[1]
89+
90+
if key != `LANG` {
91+
continue
92+
}
93+
94+
// fi_FI => fi-FI
95+
value = strings.Replace(value, `_`, `-`, -1)
96+
value = strings.ToLower(value)
97+
if strings.Contains(value, `.`) {
98+
// remove .UTF-8
99+
value = value[:strings.LastIndex(value, `.`)]
100+
}
101+
102+
candidates, _, err := language.ParseAcceptLanguage(value)
103+
if err != nil {
104+
panic(err)
105+
}
106+
107+
if len(candidates) > 0 {
108+
locale = candidates[0]
109+
}
110+
111+
break
112+
}
113+
81114
if *selectedMonth > 12 || *selectedMonth == 0 {
82115
_, _ = fmt.Fprintf(os.Stderr, `invalid month: %d`, *selectedMonth)
83116
os.Exit(1)
@@ -90,6 +123,15 @@ func main() {
90123

91124
dow := time.Weekday(*selectedDow)
92125

126+
var monthlocale base.Locale = en.English{}
127+
128+
switch locale.Parent() {
129+
case language.Finnish:
130+
monthlocale = fi.Finnish{}
131+
default:
132+
monthlocale = en.English{}
133+
}
134+
93135
currentMonth := month.New(int(*selectedYear), time.Month(*selectedMonth), dow, now)
94136

95137
if *howManyChunks == 0 {
@@ -152,7 +194,7 @@ func main() {
152194
currChunk++
153195

154196
if currChunk == chunks {
155-
currentMonth.PrintMonth(monthList)
197+
currentMonth.PrintMonth(monthList, monthlocale)
156198
fmt.Println()
157199
monthList = []month.Month{}
158200
currChunk = 0
@@ -161,7 +203,7 @@ func main() {
161203

162204
if len(monthList) > 0 {
163205
// Print remaining month(s)
164-
currentMonth.PrintMonth(monthList)
206+
currentMonth.PrintMonth(monthList, monthlocale)
165207
fmt.Println()
166208
}
167209

month/month.go

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package month
22

33
import (
44
"fmt"
5+
"github.com/raspi/kallu/locales/base"
6+
"strings"
57
"time"
8+
"unicode/utf8"
69
)
710

811
type Month struct {
@@ -82,7 +85,7 @@ func (mon Month) GetNextMonth() Month {
8285
return New(last.Year(), last.Month(), mon.dow, mon.now)
8386
}
8487

85-
func (mon Month) PrintMonth(months []Month) {
88+
func (mon Month) PrintMonth(months []Month, locale base.Locale) {
8689
const (
8790
esc = "\033["
8891
Clear = esc + "0m"
@@ -93,45 +96,40 @@ func (mon Month) PrintMonth(months []Month) {
9396
DefaultFG = SetForeground + "250m"
9497
)
9598

99+
weekLocalized := locale.GetWeek()
100+
weekdaysLocalized := locale.GetWeekDays()
101+
monthsLocalized := locale.GetMonths()
102+
96103
monthCount := len(months)
97104

98-
maxweeks := 0
99-
for _, m := range months {
100-
start, end := m.GetStartEndWeek()
101-
weeks, _ := m.GetDaysWeeks(start, end)
102-
if weeks > maxweeks {
103-
maxweeks = weeks
104-
}
105+
var headerCol []string
106+
headerSize := 0
107+
108+
curr := mon.getStart()
109+
110+
// "Week" localized
111+
headerSize += 5
112+
headerCol = append(headerCol, weekLocalized)
113+
114+
// Day names
115+
for di := 0; di < 7; di++ {
116+
headerSize += 4
117+
headerCol = append(headerCol, weekdaysLocalized[curr.Weekday()])
118+
curr = curr.AddDate(0, 0, 1)
105119
}
120+
headerSize += 1
106121

107122
fmt.Print(SetBackground + "238m")
108123
fmt.Print(SetForeground + "245m")
109124

110125
// Print month and year header
111126
for mIdx, m := range months {
112-
header := fmt.Sprintf(`%v %4v`, m.GetMonth().Month(), m.GetMonth().Year())
127+
header := fmt.Sprintf(`%v %4v`, monthsLocalized[m.GetMonth().Month()], m.GetMonth().Year())
113128
if mon.now.Year() == m.m.Year() && mon.now.Month() == m.m.Month() {
114129
header = "[" + header + "]"
115130
}
116131

117-
// Week Mon Tue Wed Thu Fri Sat Sun
118-
119-
required := 34
120-
paddedHeader := header
121-
122-
for i := 0; i < required; i++ {
123-
if len(paddedHeader) < required {
124-
// Add padding to both sides
125-
paddedHeader = " " + paddedHeader + " "
126-
}
127-
}
128-
129-
if len(paddedHeader) > required {
130-
// Cut
131-
paddedHeader = paddedHeader[0:required]
132-
}
133-
134-
o := paddedHeader
132+
o := centerString(header, headerSize)
135133

136134
if mIdx < monthCount-1 {
137135
o += " | "
@@ -147,22 +145,38 @@ func (mon Month) PrintMonth(months []Month) {
147145
fmt.Print(SetForeground + "245m")
148146
fmt.Print(SetUnderlineOn)
149147

150-
for mIdx, m := range months {
151-
curr := m.getStart()
148+
for mIdx, _ := range months {
152149
if mIdx > 0 {
153150
// Separator
154151
fmt.Print(" | ")
155152
}
156153

157-
fmt.Print("Week ")
158-
for di := 0; di < 7; di++ {
159-
fmt.Printf(`%v `, curr.Weekday().String()[0:3])
160-
curr = curr.AddDate(0, 0, 1)
154+
for idx, hdr := range headerCol {
155+
switch idx {
156+
case 0: // Week
157+
fmt.Printf(`%4s `, hdr)
158+
default:
159+
fmt.Printf(`%4s`, hdr)
160+
}
161+
162+
if idx == 7 {
163+
fmt.Print(` `)
164+
}
161165
}
162166
}
163167

164168
fmt.Println(Clear)
165169

170+
// Calculate week row count
171+
maxweeks := 0
172+
for _, m := range months {
173+
start, end := m.GetStartEndWeek()
174+
weeks, _ := m.GetDaysWeeks(start, end)
175+
if weeks > maxweeks {
176+
maxweeks = weeks
177+
}
178+
}
179+
166180
for weekIndex := 0; weekIndex < maxweeks+1; weekIndex++ {
167181

168182
if (weekIndex & 1) == 0 {
@@ -253,3 +267,8 @@ func (mon Month) PrintMonth(months []Month) {
253267
fmt.Println(Clear)
254268
}
255269
}
270+
271+
func centerString(str string, width int) string {
272+
spaces := int(float64(width-utf8.RuneCountInString(str)) / 2)
273+
return strings.Repeat(" ", spaces) + str + strings.Repeat(" ", width-(spaces+utf8.RuneCountInString(str)))
274+
}

0 commit comments

Comments
 (0)