| 1 | package Locale::Maketext::From::Strings; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | Locale::Maketext::From::Strings - Parse Apple .strings files | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 0.03 | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | use Locale::Maketext::From::Strings; | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | my $strings = Locale::Maketext::From::Strings->new( | ||||||||||||||||||||||||||||||||
| 16 | path => '/path/to/strings', | ||||||||||||||||||||||||||||||||
| 17 | namespace => 'MyApp::I18N', | ||||||||||||||||||||||||||||||||
| 18 | out_dir => 'lib', | ||||||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | $strings->load; # in memory | ||||||||||||||||||||||||||||||||
| 22 | $strings->generate; # to disk | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | This module will parse C<.strings> file used in the Apple world and generate | ||||||||||||||||||||||||||||||||
| 27 | in memory perl-packages used by the L<Locale::Maketext> module. | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | =head2 Formatting rules | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | This module can parse most of the formatting mentioned here: | ||||||||||||||||||||||||||||||||
| 32 | L<http://blog.lingohub.com/developers/2013/03/i18n-resource-file-formats-ios-strings-files/>. | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | =over 4 | ||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | =item * | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | Key-value pairs are delimited with the equal character (=), and terminated by | ||||||||||||||||||||||||||||||||
| 39 | a semicolon (;). | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | =item * | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | Keys and values are surrounded by double quotes ("). | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | =item * | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | Place-holders look can be: %.2f, %d, %1$s: | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | qr{\%[\d|\.]*\$*\d*[dsf]\b} | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | =item * | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | Comments start at the beginning of the line and span the whole line. | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | =item * | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | Multi-line comments are enclosed in /* */. | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | =item * | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | Single-line comments start with double slashes (//). | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | =item * | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | The specification says it expect UTF-16LE encoding by default, but this | ||||||||||||||||||||||||||||||||
| 66 | module expect UTF-8 instead. | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | NOTE! This might change in future release. Pass L</encoding> to constructor | ||||||||||||||||||||||||||||||||
| 69 | if you want to be sure about the value. | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | =back | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =head2 Example file | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | This could be the content of "i18n/en.strings": | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | /* comments in .strings files | ||||||||||||||||||||||||||||||||
| 78 | can be multi line, | ||||||||||||||||||||||||||||||||
| 79 | single line */ | ||||||||||||||||||||||||||||||||
| 80 | // or combination of the two | ||||||||||||||||||||||||||||||||
| 81 | "hello_user" = "Hello %1$s"; | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | "Sample data" = "sample %s %d %.3f data"; | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | // keys and values can be spread to multiple lines | ||||||||||||||||||||||||||||||||
| 86 | "welcome_message" = "Welcome back, | ||||||||||||||||||||||||||||||||
| 87 | we have missed you"; | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | TIP! Adding the default value on the left side (instead of hello_user and | ||||||||||||||||||||||||||||||||
| 90 | welcome_message) works better with L<Locale::Maketext> since it will use that | ||||||||||||||||||||||||||||||||
| 91 | as fallback if translation is missing. | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | use File::Spec::Functions qw( catfile splitdir ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | use Data::Dumper (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 99 | 4 | ▶ | use constant DEBUG => $ENV{MAKETEXT_FROM_STRINGS_DEBUG} ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 4 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | =head2 encoding | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | Holds the encoding used when reading the C<.strings> files. Defaults to | ||||||||||||||||||||||||||||||||
| 108 | "UTF-8". | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub encoding { shift->{encoding} ||= 'UTF-8' } | ||||||||||||||||||||||||||||||
|
Subroutine
encoding: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
+(shift())->{"encoding"} ||= "UTF-8"
MC/DC0%0/1
+(shift())->{"encoding"} ||= "UTF-8"
+(shift())->{"encoding"}
Truth table0%0/2
A
A= +(shift())->{"encoding"}
| |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | =head2 namespace | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | Package name of where to L</generate> or L</load> code into. Default to the | ||||||||||||||||||||||||||||||||
| 117 | caller namespace. | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 13 | ▶ | sub namespace { | ||||||||||||||||||||||||||||||
|
Subroutine
namespace: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 122 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||
| 123 | 13 | ▶ | $self->{namespace} ||= do { | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"namespace"} ||= do {
my $caller = (caller 0)[0];
$caller = caller 1 if $caller->isa(__PACKAGE__);
$caller .= '::I18N'
}
MC/DC0%0/2
$self->{"namespace"} ||= do {
my $caller = (caller 0)[0];
$caller = caller 1 if $caller->isa(__PACKAGE__);
$caller .= '::I18N'
}
$self->{"namespace"}
do {
my $caller = (caller 0)[0];
$caller = caller 1 if $caller->isa(__PACKAGE__);
$caller .= '::I18N'
}
Truth table66%2/3
A ||= B
A= $self->{"namespace"}B= do {
my $caller = (caller 0)[0];
$caller = caller 1 if $caller->isa(__PACKAGE__);
$caller .= '::I18N'
}
| |||||||||||||||||||||||||||||||||
| 124 | 2 | my $caller = (caller 0)[0]; | |||||||||||||||||||||||||||||||
| 125 | 2 | ▶ | $caller = (caller 1) if $caller->isa(__PACKAGE__); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 2 | $caller .= '::I18N'; | |||||||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | =head2 out_dir | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | Directory to where files should be written to. Defaults to "lib". | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | 2 | ▶ | sub out_dir { shift->{out_dir} ||= 'lib' } | ||||||||||||||||||||||||||||||
|
Subroutine
out_dir: called CC=2 SCAR=7.6
Pod
documented
Condition50%1/2
+(shift())->{"out_dir"} ||= "lib"
MC/DC0%0/1
+(shift())->{"out_dir"} ||= "lib"
+(shift())->{"out_dir"}
Truth table50%1/2
A
A= +(shift())->{"out_dir"}
| |||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | =head2 path | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | Path to ".strings" files. Defaults to "i18n". | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 4 | ▶ | sub path { shift->{path} ||= 'i18n' } | ||||||||||||||||||||||||||||||
|
Subroutine
path: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
+(shift())->{"path"} ||= "i18n"
MC/DC100%1/1
+(shift())->{"path"} ||= "i18n"
+(shift())->{"path"}
Truth table100%2/2
A
A= +(shift())->{"path"}
| |||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 4 | ▶ | sub _namespace_dir { | ||||||||||||||||||||||||||||||
|
Subroutine
_namespace_dir: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 147 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||
| 148 | 4 | ▶ | $self->{_namespace_dir} ||= do { | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"_namespace_dir"} ||= do {
my $dir = $self->namespace;
$dir =~ s[::][/]g;
$dir
}
MC/DC0%0/2
$self->{"_namespace_dir"} ||= do {
my $dir = $self->namespace;
$dir =~ s[::][/]g;
$dir
}
$self->{"_namespace_dir"}
do {
my $dir = $self->namespace;
$dir =~ s[::][/]g;
$dir
}
Truth table66%2/3
A ||= B
A= $self->{"_namespace_dir"}B= do {
my $dir = $self->namespace;
$dir =~ s[::][/]g;
$dir
}
| |||||||||||||||||||||||||||||||||
| 149 | 3 | my $dir = $self->namespace; | |||||||||||||||||||||||||||||||
| 150 | 3 | $dir =~ s!::!/!g; | |||||||||||||||||||||||||||||||
| 151 | 3 | $dir; | |||||||||||||||||||||||||||||||
| 152 | }; | ||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | =head2 new | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | $self = Locale::Maketext::From::Strings->new(%attributes); | ||||||||||||||||||||||||||||||||
| 160 | $self = Locale::Maketext::From::Strings->new($attributes); | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | Object constructor. | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 167 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | =head2 generate | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | Locale::Maketext::From::Strings->generate($namespace); | ||||||||||||||||||||||||||||||||
| 174 | $self->generate; | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | This method will write the I18N code to disk. Use this when the L</load> time | ||||||||||||||||||||||||||||||||
| 177 | goes up. | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | NOTE! This method does not check for existing files - they will be overwritte | ||||||||||||||||||||||||||||||||
| 180 | without warning. | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | Example one-liners: | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | $ perl -MLocale::Maketext::From::Strings=generate -e1 MyApp::I18N | ||||||||||||||||||||||||||||||||
| 185 | $ perl -Ilib -E'say +(require MyApp::I18N)->get_handle(shift)->maketext(@ARGV);' en "some key" ... | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | 1 | ▶ | sub generate { | ||||||||||||||||||||||||||||||
|
Subroutine
generate: called CC=9 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 190 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||
| 191 | 1 | my($code, $namespace_dir, $path); | |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 1 | ▶ | unless(ref $self) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | $self = bless @_ ? @_ > 1 ? {@_} : !ref $_[0] ? { namespace => shift } : {%{$_[0]}} : {}, $self; | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | 1 | $path = $self->path; | |||||||||||||||||||||||||||||||
| 198 | 1 | $namespace_dir = catfile $self->out_dir, $self->_namespace_dir; | |||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 1 | _mkdir($namespace_dir); | |||||||||||||||||||||||||||||||
| 201 | 1 | ▶ | _spurt($self->_namespace_code, $namespace_dir .'.pm') unless -s $namespace_dir .'.pm'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 202 | 1 | ▶ | opendir(my $DH, $path) or die "opendir $path: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 1 | for my $file (grep { /\.strings$/ } readdir $DH) { | |||||||||||||||||||||||||||||||
| 205 | 1 | my $language = $file; | |||||||||||||||||||||||||||||||
| 206 | 1 | my($code, $kv); | |||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 1 | ▶ | $language =~ s/\.strings$// or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 209 | 1 | $code = $self->_package_code($language); | |||||||||||||||||||||||||||||||
| 210 | 1 | $kv = $self->parse(catfile $path, $file); | |||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 1 | local $Data::Dumper::Indent = 1; | |||||||||||||||||||||||||||||||
| 213 | 1 | local $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||||||
| 214 | 1 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||||||
| 215 | 1 | $kv = Data::Dumper::Dumper($kv); | |||||||||||||||||||||||||||||||
| 216 | 1 | $kv =~ s!^\{!our %Lexicon = (!; | |||||||||||||||||||||||||||||||
| 217 | 1 | $kv =~ s!\}$!);!; | |||||||||||||||||||||||||||||||
| 218 | 1 | substr $code, -3, -3, $kv; | |||||||||||||||||||||||||||||||
| 219 | 1 | _spurt($code, catfile $namespace_dir, "$language.pm"); | |||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | =head2 load | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | Locale::Maketext::From::Strings->load($path); | ||||||||||||||||||||||||||||||||
| 228 | $self->load; | ||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||
| 230 | Will parse C<language.strings> files from L</path> and generage in-memory | ||||||||||||||||||||||||||||||||
| 231 | packages in the given L</namespace>. | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | Example L<Mojolicious> app: | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | package MyApp; | ||||||||||||||||||||||||||||||||
| 236 | use Locale::Maketext::From::Strings; | ||||||||||||||||||||||||||||||||
| 237 | use base 'Mojolicious'; | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | sub startup { | ||||||||||||||||||||||||||||||||
| 240 | my $self = sihft; | ||||||||||||||||||||||||||||||||
| 241 | my $default_lang = 'en'; | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | Locale::Maketext::From::Strings->load($self->home->rel_dir('i18n')); | ||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | $self->helper(l => sub { | ||||||||||||||||||||||||||||||||
| 246 | my $c = shift; | ||||||||||||||||||||||||||||||||
| 247 | $c->stash->{i18n} ||= MyApp::I18N->get_handle($c->session('lang'), $default_lang); | ||||||||||||||||||||||||||||||||
| 248 | $c->stash->{i18n}->maketext(@_); | ||||||||||||||||||||||||||||||||
| 249 | }); | ||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | See also L<Mojolicious::Plugin::I18N>. | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | =cut | ||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||
| 256 | 2 | ▶ | sub load { | ||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=10 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 257 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 258 | 2 | my($namespace, $namespace_dir, $path); | |||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | 2 | ▶ | unless(ref $self) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | $self = bless @_ ? @_ > 1 ? {@_} : !ref $_[0] ? { path => shift } : {%{$_[0]}} : {}, $self; | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | 2 | $namespace = $self->namespace; | |||||||||||||||||||||||||||||||
| 265 | 2 | $namespace_dir = $self->_namespace_dir; | |||||||||||||||||||||||||||||||
| 266 | 2 | $path = $self->path; | |||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | 2 | ▶ | eval $self->_namespace_code or die $@; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 269 | 2 | $INC{"$namespace_dir.pm"} = 'GENERATED'; | |||||||||||||||||||||||||||||||
| 270 | 2 | ▶ | opendir(my $DH, $path) or die "opendir $path: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | 2 | for my $file (grep { /\.strings$/ } readdir $DH) { | |||||||||||||||||||||||||||||||
| 273 | 2 | my $language = $file; | |||||||||||||||||||||||||||||||
| 274 | 2 | ▶ | $language =~ s/\.strings$// or next; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | 2 | ▶ | eval $self->_package_code($language) or die $@; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 277 | 2 | $self->parse(catfile($path, $file), eval "\\%$namespace\::$language\::Lexicon"); | |||||||||||||||||||||||||||||||
| 278 | 2 | $INC{"$namespace_dir/$language.pm"} = 'GENERATED'; | |||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | =head2 parse | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | $data = $self->parse($file); | ||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | Will parse C<$file> and store the key value pairs in C<$data>. | ||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | =cut | ||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||
| 292 | 4 | ▶ | sub parse { | ||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 293 | 4 | my($self, $file, $data) = @_; | |||||||||||||||||||||||||||||||
| 294 | 4 | ▶ | my $encoding = $self->{encoding} || 'UTF-8'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'encoding'} || 'UTF-8'
MC/DC0%0/1
$self->{'encoding'} || 'UTF-8'
$self->{'encoding'}
Truth table50%1/2
A
A= $self->{'encoding'}
| |||||||||||||||||||||||||||||||||
| 295 | 4 | my $buf = ''; | |||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | 4 | ▶ | $data ||= {}; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$data ||= {}
MC/DC100%1/1
$data ||= {}
$data
Truth table100%2/2
A
A= $data
| |||||||||||||||||||||||||||||||||
| 298 | 4 | ▶ | open my $FH, "<:encoding($encoding)", $file or die "read $file: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||
| 300 | 4 | ▶ | while(<$FH>) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 301 | 72 | $buf .= $_; | |||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | 72 | ▶ | if($buf =~ s!"([^"]+)"\s*=\s*"([^"]+)(");!!s) { # key-value | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 304 | 16 | my($key, $value) = ($1, $2); | |||||||||||||||||||||||||||||||
| 305 | 16 | warn "[$file] ($key) => ($value)\n" if DEBUG; | |||||||||||||||||||||||||||||||
| 306 | 16 | my $pos = 0; | |||||||||||||||||||||||||||||||
| 307 | 16 | $data->{$key} = $value; | |||||||||||||||||||||||||||||||
| 308 | 16 | ▶ | $data->{$key} =~ s/\%(\d*)\$?([\d\.]*[dsf])\b/{ ++$pos; sprintf '[sprintf,%%%s,_%s]', $2, $1 || $pos }/ge; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$1 || $pos
MC/DC0%0/2
$1 || $pos
$1
$pos
Truth table66%2/3
A || B
A= $1B= $pos
| |||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||
| 310 | elsif($buf =~ s!^//(.*)$!!m) { # comment | ||||||||||||||||||||||||||||||||
| 311 | 20 | warn "[$file] COMMENT ($1)\n" if DEBUG; | |||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||
| 313 | elsif($buf =~ s!/\*(.*)\*/!!s) { # multi-line comment | ||||||||||||||||||||||||||||||||
| 314 | 4 | warn "[$file] MULTI-LINE-COMMENT ($1)\n" if DEBUG; | |||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||
| 318 | 4 | return $data; | |||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||
| 321 | =head2 import | ||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | See L</generate> for example one-liner. | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | =cut | ||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | 4 | ▶ | sub import { | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||
| 328 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||
| 330 | 4 | ▶ | if(@_ and $_[0] eq 'generate') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and $_[0] eq "generate"
MC/DC0%0/2
@_ and $_[0] eq "generate"
@_
$_[0] eq "generate"
Truth table33%1/3
A and B
A= @_B= $_[0] eq "generate"
| |||||||||||||||||||||||||||||||||
| 331 | 0 | $class->generate(@ARGV); | |||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | 1 | ▶ | sub _mkdir { | ||||||||||||||||||||||||||||||
|
Subroutine
_mkdir: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 336 | 1 | my @path = splitdir shift; | |||||||||||||||||||||||||||||||
| 337 | 1 | my @current_path; | |||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | 1 | for my $part (@path) { | |||||||||||||||||||||||||||||||
| 340 | 4 | push @current_path, $part; | |||||||||||||||||||||||||||||||
| 341 | 4 | my $dir = catfile @current_path; | |||||||||||||||||||||||||||||||
| 342 | 4 | ▶ | next if -d $dir; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 343 | 3 | ▶ | mkdir $dir or die "mkdir $dir: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||
| 347 | 3 | ▶ | sub _namespace_code { | ||||||||||||||||||||||||||||||
|
Subroutine
_namespace_code: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||||||
| 348 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||
| 349 | 3 | my $namespace = $self->namespace; | |||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | 3 | ▶ | if(eval "require $namespace; 1") { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 352 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||
| 355 | return <<" PACKAGE" | ||||||||||||||||||||||||||||||||
| 356 | package $namespace; | ||||||||||||||||||||||||||||||||
| 357 | use base 'Locale::Maketext'; | ||||||||||||||||||||||||||||||||
| 358 | our \%Lexicon = ( _AUTO => 1 ); | ||||||||||||||||||||||||||||||||
| 359 | our \%LANGUAGES = (); # key = language name, value = class name | ||||||||||||||||||||||||||||||||
| 360 | "$namespace"; | ||||||||||||||||||||||||||||||||
| 361 | PACKAGE | ||||||||||||||||||||||||||||||||
| 362 | 3 | } | |||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||
| 364 | 3 | ▶ | sub _package_code { | ||||||||||||||||||||||||||||||
|
Subroutine
_package_code: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 365 | 3 | my($self, $language) = @_; | |||||||||||||||||||||||||||||||
| 366 | 3 | my $namespace = $self->namespace; | |||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | 3 | return <<" PACKAGE"; | |||||||||||||||||||||||||||||||
| 369 | \$${namespace}::LANGUAGES{$language} = "$namespace\::$language"; | ||||||||||||||||||||||||||||||||
| 370 | package $namespace\::$language; | ||||||||||||||||||||||||||||||||
| 371 | use base '$namespace'; | ||||||||||||||||||||||||||||||||
| 372 | 1; | ||||||||||||||||||||||||||||||||
| 373 | PACKAGE | ||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||
| 376 | 2 | ▶ | sub _spurt { | ||||||||||||||||||||||||||||||
|
Subroutine
_spurt: called CC=3 SCAR=11.7
| |||||||||||||||||||||||||||||||||
| 377 | 2 | my($content, $path) = @_; | |||||||||||||||||||||||||||||||
| 378 | 2 | ▶ | die qq{Can't open file "$path": $!} unless open my $FH, '>', $path; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 379 | 2 | ▶ | die qq{Can't write to file "$path": $!} unless defined syswrite $FH, $content; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 385 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||
| 387 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||
| 389 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | =cut | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | 4 | 1; | |||||||||||||||||||||||||||||||