示例#1
0
func (w *weatherCom) dlCurrent() error {
	if !w.DisplayCurrentIcon {
		return nil
	}

	cur := &Current{}
	url := fmt.Sprintf(WeatherComURLBase+WeatherComSuffixCurrent+unitTemp(w.UseCelcius), w.LocationCode)
	e := download.XML(url, cur)
	if e != nil {
		return e
	}

	// Received data is valid, update it.
	w.current = cur
	cur.WeatherDescription = tran.Splug(cur.WeatherDescription)

	// Prepend degree symbol to unit if missing.
	cur.UnitTemp = unitDegree(cur.UnitTemp)

	// Parse sun time.
	cur.TxtSunrise, cur.TxtSunset, cur.IsNight, e = FormatSun(cur.Sunrise, cur.Sunset, w.Time24H)

	// Parse update time.
	upd, e := time.Parse("1/2/06 3:04 PM MST", cur.UpdateTime)
	if e != nil {
		return e
	}
	cur.TxtUpdateTime = upd.Format(timeFormat(w.Time24H))

	return nil
}
示例#2
0
文件: weather.go 项目: sqp/godock
// FormatField formats a list of fields with the given template.
//
// It's a callback from the template, where the list of fields to display is set.
// With this method, we can translate titles, align data and allow custom
// templating for the user.
//
func FormatField(template *cdtype.Template, list []string, data interface{}) (string, error) {
	buf := bytes.NewBuffer(nil)
	tw := &tabwriter.Writer{}

	tw.Init(buf, 0, 4, 1, '\t', 0)
	needNL := false

	for _, key := range list {
		if needNL {
			fmt.Fprintln(tw)
		}
		needNL = true
		title := ""
		switch key {
		case "tempReal":
			title = tran.Splug("Temperature")

		case "tempFelt":
			title = tran.Splug("Feels like")

		case "wind":
			title = tran.Splug("Wind")

		case "humidity":
			title = tran.Splug("Humidity")

		case "pressure":
			title = tran.Splug("Pressure")

		case "sun":
			title = tran.Splug("Sunrise") + " - " + tran.Splug("Sunset")

		case "tempDay":
			title = tran.Splug("Temperature")

		case "precipitation":
			title = tran.Splug("Precipitation probability")
		}

		fmt.Fprint(tw, title+":\t")
		e := template.ExecuteTemplate(tw, key, data)
		if e != nil {
			return "", e
		}
	}
	tw.Flush()
	return strings.Trim(buf.String(), " \n"), nil
}
示例#3
0
func (w *weatherCom) dlForecast() error {
	w.forecast = &Forecast{}
	url := fmt.Sprintf(WeatherComURLBase+WeatherComSuffixForecast+unitTemp(w.UseCelcius), w.LocationCode, w.NbDays+1)
	e := download.XML(url, w.forecast)
	if e != nil {
		return e
	}
	w.forecast.UnitTemp = unitDegree(w.forecast.UnitTemp)

	// Parse day number and sun time.
	for i := range w.forecast.Days {
		day := &w.forecast.Days[i]
		date, e := time.Parse("Jan 2", day.Date)
		if e != nil {
			return e
		}
		day.MonthDay = date.Day()

		day.TxtSunrise, day.TxtSunset, _, e = FormatSun(day.Sunrise, day.Sunset, w.Time24H)
		if e != nil {
			return e
		}

		day.DayName = tran.Splug(day.DayName)
		for i := range day.Part {

			if day.Part[i].WeatherDescription == "" {
				day.Part[i].WeatherDescription = ValueMissing
			} else {
				day.Part[i].WeatherDescription = tran.Splug(day.Part[i].WeatherDescription)
			}
		}
	}

	return nil
}
示例#4
0
文件: tran_test.go 项目: sqp/godock
func TestTranslate(t *testing.T) {
	os.Setenv("LANGUAGE", "fr")

	// Translate dock strings.
	for in, out := range map[string]string{
		"Add":    "Ajouter",
		"Edit":   "Éditer",
		"Window": "Fenêtre",
	} {
		ret := tran.Slate(in)
		assert.Equal(t, ret, out, "translate %s != %s", ret, out)
	}

	// Translate plugins directly.
	for in, out := range map[string]string{
		"Animation when music changes:": "Animation au changement de musique",
		"Saturday":                      "Samedi",
	} {
		ret := tran.Splug(in)
		assert.Equal(t, ret, out, "translate %s != %s", ret, out)
	}

	// Translate plugins with domain name.
	for in, out := range map[string]string{
		"Lock position?":    "Verrouiller la position ?",
		"Image filename:":   "Nom du fichier de l'image :",
		"Font:":             "Police",
		"Use a custom font": "Utiliser une police personnalisée",
	} {
		ret := tran.Sloc("cairo-dock-plugins", in)
		assert.Equal(t, ret, out, "translate %s != %s", ret, out)
	}

	// Other string unchanged.
	for _, str := range []string{"unknown", "unchanged"} {
		ret := tran.Slate(str)
		assert.Equal(t, ret, str, "untranslated %s != %s", ret, str)
	}
}