Commits

Toby Inkster committed db2ae3b

Add FR, PT, ES translations; document that BCP47 language codes should be used; add German language test cases; prepare for release.

Comments (0)

Files changed (3)

lib/DateTimeX/Format/Ago.pm

 
 use Carp 0 qw[];
 use DateTime 0 ;
-use Scalar::Util 0 qw[blessed refaddr];
+use Scalar::Util 0 qw[blessed];
 
 our %__;
 BEGIN {
 		hours     => ["vor %d Stunden", "vor einer Stunde"],
 		minutes   => ["vor %d Minuten", "vor einer Minute"],
 		};
+	
+	# These courtesy of Google Translate...
+	$__{FR} = {
+		future    => "à l'avenir",
+		recent    => "viens",
+		years     => ["il ya %d ans", "il ya un an"],
+		months    => ["il ya %d mois", "il ya un mois"],
+		weeks     => ["il ya %d semaines", "il ya une semaine"],
+		days      => ["il ya %d jours", "il ya un jour"],
+		hours     => ["il ya %d heures", "il ya une heure"],
+		minutes   => ["il ya %d minutes", "il ya une minute"],
+		};
+	$__{ES} = {
+		future    => "en el futuro",
+		recent    => "ahora mismo",
+		years     => ["hace %d años", "hace un año"],
+		months    => ["hace %d meses", "hace un mes"],
+		weeks     => ["hace %d semanas", "hace una semana"],
+		days      => ["hace %d días", "hace un día"],
+		hours     => ["hace %d horas", "hace una hora"],
+		minutes   => ["hace %d minutos", "hace un minuto"],
+		};
+	$__{PT} = {
+		future    => "no futuro",
+		recent    => "só agora",
+		years     => ["%d anos atrás", "há um ano"],
+		months    => ["%d meses atrás", "há um mês"],
+		weeks     => ["%d semanas atrás", "há uma semana"],
+		days      => ["%d days atrás", "há um dia"],
+		hours     => ["%d horas atrás", "há uma hora"],
+		minutes   => ["%d minutos atrás", "há um minuto"],
+		};
+
+	# ISO 639-2
+	$__{ENG} = $__{EN};
+	$__{FRE} = $__{FR};
+	$__{FRA} = $__{FR};
+	$__{ENG} = $__{EN};
+	$__{GER} = $__{DE};
+	$__{DEU} = $__{DE};
+	$__{SPA} = $__{ES};
+	$__{POR} = $__{PT};
 }
 
 sub new
 		$language =~ s/(^|[_-])([^_-]*)$//;
 	}
 	
-	Carp::croak(sprintf("%s doesn't know about language %s", __PACKAGE__, $self->{language}));
+	Carp::croak(sprintf("%s doesn't know about language '%s'", __PACKAGE__, $self->{language}));
 }
 
 TRUE;
 
 =item C<< new(language => $lang) >>
 
-Creates a formatter object. If the language is ommitted, extracts it from
-C<< $ENV{LANG} >>.
+Creates a formatter object for the given language (a BCP47 language code).
+If the language is omitted, extracts it from C<< $ENV{LANG} >>.
+
+Decent English ('en') and German ('de') support is provided. French ('fr'),
+Portuguese ('pt') and Castillian Spanish ('es') are also provided, but some
+of the strings were translated with Google Translate, so they might not be
+perfect.
 
 =back
 
 
 =item C<< format_datetime($dt) >>
 
-Returns something like "3 days ago" or "just now".
+Returns something like "3 days ago", "just now" or "hace un año".
 
 =item C<< parse_datetime($string) >>
 
 Please report any bugs to
 L<http://rt.cpan.org/Dist/Display.html?Queue=DateTimeX-Format-Ago>.
 
-I'm actively seeking translations - only have English and German so far.
-Feel free to attach patches for other languages as bug reports.
+I'm actively seeking translations. Feel free to attach patches for other
+languages as bug reports.
 
 =head1 SEE ALSO
 
 L<DateTime>, L<DateTime::Format::Natural>.
 
+L<http://www.rfc-editor.org/rfc/bcp/bcp47.txt>.
+
 =head1 AUTHOR
 
 Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
 	:version_from _:main ;
 	:readme_from _:main ;
 	:test_requires "Test::More 0.61" ;
-	:requires "Scalar::Util" , "common::sense" .
+	:requires "Scalar::Util" , "common::sense" , "DateTime" , "Carp" .
 
 _:main <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName> "lib/DateTimeX/Format/Ago.pm" .
 
+use strict;
+use DateTimeX::Format::Ago;
+use Test::More tests => 200;
+
+# Some of these tests rely on computation being carried out reasonably fast.
+# I can only see them failing on really slow and overloaded CPUs though.
+
+my $ago = DateTimeX::Format::Ago->new(language => 'DE');
+
+foreach my $unit (qw/years months weeks days hours minutes/)
+{
+	my $max = {
+		years   => 25,
+		months  => 11,
+		weeks   => 3,  # don't want to fail tests in February 2013.
+		days    => 6,  
+		hours   => 22, # don't want to fail due to daylight savings.
+		minutes => 59,
+		}->{$unit};
+
+	my $when = DateTime->now->subtract($unit => 1);
+	is($ago->format_datetime($when), {
+		'years'    => 'vor einem Jahr',
+		'months'   => 'vor einem Monat',
+		'weeks'    => 'vor einer Woche',
+		'days'     => 'vor einem Tag',
+		'hours'    => 'vor einer Stunde',
+		'minutes'  => 'vor einer Minute',
+		}->{$unit});
+	my $deunit = {
+		years    => 'Jahren',
+		months   => 'Monaten',
+		weeks    => 'Wochen',
+		days     => 'Tagen',
+		hours    => 'Stunden',
+		minutes  => 'Minuten',
+		}->{$unit};
+	for my $n (2..$max)
+	{
+		my $when = DateTime->now->subtract($unit => $n);
+		is($ago->format_datetime($when), "vor $n $deunit");
+	}
+}
+
+for my $n (1..58)
+{
+	my $when = DateTime->now->subtract(seconds => $n);
+	is($ago->format_datetime($when), "gerade jetzt");
+}
+
+for my $n (62..70)
+{
+	my $when = DateTime->now->subtract(seconds => $n);
+	is($ago->format_datetime($when), "vor einer Minute");
+}
+
+for my $unit (qw/seconds minutes hours days weeks months years/)
+{
+	my $when = DateTime->now->add($unit => 3);
+	is($ago->format_datetime($when), "in der Zukunft");
+}