Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3076 from duckduckgo/mrshu/forecast-lang
Browse files Browse the repository at this point in the history
Forecast: Prepare for translated forecasts
  • Loading branch information
moollaza authored Dec 20, 2016
2 parents a5e92a0 + 2b9d356 commit c53d92e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lib/DDG/Spice/Forecast.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Text::Trim;
triggers start => "weather", "forecast", "weather forecast";

spice from => '([^/]*)/?([^/]*)';
spice to => 'https://darksky.net/ddg?apikey={{ENV{DDG_SPICE_FORECAST_APIKEY}}}&q=$1&callback={{callback}}';
spice to => 'https://darksky.net/ddg?apikey={{ENV{DDG_SPICE_FORECAST_APIKEY}}}&q=$1&callback={{callback}}&lang=$2';

# cache DDG Rewrite for 24 hours and
# API responses with return code 200 for 30 minutes
Expand All @@ -22,7 +22,15 @@ handle remainder => sub {
return if $_;

my $loc_str = join " ", map { $loc->{$_} } @locs;
return $loc_str, 'current', {is_cached => 0};

# Default to English
my $parsed_lang = 'en';
# And try to extract language from locale
if ($lang && $lang->locale) {
($parsed_lang) = $lang->locale =~ /_([A-Z]{2})$/;
$parsed_lang = lc $parsed_lang;
}
return $loc_str, $parsed_lang, {is_cached => 0};
};

# 2015.03.17 tommytommytommy:
Expand Down
8 changes: 5 additions & 3 deletions t/Forecast.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use warnings;
use Test::More;
use URI::Escape;
use DDG::Test::Location;
use DDG::Test::Language;
use DDG::Test::Spice;
use DDG::Request;

Expand All @@ -14,8 +15,9 @@ ddg_spice_test(
DDG::Request->new(
query_raw => 'weather',
location => test_location('de'),
language => test_language('de'),
) => test_spice(
"/js/spice/forecast/M%C3%B6nchengladbach%20Nordrhein-Westfalen%20Germany/current",
"/js/spice/forecast/M%C3%B6nchengladbach%20Nordrhein-Westfalen%20Germany/de",
call_type => 'include',
caller => 'DDG::Spice::Forecast',
is_cached => 0
Expand All @@ -25,7 +27,7 @@ ddg_spice_test(
query_raw => 'weather',
location => test_location('us'),
) => test_spice(
"/js/spice/forecast/Phoenixville%20Pennsylvania%20United%20States/current",
"/js/spice/forecast/Phoenixville%20Pennsylvania%20United%20States/en",
call_type => 'include',
caller => 'DDG::Spice::Forecast',
is_cached => 0
Expand All @@ -35,7 +37,7 @@ ddg_spice_test(
query_raw => 'weather',
location => test_location('my'),
) => test_spice(
"/js/spice/forecast/Kuala%20Lumpur%20Kuala%20Lumpur%20Malaysia/current",
"/js/spice/forecast/Kuala%20Lumpur%20Kuala%20Lumpur%20Malaysia/en",
call_type => 'include',
caller => 'DDG::Spice::Forecast',
is_cached => 0
Expand Down

0 comments on commit c53d92e

Please sign in to comment.