49
|
1 package IMPL::Resources;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 our $Encoding ||= 'utf-8';
|
|
6 our %Files;
|
|
7
|
|
8 my %Data;
|
|
9
|
|
10
|
|
11 foreach my $group (keys %Files) {
|
|
12 $Data{$group} = ParseResource($Files{$group});
|
|
13 }
|
|
14
|
|
15 sub findFile {
|
|
16 my ($fname) = @_;
|
|
17
|
|
18 foreach my $dir (',',@INC) {
|
|
19 my $fullfname = "$dir/$fname";
|
|
20 return $fullfname if -f $fullfname;
|
|
21 }
|
|
22
|
|
23 return $fname;
|
|
24 }
|
|
25
|
|
26 sub ParseResource {
|
|
27 my ($fname) = @_;
|
|
28
|
|
29 open my $hRes, "<:encoding($Encoding)", findFile($fname) or die "Failed to open file $fname: $!";
|
|
30
|
|
31 my %Map;
|
|
32 my $line = 1;
|
|
33 while (<$hRes>) {
|
|
34 chomp;
|
|
35 $line ++ and next if /^\s*$/;
|
|
36
|
|
37 if (/^(\w+)\s*=\s*(.*)$/) {
|
|
38 $Map{$1} = $2;
|
|
39 } else {
|
|
40 die "Invalid resource format in $fname at $line";
|
|
41 }
|
|
42 $line ++;
|
|
43 }
|
|
44
|
|
45 return \%Map;
|
|
46 }
|
|
47
|
|
48 sub import {
|
|
49 my ($class,@groups) = @_;
|
|
50 my $caller = caller;
|
|
51 my %merged = map %{$Data{$_} || {} }, @groups;
|
|
52
|
|
53 no strict 'refs';
|
|
54 foreach my $item ( keys %merged ) {
|
|
55 *{"${caller}::ids_$item"} = sub { sprintf($merged{$item},@_) }
|
|
56 }
|
|
57 }
|
|
58
|
|
59 1;
|