Subject: | Enhancement: Localization support |
Hi,
the Google Weather API supports a couple of localizations of the
provided weather data that Weather::Google currently doesn't make use of.
A localization may be chosen by passing a "hl" argument followed by a
supported ISO language code [1] along with the GET request, so adding
this feature is quite easy.
The attached patch adds localization support based on the 0.03 codebase,
updates the documentation accordingly and introduces a couple of related
tests ("t/05_language.t"). The used language defaults to English so that
the module's behaviour and results don't change unless requested by
setting an alternate language.
I guess other users of Weather::Google might be interested in using
localized weather data as well, so it may be useful to apply the patch
to the official code.
Kind regards,
Alex
[1]
http://toolbar.google.com/buttons/apis/howto_guide.html#multiplelanguages
Subject: | Weather-Google-0.03_localization_support.patch |
diff -Naur Weather-Google-0.03.orig/lib/Weather/Google.pm Weather-Google-0.03/lib/Weather/Google.pm
--- Weather-Google-0.03.orig/lib/Weather/Google.pm 2008-05-10 18:15:20.000000000 +0200
+++ Weather-Google-0.03/lib/Weather/Google.pm 2009-08-24 19:13:58.084494304 +0200
@@ -14,14 +14,17 @@
sub new {
my $class = shift;
- my $self = {};
+ my $self = { lang => 'en' };
+ my $area = shift;
+ my $opt = shift;
bless ($self,$class);
$self->{xs} = XML::Simple->new;
# $self->{xs} = XML::Parser->new;
- return $self unless @_;
+ return $self unless $area;
+
+ $self->language($opt->{language}) if defined $opt->{language};
- my $area = shift;
if ( $area =~ /^\d{5}?$/ ) {
$self->zip($area);
return $self;
@@ -39,7 +42,7 @@
return;
}
- my $xml = get(GAPI.$zip);
+ my $xml = get(GAPI.$zip."&hl=".$self->{lang});
$xml = Encode::decode_utf8($xml,$Encode::FB_DEFAULT) if $ENCODE;
my $w = $self->{xs}->xml_in($xml) or return;
# my $w = $self->{xs}->parse($xml) or return;
@@ -54,7 +57,7 @@
# Encode the location for URL
$loc =~ s/([^\w()â*~!.-])/sprintf '%%%02x', ord $1/eg;
- my $xml = get(GAPI.$loc);
+ my $xml = get(GAPI.$loc."&hl=".$self->{lang});
if ( $ENCODE ) {
$xml = Encode::decode_utf8($xml,$Encode::FB_DEFAULT);
} else {
@@ -66,6 +69,27 @@
return 1;
}
+sub language {
+ my $self = shift;
+ my $lang = shift;
+
+ return $self->{lang} unless $lang;
+
+ # List of supported languages according to
+ # http://toolbar.google.com/buttons/apis/howto_guide.html#multiplelanguages
+ my %languages = map { $_ => 1 } qw/
+ en da de es fi fr it ja ko nl no pt-BR ru sv zh-CN zh-TW
+ /;
+
+ unless (defined $languages{$lang})
+ {
+ warn qq|"$lang" is not a supported ISO language code.\n|;
+ return $self->{lang};
+ }
+
+ $self->{lang} = $lang;
+}
+
sub current_conditions {
my $self = shift;
return $self->{current} unless @_;
@@ -226,11 +250,13 @@
## Initialize the module
$gw = new Weather::Google(90210); # Zip code
$gw = new Weather::Google('Beverly Hills, CA'); # City name
+ $gw = new Weather::Google('Herne, Germany',{language => 'de'});
# Or
$gw = new Weather::Google;
$gw->zip(90210); # Zip code
$gw->city('Beverly Hills, CA'); # City name
+ $gw->language('de'); # Localization
## Get some current information
@@ -276,7 +302,16 @@
=item new
Initializes and returns a Weather::Google object. Optionally takes a
-Zip/postal code or city name as an argument.
+Zip/postal code or city name as an argument, optionally followed by a hashref
+of additional options:
+
+=over
+
+=item language
+
+Have a look at the language() method's description below.
+
+=back
=cut
@@ -294,6 +329,19 @@
=cut
+=item language
+
+Optionally takes an ISO language code as an argument (i.e. "en", "de") to set
+the language that is passed to the weather query for proper localization.
+(Default: "en")
+
+Supported language codes: "en", "da", "de", "es", "fi", "fr", "it", "ja",
+"ko", "nl", "no", "pt-BR", "ru", "sv", "zh-CN", "zh-TW"
+
+Returns the currently set ISO language code.
+
+=cut
+
=item current_conditions
Method to report on current weather conditions. With no argument, this
diff -Naur Weather-Google-0.03.orig/MANIFEST Weather-Google-0.03/MANIFEST
--- Weather-Google-0.03.orig/MANIFEST 2008-05-10 18:15:20.000000000 +0200
+++ Weather-Google-0.03/MANIFEST 2009-08-23 11:48:52.851092228 +0200
@@ -11,5 +11,6 @@
t/02current_conditions.t
t/03forecast_conditions.t
t/04forecast_information.t
+t/05language.t
t/pod-coverage.t
t/pod.t
diff -Naur Weather-Google-0.03.orig/t/05language.t Weather-Google-0.03/t/05language.t
--- Weather-Google-0.03.orig/t/05language.t 1970-01-01 01:00:00.000000000 +0100
+++ Weather-Google-0.03/t/05language.t 2009-08-23 12:53:31.403091309 +0200
@@ -0,0 +1,30 @@
+#!/usr/bin/perl -w
+
+use strict;
+use warnings;
+
+use Test::More tests => 5;
+
+use Weather::Google;
+
+my $gw = new Weather::Google();
+
+# 1
+is($gw->language(), "en", "default language: en");
+
+# 2
+$gw->language("de");
+is($gw->language(), "de", "set language via language(): 'de'");
+
+$gw = new Weather::Google("Herne, Germany", {language => "de"});
+
+# 3
+is($gw->language(), "de", "set language via new(): 'de'");
+
+# 4
+$gw = new Weather::Google("Herne, Germany", {language => "unsupported"});
+is($gw->language(), "en", "set unsupported language via new(): not set");
+
+# 5
+$gw->language("unsupported");
+is($gw->language(), "en", "set unsupported language via language(): not set");