Skip to content

i18n

The following languages are supported(according to the order of translation time)

Ask for help to translate carbon in other languages

The following methods are supported

  • Constellation():get constellation name, like Aries
  • Season():get season name, like Spring
  • DiffForHumans():get the difference with human-readable format string, like 1 year from now
  • ToMonthString():output month format string, like January
  • ToShortMonthString():output short month format string, like Jan
  • ToWeekString():output week format string, like Sunday
  • ToShortWeekString():output short week format string, like Sun

Set locale

go
lang := carbon.NewLanguage()
lang.SetLocale("en")

carbon.SetTestNow(carbon.Parse("2020-08-05 13:14:15"))
now := carbon.Now().SetLanguage(lang)

now.Copy().AddHours(1).DiffForHumans() // 1 hour from now
now.Copy().AddHours(1).ToMonthString() // August
now.Copy().AddHours(1).ToShortMonthString() // Aug
now.Copy().AddHours(1).ToWeekString() // Wednesday
now.Copy().AddHours(1).ToShortWeekString() // Wed
now.Copy().AddHours(1).Constellation() // Leo
now.Copy().AddHours(1).Season() // Summer

Reset some resources

the rests still translate from the given locale

go
lang := carbon.NewLanguage()

resources := map[string]string {
  "hour": "%dh",
}
lang.SetLocale("en").SetResources(resources)

carbon.SetTestNow(carbon.Parse("2020-08-05 13:14:15"))
now := carbon.Now().SetLanguage(lang)

now.Copy().AddYears(1).DiffForHumans() // 1 year from now
now.Copy().AddHours(1).DiffForHumans() // 1h from now
now.ToMonthString() // August
now.ToShortMonthString() // Aug
now.ToWeekString() // Tuesday
now.ToShortWeekString() // Tue
now.Constellation() // Leo
now.Season() // Summer

Reset all resources

go
lang := carbon.NewLanguage()
resources := map[string]string {
  "months": "january|february|march|april|may|june|july|august|september|october|november|december",
  "short_months": "jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec",
  "weeks": "sunday|monday|tuesday|wednesday|thursday|friday|saturday",
  "short_weeks": "sun|mon|tue|wed|thu|fri|sat",
  "seasons": "spring|summer|autumn|winter",
  "constellations": "aries|taurus|gemini|cancer|leo|virgo|libra|scorpio|sagittarius|capricornus|aquarius|pisce",
  "year": "1 yr|%d yrs",
  "month": "1 mo|%d mos",
  "week": "%dw",
  "day": "%dd",
  "hour": "%dh",
  "minute": "%dm",
  "second": "%ds",
  "now": "just now",
  "ago": "%s ago",
  "from_now": "in %s",
  "before": "%s before",
  "after": "%s after",
}
lang.SetResources(resources)

carbon.SetTestNow(carbon.Parse("2020-08-05 13:14:15"))
now := carbon.Now().SetLanguage(lang)

now.Copy().AddYears(1).DiffForHumans() // in 1 yr
now.Copy().AddHours(1).DiffForHumans() // in 1h
now.ToMonthString() // august
now.ToShortMonthString() // aug
now.ToWeekString() // tuesday
now.ToShortWeekString() // tue
now.Constellation() // leo
now.Season() // summer

Released under the MIT License, unauthorized reproduction is prohibited in any form