Commits

Toby Inkster committed 6740351

document "lang" argument, and provide a shortcut role

  • Participants
  • Parent commits 6152724

Comments (0)

Files changed (2)

 Setting the argument C<no_wrap> to true can be used to I<hint> that line
 wrapping should be avoided.
 
+The C<lang> argument can be used to indicate the language of the C<text> as
+an ISO 639-1 code (e.g. "en" for English). Not all objects implementing the
+Ask API will pay attention to this hint, so don't be too surprised to see
+text in French with an English "OK" button underneath!
+
 =item C<< warning(text => $text, %arguments) >>
 
 Display a warning to the user.
 The C<default> argument can be used to supply a default return value if the
 user cannot be asked for some reason (e.g. running on an unattended terminal).
 
+The C<lang> argument can be used to indicate the language of the C<text> as
+an ISO 639-1 code (e.g. "en" for English).
+
 =item C<< question(text => $text, %arguments) >>
 
 Ask the user to answer an affirmative/negative question (i.e. OK/cancel,
 The C<default> argument can be used to supply a default return value if the
 user cannot be asked for some reason (e.g. running on an unattended terminal).
 
+The C<lang> argument can be used to indicate the language of the C<text> as
+an ISO 639-1 code (e.g. "en" for English).
+
 =item C<< file_selection(%arguments) >>
 
 Ask the user for a file name. Returns the file name. No checks are made to
 The C<default> argument can be used to supply a default return value if the
 user cannot be asked for some reason (e.g. running on an unattended terminal).
 
+The C<lang> argument can be used to indicate the language of the C<text> and
+labels as an ISO 639-1 code (e.g. "en" for English).
+
 =item C<< multiple_choice(text => $text, choices => \@choices) >>
 
 Asks the user to select zero or more options from many choices.
 user cannot be asked for some reason (e.g. running on an unattended terminal).
 It must be an arrayref.
 
+The C<lang> argument can be used to indicate the language of the C<text> and
+labels as an ISO 639-1 code (e.g. "en" for English).
+
 =back
 
 If you wish to create your own implementation of the Ask API, please
       info     => { -as => 'notify' },
    ;
 
+=head2 I18n
+
+It is strongly recommended that you pass a C<lang> argument with each method
+call. Not all backends yet pay attention to it.
+
+See also L<AskX::AutoLang> as a way to avoid passing C<< lang => "fu" >> to
+every single method call!
+
 =head1 ENVIRONMENT
 
 The C<PERL_ASK_BACKEND> environment variable can be used to influence the

lib/AskX/AutoLang.pm

+package AskX::AutoLang;
+
+use Moo::Role;
+
+my @methods = qw(
+	info warning error entry question file_selection
+	single_choice multiple_choice
+);
+
+has language => (is => 'ro', required => 1);
+
+for my $method (@methods)
+{
+	around $method => sub {
+		my $orig = shift;
+		my $self = shift;
+		$self->$orig(lang => $self->language, @_);
+	};
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+AskX::AutoLang - automatically supply a "lang" argument to all method calls
+
+=head1 SYNPOSIS
+
+   my $ask = Ask->detect(traits => ['AskX::AutoLang'], lang => "fr");
+	$ask->question("Voulez-vous coucher avec moi ce soir?");
+
+=head1 DESCRIPTION
+
+Saves supplying C<< lang => "fu" >> to all method calls. Just do it once in
+the constructor.
+
+=head1 BUGS
+
+Please report any bugs to
+L<http://rt.cpan.org/Dist/Display.html?Queue=Ask>.
+
+=head1 SEE ALSO
+
+L<Ask>.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+This software is copyright (c) 2012-2013 by Toby Inkster.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=head1 DISCLAIMER OF WARRANTIES
+
+THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+