3 # $Id: YaPI.pm.in 33405 2006-10-13 13:12:42Z mvidner $
9 YaPI - common functions for modules implementing YaST API
14 # substituted by configure
15 my $modules = '@yast2dir4perl@/modules';
16 # unconditional 'use lib' could override a "use lib ." that
17 # we do during compilation, #197099
18 grep { $_ eq $modules } @INC or unshift(@INC, $modules);
23 our @ISA = qw(Exporter);
24 our @EXPORT = qw(textdomain __);
29 use Locale::gettext ("!textdomain");
30 use POSIX (); # Needed for setlocale()
32 POSIX::setlocale(&POSIX::LC_MESSAGES, "");
37 our @CAPABILITIES = ();
41 These are to be used by modules that use YaPI as their base class.
48 Returns a reference to a list of hashes describing the functions
49 in the current package. The information is taken from TYPEINFO.
53 functionName => "contains",
55 argument => [ "string", ["list", "string"]],
62 BEGIN { $TYPEINFO{Interface} = ["function", "any"]; }
68 my %TI = %{"${self}::TYPEINFO"};
70 foreach my $k (keys %TYPEINFO) {
71 $TI{$k} = $TYPEINFO{$k};
74 foreach my $funcName (sort keys %TI) {
75 my @dummy = @{$TI{$funcName}};
78 $hash->{'functionName'} = $funcName;
79 $hash->{'return'} = $dummy[1];
81 $hash->{'argument'} = \@dummy;
89 Returns the version of the current package.
93 BEGIN { $TYPEINFO{Version} = ["function", "string"]; }
97 return ${"${self}::VERSION"};
102 Greps C<@CAPABILITIES> of the current package.
104 if (YaPI::Foo->Supports ("frobnicate")) {...}
108 BEGIN { $TYPEINFO{Supports} = ["function", "boolean", "string"]; }
114 my @c = @{"${self}::CAPABILITIES"};
115 foreach my $k (@CAPABILITIES) {
119 return !!grep( ($_ eq $cap), @c);
125 Logs an error and remembers it for L</Error>.
130 code # mandatory, an uppercase short string
133 # if all of the following are missing, caller () is used
141 BEGIN { $TYPEINFO{SetError} = ["function", "boolean", ["map", "string", "any" ]]; }
145 if( !$__error{package} && !$__error{file} && !$__error{line})
147 @__error{'package','file','line'} = caller();
149 if ( defined $__error{summary} ) {
150 y2error($__error{code}."[".$__error{line}.":".$__error{file}."] ".$__error{summary});
152 y2error($__error{code});
154 if(defined $__error{description} && $__error{description} ne "") {
155 y2error("Description: ".$__error{description});
163 Returns the error set by L</SetError>
167 BEGIN { $TYPEINFO{Error} = ["function", ["map", "string", "any"] ]; }
177 C<< textdomain "mydomain"; >>
179 Just use a double underscore to mark text to be translated: C<__("my text")>.
180 Both C<textdomain> and C<__> are exported to the calling package.
182 These must not be used any longer because they collide with symbols
183 exported by this module:
185 # use Locale::gettext; # textdomain
188 These don't hurt but aren't necessary:
191 # POSIX::setlocale(LC_MESSAGES, ""); # YaPI calls it itself now
195 Calls Locale::gettext::textdomain
197 remembers an association between the calling package and the
198 domain. Later calls of __ use this domain as an argument to dgettext.
202 # See also bug 38613 where untranslated texts were seen because
203 # a random textdomain was used instead of the proper one.
209 my ($package, $filename, $line) = caller;
211 if (defined ($textdomains{package}))
213 if ($textdomains{package} ne $domain)
215 y2error ("Textdomain '$domain' overrides old textdomain '$textdomains{package}' in package $package, $filename:$line");
218 $textdomains{$package} = $domain;
219 return Locale::gettext::textdomain ($domain);
222 =head3 __ (double underscore)
224 Calls Locale::gettext::dgettext, supplying the textdomain of the calling
225 package (set by calling textdomain).
227 Note: the single underscore function (_) will be removed because it
228 is automaticaly exported to main:: which causes namespace conflicts.
232 # bug 39954: __ better than _
235 my $package = caller;
236 my $domain = $textdomains{$package};
237 return Locale::gettext::dgettext ($domain, $msgid);
240 # Compatibility by partial typeglob assignment:
241 # &_ will call &__ but $_ will not be $__ which would happen
242 # if we just asigned *_ = *__.
243 # Cannot just call __ from _ because of "caller".