dongsheng@623: # Locale::Po4a::Common -- Common parts of the po4a scripts and utils dongsheng@623: # $Id: Common.pm,v 1.20 2009-02-13 23:16:44 nekral-guest Exp $ dongsheng@623: # dongsheng@623: # Copyright 2005 by Jordi Vilalta dongsheng@623: # dongsheng@623: # This program is free software; you may redistribute it and/or modify it dongsheng@623: # under the terms of GPL (see COPYING). dongsheng@623: # dongsheng@623: # This module has common utilities for the various scripts of po4a dongsheng@623: dongsheng@623: =head1 NAME dongsheng@623: dongsheng@623: Locale::Po4a::Common - Common parts of the po4a scripts and utils dongsheng@623: dongsheng@623: =head1 DESCRIPTION dongsheng@623: dongsheng@623: Locale::Po4a::Common contains common parts of the po4a scripts and some useful dongsheng@623: functions used along the other modules. dongsheng@623: dongsheng@623: In order to use Locale::Po4a programatically, one may want to disable dongsheng@623: the use of Text::WrapI18N, by writing e.g. dongsheng@623: dongsheng@623: use Locale::Po4a::Common qw(nowrapi18n); dongsheng@623: use Locale::Po4a::Text; dongsheng@623: dongsheng@623: instead of: dongsheng@623: dongsheng@623: use Locale::Po4a::Text; dongsheng@623: dongsheng@623: Ordering is important here: as most Locale::Po4a modules themselves dongsheng@623: load Locale::Po4a::Common, the first time this module is loaded dongsheng@623: determines whether Text::WrapI18N is used. dongsheng@623: dongsheng@623: =cut dongsheng@623: dongsheng@623: package Locale::Po4a::Common; dongsheng@623: dongsheng@623: require Exporter; dongsheng@623: use vars qw(@ISA @EXPORT); dongsheng@623: @ISA = qw(Exporter); dongsheng@623: @EXPORT = qw(wrap_msg wrap_mod wrap_ref_mod textdomain gettext dgettext); dongsheng@623: dongsheng@623: use 5.006; dongsheng@623: use strict; dongsheng@623: use warnings; dongsheng@623: dongsheng@623: sub import { dongsheng@623: my $class=shift; dongsheng@623: dongsheng@623: my $wrapi18n=1; dongsheng@623: if (exists $_[0] && defined $_[0] && $_[0] eq 'nowrapi18n') { dongsheng@623: shift; dongsheng@623: $wrapi18n=0; dongsheng@623: } dongsheng@623: $class->export_to_level(1, $class, @_); dongsheng@623: dongsheng@623: return if defined &wrapi18n; dongsheng@623: dongsheng@623: if ($wrapi18n && -t STDERR && -t STDOUT && eval { require Text::WrapI18N }) { dongsheng@623: dongsheng@623: # Don't bother determining the wrap column if we cannot wrap. dongsheng@623: my $col=$ENV{COLUMNS}; dongsheng@623: if (!defined $col) { dongsheng@623: my @term=eval "use Term::ReadKey; Term::ReadKey::GetTerminalSize()"; dongsheng@623: $col=$term[0] if (!$@); dongsheng@623: # If GetTerminalSize() failed we will fallback to a safe default. dongsheng@623: # This can happen if Term::ReadKey is not available dongsheng@623: # or this is a terminal-less build or such strange condition. dongsheng@623: } dongsheng@623: $col=76 if (!defined $col); dongsheng@623: dongsheng@623: eval ' use Text::WrapI18N qw($columns); dongsheng@623: $columns = $col; dongsheng@623: '; dongsheng@623: dongsheng@623: eval ' sub wrapi18n($$$) { Text::WrapI18N::wrap($_[0],$_[1],$_[2]) } ' dongsheng@623: } else { dongsheng@623: dongsheng@623: # If we cannot wrap, well, that's too bad. Survive anyway. dongsheng@623: eval ' sub wrapi18n($$$) { $_[0].$_[2] } ' dongsheng@623: } dongsheng@623: } dongsheng@623: dongsheng@623: sub min($$) { dongsheng@623: return $_[0] < $_[1] ? $_[0] : $_[1]; dongsheng@623: } dongsheng@623: dongsheng@623: =head1 FUNCTIONS dongsheng@623: dongsheng@623: =head2 Showing output messages dongsheng@623: dongsheng@623: =over dongsheng@623: dongsheng@623: =item dongsheng@623: dongsheng@623: show_version($) dongsheng@623: dongsheng@623: Shows the current version of the script, and a short copyright message. It dongsheng@623: takes the name of the script as an argument. dongsheng@623: dongsheng@623: =cut dongsheng@623: dongsheng@623: sub show_version { dongsheng@623: my $name = shift; dongsheng@623: dongsheng@623: print sprintf(gettext( dongsheng@623: "%s version %s.\n". dongsheng@623: "written by Martin Quinson and Denis Barbier.\n\n". dongsheng@623: "Copyright (C) 2002, 2003, 2004 Software of Public Interest, Inc.\n". dongsheng@623: "This is free software; see source code for copying\n". dongsheng@623: "conditions. There is NO warranty; not even for\n". dongsheng@623: "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." dongsheng@623: ), $name, $Locale::Po4a::TransTractor::VERSION)."\n"; dongsheng@623: } dongsheng@623: dongsheng@623: =item dongsheng@623: dongsheng@623: wrap_msg($@) dongsheng@623: dongsheng@623: This function displays a message the same way than sprintf() does, but wraps dongsheng@623: the result so that they look nice on the terminal. dongsheng@623: dongsheng@623: =cut dongsheng@623: dongsheng@623: sub wrap_msg($@) { dongsheng@623: my $msg = shift; dongsheng@623: my @args = @_; dongsheng@623: dongsheng@623: return wrapi18n("", "", sprintf($msg, @args))."\n"; dongsheng@623: } dongsheng@623: dongsheng@623: =item dongsheng@623: dongsheng@623: wrap_mod($$@) dongsheng@623: dongsheng@623: This function works like wrap_msg(), but it takes a module name as the first dongsheng@623: argument, and leaves a space at the left of the message. dongsheng@623: dongsheng@623: =cut dongsheng@623: dongsheng@623: sub wrap_mod($$@) { dongsheng@623: my ($mod, $msg) = (shift, shift); dongsheng@623: my @args = @_; dongsheng@623: dongsheng@623: $mod .= ": "; dongsheng@623: my $spaces = " " x min(length($mod), 15); dongsheng@623: return wrapi18n($mod, $spaces, sprintf($msg, @args))."\n"; dongsheng@623: } dongsheng@623: dongsheng@623: =item dongsheng@623: dongsheng@623: wrap_ref_mod($$$@) dongsheng@623: dongsheng@623: This function works like wrap_msg(), but it takes a file:line reference as the dongsheng@623: first argument, a module name as the second one, and leaves a space at the left dongsheng@623: of the message. dongsheng@623: dongsheng@623: =back dongsheng@623: dongsheng@623: =cut dongsheng@623: dongsheng@623: sub wrap_ref_mod($$$@) { dongsheng@623: my ($ref, $mod, $msg) = (shift, shift, shift); dongsheng@623: my @args = @_; dongsheng@623: dongsheng@623: if (!$mod) { dongsheng@623: # If we don't get a module name, show the message like wrap_mod does dongsheng@623: return wrap_mod($ref, $msg, @args); dongsheng@623: } else { dongsheng@623: $ref .= ": "; dongsheng@623: my $spaces = " " x min(length($ref), 15); dongsheng@623: $msg = "$ref($mod)\n$msg"; dongsheng@623: return wrapi18n("", $spaces, sprintf($msg, @args))."\n"; dongsheng@623: } dongsheng@623: } dongsheng@623: dongsheng@623: =head2 Wrappers for other modules dongsheng@623: dongsheng@623: =over dongsheng@623: dongsheng@623: =item dongsheng@623: dongsheng@623: Locale::Gettext dongsheng@623: dongsheng@623: When the Locale::Gettext module cannot be loaded, this module provide dummy dongsheng@623: (empty) implementation of the following functions. In that case, po4a dongsheng@623: messages won't get translated but the program will continue to work. dongsheng@623: dongsheng@623: If Locale::gettext is present, this wrapper also calls dongsheng@623: setlocale(LC_MESSAGES, "") so callers don't depend on the POSIX module dongsheng@623: either. dongsheng@623: dongsheng@623: =over dongsheng@623: dongsheng@623: =item dongsheng@623: dongsheng@623: bindtextdomain($$) dongsheng@623: dongsheng@623: =item dongsheng@623: dongsheng@623: textdomain($) dongsheng@623: dongsheng@623: =item dongsheng@623: dongsheng@623: gettext($) dongsheng@623: dongsheng@623: =item dongsheng@623: dongsheng@623: dgettext($$) dongsheng@623: dongsheng@623: =back dongsheng@623: dongsheng@623: =back dongsheng@623: dongsheng@623: =cut dongsheng@623: dongsheng@623: BEGIN { dongsheng@623: if (eval { require Locale::gettext }) { dongsheng@623: import Locale::gettext; dongsheng@623: require POSIX; dongsheng@623: POSIX::setlocale(&POSIX::LC_MESSAGES, ''); dongsheng@623: } else { dongsheng@623: eval ' dongsheng@623: sub bindtextdomain($$) { } dongsheng@623: sub textdomain($) { } dongsheng@623: sub gettext($) { shift } dongsheng@623: sub dgettext($$) { return $_[1] } dongsheng@623: ' dongsheng@623: } dongsheng@623: } dongsheng@623: dongsheng@623: 1; dongsheng@623: __END__ dongsheng@623: dongsheng@623: =head1 AUTHORS dongsheng@623: dongsheng@623: Jordi Vilalta dongsheng@623: dongsheng@623: =head1 COPYRIGHT AND LICENSE dongsheng@623: dongsheng@623: Copyright 2005 by SPI, inc. dongsheng@623: dongsheng@623: This program is free software; you may redistribute it and/or modify it dongsheng@623: under the terms of GPL (see the COPYING file). dongsheng@623: dongsheng@623: =cut