// RelativeDate works like Date, but uses relative names when possible. e.g. // in English "yesterday", "today" and "tomorrow" are the available relative // names, but other languages might have more or none of them. func RelativeDate(lang i18n.Languager, t time.Time, style DateStyle) string { today := time.Now().Add(-12 * time.Hour).Round(day) dateDay := t.Add(-12 * time.Hour).Round(day) if today.Equal(dateDay) { /// TODAY return i18n.Tc(lang, "formatutil", "today") } if today.AddDate(0, 0, -1).Equal(dateDay) { /// YESTERDAY return i18n.Tc(lang, "formatutil", "yesterday") } return Date(lang, t, style) }
func main() { /// Please, translate this. /// This comment is still part of the translation comment. // This comment is not part of the translation comment. fmt.Println(i18n.T(nil, "Hello world\n")) /// This translation has a context fmt.Println(i18n.Tc(nil, "second", "Hello world\n")) /// This is a long translation, to test line splitting in quoted strings. fmt.Println(i18n.T(nil, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sed ante ut massa ultrices auctor. Vivamus rutrum ut ante et aliquet. Proin ut rutrum enim, a elementum ligula. Morbi malesuada.")) // This is not part of the translation comment. // // T: Translate this too. fmt.Println(i18n.T(nil, "Bye"+" "+"wor\"ld")) /*/ This whole comment is part of the translation comment. And it keeps newlines, but strips leading whitespace. */ // This, however, is not. fmt.Println(i18n.T(nil, "Hello again "+"world")) for ii := 0; ii < 5; ii++ { /// Using i18n.Sprintfn. The format string is fixed by i18n.Sprintfn /// so it doesn't show any extra arguments. fmt.Println(i18n.Sprintfn(nil, "Hello one world", "Hello %d worlds", ii, ii)) } }
// Date formats the date part of the given time.Time according to // the given DateStyle. func Date(lang i18n.Languager, t time.Time, style DateStyle) string { var layout string switch style { case DateStyleShort: /// SHORT DATE FORMAT layout = i18n.Tc(lang, "formatutil", "02/01/2006") default: fallthrough case DateStyleMedium: /// MEDIUM DATE FORMAT layout = i18n.Tc(lang, "formatutil", "Jan 2, 2006") case DateStyleLong: /// LONG DATE FORMAT layout = i18n.Tc(lang, "formatutil", "January 2, 2006") } return t.Format(layout) }
func formatNumber(lang i18n.Languager, integer string, decimal string) string { /// THOUSANDS SEPARATOR tSep := i18n.Tc(lang, "formautil", ",") var buf bytes.Buffer ii := 0 for _, c := range stringutil.Reverse(integer) { if ii == 3 { buf.WriteString(tSep) ii = 0 } buf.WriteRune(c) ii++ } s := stringutil.Reverse(buf.String()) if decimal != "" { /// DECIMAL SEPARATOR dSep := i18n.Tc(lang, "formautil", ".") return s + dSep + decimal } return s }
func (c *Context) Tc(context string, str string) string { return i18n.Tc(c, context, str) }