Skip to content

Commit

Permalink
Merge pull request #56 from n-jaisabai/master
Browse files Browse the repository at this point in the history
Add support for thai language (th_TH)
  • Loading branch information
goodsign authored Jul 8, 2022
2 parents 00752ef + f122332 commit 04a3233
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const (
LocaleCsCZ = "cs_CZ" // Czech (Czech Republic)
LocaleSlSI = "sl_SI" // Slovenian (Slovenia)
LocaleLtLT = "lt_LT" // Lithuanian (Lithuania)
LocaleThTH = "th_TH" // Thai (Thailand)
)
```

Expand Down
13 changes: 13 additions & 0 deletions default_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ const (
DefaultFormatLtLTShort = "2006-01-02"
DefaultFormatLtLTDateTime = "2006-01-02, 15:04"
DefaultFormatLtLTTime = "15:04"

DefaultFormatThTHFull = "Monday ที่ 2 January 2006" // Thai (Thailand)
DefaultFormatThTHLong = "วันที่ 2 January 2006"
DefaultFormatThTHMedium = "2 Jan 2006"
DefaultFormatThTHShort = "02/01/2006"
DefaultFormatThTHDateTime = "02/01/2006 15:04"
DefaultFormatThTHTime = "15:04"
)

// FullFormatsByLocale maps locales to the'full' date formats for all
Expand Down Expand Up @@ -297,6 +304,7 @@ var FullFormatsByLocale = map[Locale]string{
LocaleCsCZ: DefaultFormatCsCZFull,
LocaleUkUA: DefaultFormatUkUAFull,
LocaleLtLT: DefaultFormatLtLTFull,
LocaleThTH: DefaultFormatThTHFull,
}

// LongFormatsByLocale maps locales to the 'long' date formats for all
Expand Down Expand Up @@ -338,6 +346,7 @@ var LongFormatsByLocale = map[Locale]string{
LocaleCsCZ: DefaultFormatCsCZLong,
LocaleUkUA: DefaultFormatUkUALong,
LocaleLtLT: DefaultFormatLtLTLong,
LocaleThTH: DefaultFormatThTHLong,
}

// MediumFormatsByLocale maps locales to the 'medium' date formats for all
Expand Down Expand Up @@ -379,6 +388,7 @@ var MediumFormatsByLocale = map[Locale]string{
LocaleCsCZ: DefaultFormatCsCZMedium,
LocaleUkUA: DefaultFormatUkUAMedium,
LocaleLtLT: DefaultFormatLtLTMedium,
LocaleThTH: DefaultFormatThTHMedium,
}

// ShortFormatsByLocale maps locales to the 'short' date formats for all
Expand Down Expand Up @@ -420,6 +430,7 @@ var ShortFormatsByLocale = map[Locale]string{
LocaleCsCZ: DefaultFormatCsCZShort,
LocaleUkUA: DefaultFormatUkUAShort,
LocaleLtLT: DefaultFormatLtLTShort,
LocaleThTH: DefaultFormatThTHShort,
}

// DateTimeFormatsByLocale maps locales to the 'DateTime' date formats for
Expand Down Expand Up @@ -461,6 +472,7 @@ var DateTimeFormatsByLocale = map[Locale]string{
LocaleCsCZ: DefaultFormatCsCZDateTime,
LocaleUkUA: DefaultFormatUkUADateTime,
LocaleLtLT: DefaultFormatLtLTDateTime,
LocaleThTH: DefaultFormatThTHDateTime,
}

// TimeFormatsByLocale maps locales to the 'Time' date formats for
Expand Down Expand Up @@ -502,4 +514,5 @@ var TimeFormatsByLocale = map[Locale]string{
LocaleCsCZ: DefaultFormatCsCZTime,
LocaleUkUA: DefaultFormatUkUATime,
LocaleLtLT: DefaultFormatLtLTTime,
LocaleThTH: DefaultFormatThTHTime,
}
84 changes: 84 additions & 0 deletions format_th_th.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package monday

import "strings"

// ============================================================
// Format rules for "th_TH" locale: Thai (Thailand)
// ============================================================

var longDayNamesThTH = map[string]string{
"Sunday": "วันอาทิตย์",
"Monday": "วันจันทร์",
"Tuesday": "วันอังคาร",
"Wednesday": "วันพุธ",
"Thursday": "วันพฤหัสบดี",
"Friday": "วันศุกร์",
"Saturday": "วันเสาร์",
}

var shortDayNamesThTH = map[string]string{
"Sun": "อา.",
"Mon": "จ.",
"Tue": "อ.",
"Wed": "พ.",
"Thu": "พฤ.",
"Fri": "ศ.",
"Sat": "ส.",
}

var longMonthNamesThTH = map[string]string{
"January": "มกราคม",
"February": "กุมภาพันธ์",
"March": "มีนาคม",
"April": "เมษายน",
"May": "พฤษภาคม",
"June": "มิถุนายน",
"July": "กรกฎาคม",
"August": "สิงหาคม",
"September": "กันยายน",
"October": "ตุลาคม",
"November": "พฤศจิกายน",
"December": "ธันวาคม",
}

var shortMonthNamesThTH = map[string]string{
"Jan": "ม.ค.",
"Feb": "ก.พ.",
"Mar": "มี.ค.",
"Apr": "เม.ย.",
"May": "พ.ค.",
"Jun": "มิ.ย.",
"Jul": "ก.ค.",
"Aug": "ส.ค.",
"Sep": "ก.ย.",
"Oct": "ต.ค.",
"Nov": "พ.ย.",
"Dec": "ธ.ค.",
}

func parseFuncThCommon(locale Locale) internalParseFunc {
return func(layout, value string) string {
// This special case is needed because th_TH... contains month and day names
// that consist of dots, and special character. Example: "February" = "กุมภาพันธ์", "Feb" = "ก.พ."
//
// This means that probably default time package layout IDs like 'January' or 'Jan'
// shouldn't be used in th_TH. But this is a time-compatible package, so someone
// might actually use those and we need to replace those before doing standard procedures.
for k, v := range knownMonthsShortReverse[locale] {
value = strings.Replace(value, k, v, -1)
}
for k, v := range knownDaysShortReverse[locale] {
value = strings.Replace(value, k, v, -1)
}
for k, v := range knownMonthsLongReverse[locale] {
value = strings.Replace(value, k, v, -1)
}
for k, v := range knownDaysLongReverse[locale] {
value = strings.Replace(value, k, v, -1)
}

return commonFormatFunc(value, layout,
knownDaysShortReverse[locale], knownDaysLongReverse[locale],
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale], knownPeriods[locale])
}
}
2 changes: 2 additions & 0 deletions locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
LocaleCsCZ = "cs_CZ" // Czech (Czech Republic)
LocaleSlSI = "sl_SI" // Slovenian (Slovenia)
LocaleLtLT = "lt_LT" // Lithuanian (Lithuania)
LocaleThTH = "th_TH" // Thai (Thailand)
)

// ListLocales returns all locales supported by the package.
Expand Down Expand Up @@ -89,5 +90,6 @@ func ListLocales() []Locale {
LocaleCsCZ,
LocaleSlSI,
LocaleLtLT,
LocaleThTH,
}
}
8 changes: 8 additions & 0 deletions monday.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var internalFormatFuncs = map[Locale]internalFormatFunc{
LocaleCsCZ: createCommonFormatFunc(LocaleCsCZ),
LocaleSlSI: createCommonFormatFunc(LocaleSlSI),
LocaleLtLT: createCommonFormatFuncWithGenitive(LocaleLtLT),
LocaleThTH: createCommonFormatFunc(LocaleThTH),
}

// internalParseFunc is a preprocessor for default time.ParseInLocation func
Expand Down Expand Up @@ -93,6 +94,7 @@ var internalParseFuncs = map[Locale]internalParseFunc{
LocaleCsCZ: createCommonParseFunc(LocaleCsCZ),
LocaleSlSI: createCommonParseFunc(LocaleSlSI),
LocaleLtLT: createCommonParsetFuncWithGenitive(LocaleLtLT),
LocaleThTH: parseFuncThCommon(LocaleThTH),
}

var knownDaysShort = map[Locale]map[string]string{} // Mapping for 'Format', days of week, short form
Expand Down Expand Up @@ -365,6 +367,12 @@ func fillKnownWords() {
fillKnownMonthsShort(shortMonthNamesLtLT, LocaleLtLT)
fillKnownMonthsGenitiveLong(longMonthNamesGenitiveLtLT, LocaleLtLT)
fillKnownMonthsGenitiveShort(shortMonthNamesGenitiveLtLT, LocaleLtLT)

// Th_TH: Thai (Thailand)
fillKnownDaysLong(longDayNamesThTH, LocaleThTH)
fillKnownDaysShort(shortDayNamesThTH, LocaleThTH)
fillKnownMonthsLong(longMonthNamesThTH, LocaleThTH)
fillKnownMonthsShort(shortMonthNamesThTH, LocaleThTH)
}

func fill(src map[string]string, dest map[Locale]map[string]string, locale Locale) {
Expand Down
8 changes: 8 additions & 0 deletions monday_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ var formatTests = []FormatTest{
{LocaleSlSI, time.Date(2013, 5, 13, 0, 0, 0, 0, time.UTC), "2 Jan 2006", "13 maj 2013"},
{LocaleSlSI, time.Date(0, 5, 1, 0, 0, 0, 0, time.UTC), "January", "maj"},
{LocaleSlSI, time.Date(0, 5, 13, 0, 0, 0, 0, time.UTC), "2 January", "13 maj"},

{LocaleThTH, time.Date(2022, 7, 8, 0, 0, 0, 0, time.UTC), "Mon Jan 2 2006", "ศ. ก.ค. 8 2022"},
{LocaleThTH, time.Date(2022, 7, 8, 0, 0, 0, 0, time.UTC), "Monday Jan 2 2006", "วันศุกร์ ก.ค. 8 2022"},
{LocaleThTH, time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC), "Monday January 02 2006", "วันอังคาร กุมภาพันธ์ 01 2022"},
{LocaleThTH, time.Date(2022, 5, 13, 0, 0, 0, 0, time.UTC), "2006. 2 January.", "2022. 13 พฤษภาคม."},
{LocaleThTH, time.Date(2022, 5, 13, 0, 0, 0, 0, time.UTC), "2 Jan 2006", "13 พ.ค. 2022"},
{LocaleThTH, time.Date(0, 5, 1, 0, 0, 0, 0, time.UTC), "January", "พฤษภาคม"},
{LocaleThTH, time.Date(0, 5, 13, 0, 0, 0, 0, time.UTC), "2 January", "13 พฤษภาคม"},
}

func TestFormat(t *testing.T) {
Expand Down

0 comments on commit 04a3233

Please sign in to comment.