Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
Add language support
Browse files Browse the repository at this point in the history
Fixes #27
  • Loading branch information
kejadlen committed Apr 23, 2019
1 parent 037838b commit 36d4d14
Show file tree
Hide file tree
Showing 11 changed files with 1,026 additions and 441 deletions.
1,265 changes: 835 additions & 430 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dark-sky"
version = "3.0.4"
version = "3.1.0"
authors = ["Alpha Chen <alpha.chen@gmail.com>"]

[dependencies]
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ These environment variables can be [configured in Alfred][env-vars]:
location*.)
- `FORECAST_UNITS`: Defaults to `auto`, which sets the units based on the
location. Use `si` for Celsius and `us` for Fahrenheit.
- `DEFAULT_LAT_LONG`: Set this to override IP geolocation. Ex: `47.7396,-122.3426` for Seattle.
- `DEFAULT_LOCATION`: Used for displaying the location name when using `DEFAULT_LAT_LONG`.
- `FORECAST_LANG`: Defaults to `en`. See [Dark Sky
documentation][dark-sky-lang] for full list of language options.
- `DEFAULT_LAT_LONG`: Set this to override IP geolocation. Ex:
`47.7396,-122.3426` for Seattle.
- `DEFAULT_LOCATION`: Used for displaying the location name when using
`DEFAULT_LAT_LONG`.
- `LIGHT_ICONS`: `true` gives white icons, `false` gives black icons.

[env-vars]: https://www.alfredapp.com/help/workflows/advanced/variables/
[dark-sky-api-key]: https://darksky.net/dev/register
[google-api-key]: https://developers.google.com/maps/documentation/geocoding/#api_key
[dark-sky-lang]: https://darksky.net/dev/docs#forecast-request

# Attributions

Expand Down
59 changes: 57 additions & 2 deletions src/dark_sky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct DarkSky {
pub location: location::Location,
pub theme: Theme,
pub units: forecast::Units,
pub lang: forecast::Lang,
}

impl DarkSky {
Expand Down Expand Up @@ -60,9 +61,63 @@ impl DarkSky {
forecast::Units::Us => "us",
forecast::Units::Si => "si",
};
let lang = match self.lang {
forecast::Lang::Arabic => "ar",
forecast::Lang::Azerbaijani => "az",
forecast::Lang::Belarusian => "be",
forecast::Lang::Bulgarian => "bg",
forecast::Lang::Bengali => "bn",
forecast::Lang::Bosnian => "bs",
forecast::Lang::Catalan => "ca",
forecast::Lang::Czech => "cs",
forecast::Lang::Danish => "da",
forecast::Lang::German => "de",
forecast::Lang::Greek => "el",
forecast::Lang::English => "en",
forecast::Lang::Esperanto => "eo",
forecast::Lang::Spanish => "es",
forecast::Lang::Estonian => "et",
forecast::Lang::Finnish => "fi",
forecast::Lang::French => "fr",
forecast::Lang::Hebrew => "he",
forecast::Lang::Hindi => "hi",
forecast::Lang::Croatian => "hr",
forecast::Lang::Hungarian => "hu",
forecast::Lang::Indonesian => "id",
forecast::Lang::Icelandic => "is",
forecast::Lang::Italian => "it",
forecast::Lang::Japanese => "ja",
forecast::Lang::Georgian => "ka",
forecast::Lang::Kannada => "kn",
forecast::Lang::Korean => "ko",
forecast::Lang::Cornish => "kw",
forecast::Lang::Latvian => "lv",
forecast::Lang::Malayam => "ml",
forecast::Lang::Marathi => "mr",
forecast::Lang::NorwegianBokmal => "nb",
forecast::Lang::Dutch => "nl",
forecast::Lang::Punjabi => "pa",
forecast::Lang::Polish => "pl",
forecast::Lang::Portuguese => "pt",
forecast::Lang::Romanian => "ro",
forecast::Lang::Russian => "ru",
forecast::Lang::Slovak => "sk",
forecast::Lang::Slovenian => "sl",
forecast::Lang::Serbian => "sr",
forecast::Lang::Swedish => "sv",
forecast::Lang::Tamil => "ta",
forecast::Lang::Telugu => "te",
forecast::Lang::Tetum => "tet",
forecast::Lang::Turkish => "tr",
forecast::Lang::Ukrainian => "uk",
forecast::Lang::Urdu => "ur",
forecast::Lang::IgpayAtinlay => "x-pig-latin",
forecast::Lang::SimplifiedChinese => "zh",
forecast::Lang::TraditionalChinese => "zh-tw",
};
let url = format!(
"https://api.darksky.net/forecast/{}/{},{}?units={}",
self.dark_sky_api_key, lat, long, units,
"https://api.darksky.net/forecast/{}/{},{}?units={}&lang={}",
self.dark_sky_api_key, lat, long, units, lang
);
Ok(reqwest::get(&url)?.json()?)
}
Expand Down
1 change: 1 addition & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(deprecated)]
error_chain! {
foreign_links {
Http(::reqwest::Error);
Expand Down
56 changes: 56 additions & 0 deletions src/forecast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,62 @@ pub enum Units {
Si,
}

#[derive(Debug)]
pub enum Lang {
Arabic,
Azerbaijani,
Belarusian,
Bulgarian,
Bengali,
Bosnian,
Catalan,
Czech,
Danish,
German,
Greek,
English,
Esperanto,
Spanish,
Estonian,
Finnish,
French,
Hebrew,
Hindi,
Croatian,
Hungarian,
Indonesian,
Icelandic,
Italian,
Japanese,
Georgian,
Kannada,
Korean,
Cornish,
Latvian,
Malayam,
Marathi,
NorwegianBokmal,
Dutch,
Punjabi,
Polish,
Portuguese,
Romanian,
Russian,
Slovak,
Slovenian,
Serbian,
Swedish,
Tamil,
Telugu,
Tetum,
Turkish,
Ukrainian,
Urdu,
IgpayAtinlay,
SimplifiedChinese,
TraditionalChinese,
}

#[derive(Clone, Debug)]
pub enum Icon {
ClearDay,
Expand Down
59 changes: 58 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(range_contains, try_from)]
#![recursion_limit = "1024"]

extern crate alphred;
Expand Down Expand Up @@ -45,12 +44,70 @@ quick_main!(|| {
"si" => forecast::Units::Si,
units => bail!("invalid `FORECAST_UNITS`: '{}'", units),
};
let lang = env::var("FORECAST_LANG").unwrap_or_else(|_| "en".into());
let lang = match lang.as_str() {
"ar" => forecast::Lang::Arabic,
"az" => forecast::Lang::Azerbaijani,
"be" => forecast::Lang::Belarusian,
"bg" => forecast::Lang::Bulgarian,
"bn" => forecast::Lang::Bengali,
"bs" => forecast::Lang::Bosnian,
"ca" => forecast::Lang::Catalan,
"cs" => forecast::Lang::Czech,
"da" => forecast::Lang::Danish,
"de" => forecast::Lang::German,
"el" => forecast::Lang::Greek,
"en" => forecast::Lang::English,
"eo" => forecast::Lang::Esperanto,
"es" => forecast::Lang::Spanish,
"et" => forecast::Lang::Estonian,
"fi" => forecast::Lang::Finnish,
"fr" => forecast::Lang::French,
"he" => forecast::Lang::Hebrew,
"hi" => forecast::Lang::Hindi,
"hr" => forecast::Lang::Croatian,
"hu" => forecast::Lang::Hungarian,
"id" => forecast::Lang::Indonesian,
"is" => forecast::Lang::Icelandic,
"it" => forecast::Lang::Italian,
"ja" => forecast::Lang::Japanese,
"ka" => forecast::Lang::Georgian,
"kn" => forecast::Lang::Kannada,
"ko" => forecast::Lang::Korean,
"kw" => forecast::Lang::Cornish,
"lv" => forecast::Lang::Latvian,
"ml" => forecast::Lang::Malayam,
"mr" => forecast::Lang::Marathi,
"nb" => forecast::Lang::NorwegianBokmal,
"nl" => forecast::Lang::Dutch,
"no" => forecast::Lang::NorwegianBokmal,
"pa" => forecast::Lang::Punjabi,
"pl" => forecast::Lang::Polish,
"pt" => forecast::Lang::Portuguese,
"ro" => forecast::Lang::Romanian,
"ru" => forecast::Lang::Russian,
"sk" => forecast::Lang::Slovak,
"sl" => forecast::Lang::Slovenian,
"sr" => forecast::Lang::Serbian,
"sv" => forecast::Lang::Swedish,
"ta" => forecast::Lang::Tamil,
"te" => forecast::Lang::Telugu,
"tet" => forecast::Lang::Tetum,
"tr" => forecast::Lang::Turkish,
"uk" => forecast::Lang::Ukrainian,
"ur" => forecast::Lang::Urdu,
"x-pig-latin" => forecast::Lang::IgpayAtinlay,
"zh" => forecast::Lang::SimplifiedChinese,
"zh-tw" => forecast::Lang::TraditionalChinese,
lang => bail!("invalid `FORECAST_LANG`: '{}'", lang),
};

let dark_sky = dark_sky::DarkSky {
dark_sky_api_key,
location,
theme,
units,
lang,
};

dark_sky.run()
Expand Down
2 changes: 1 addition & 1 deletion src/precipitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Ord for Probability {
impl fmt::Display for Intensity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let intensity = format!("{:.3}", (self.0 * 1000.).round() / 1000.);
let mut intensity = String::from(intensity.trim_right_matches('0'));
let mut intensity = String::from(intensity.trim_end_matches('0'));
if intensity.ends_with('.') {
intensity.push('0');
}
Expand Down
3 changes: 2 additions & 1 deletion src/sparkline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl fmt::Display for Ascii {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ticks = vec!['▁', '▂', '▃', '▄', '▅', '▆', '▇'];
let ticks_len = (ticks.len() - 1) as f64;
let sparkline: String = self.data
let sparkline: String = self
.data
.iter()
.step_by(self.step)
.map(|x| {
Expand Down
3 changes: 2 additions & 1 deletion src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ impl Theme {
Icon::PartlyCloudyDay => Some("Cloud-Sun"),
Icon::PartlyCloudyNight => Some("Cloud-Moon"),
Icon::Unknown(_) => None,
}.map(|x| {
}
.map(|x| {
let theme = match *self {
Theme::Light => "Light",
Theme::Dark => "Dark",
Expand Down
8 changes: 6 additions & 2 deletions workflow/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,14 @@ fi</string>
- `DARK_SKY_API_KEY`: Get an API key [here][dark-sky-api-key].
- `GOOGLE_API_KEY`: Get an API key [here][google-api-key]. (Used for geocoding queries. *This can be omitted if you only want the forecast for the current location*.)
- `FORECAST_UNITS`: Defaults to `auto`, which sets the units based on the location. Use `si` for Celsius and `us` for Fahrenheit.
- `FORECAST_LANG`: Defaults to `en`. See [Dark Sky documentation][dark-sky-lang] for full list of language options.
- `DEFAULT_LAT_LONG`: Set this to override IP geolocation. Ex: `47.7396,-122.3426` for Seattle.
- `DEFAULT_LOCATION`: Used for displaying the location name when using `DEFAULT_LAT_LONG`.
- `LIGHT_ICONS`: `true` gives white icons, `false` gives black icons.
[dark-sky-api-key]: https://darksky.net/dev/register
[google-api-key]: https://developers.google.com/maps/documentation/geocoding/#api_key</string>
[google-api-key]: https://developers.google.com/maps/documentation/geocoding/#api_key
[dark-sky-lang]: https://darksky.net/dev/docs#forecast-request</string>
<key>uidata</key>
<dict>
<key>2A5C0A87-204E-49EA-94A7-8E62BB4EFD8A</key>
Expand Down Expand Up @@ -498,6 +500,8 @@ fi</string>
<string></string>
<key>DEFAULT_LOCATION</key>
<string></string>
<key>FORECAST_LANG</key>
<string>en</string>
<key>FORECAST_UNITS</key>
<string>auto</string>
<key>GOOGLE_API_KEY</key>
Expand All @@ -514,7 +518,7 @@ fi</string>
<string>GOOGLE_API_KEY</string>
</array>
<key>version</key>
<string>3.0.4</string>
<string>3.1.0</string>
<key>webaddress</key>
<string>http://github.com/kejadlen/dark-sky.alfredworkflow</string>
</dict>
Expand Down

0 comments on commit 36d4d14

Please sign in to comment.