View | Details | Raw Unified | Return to bug 26547
Collapse All | Expand All

(-)a/misc/translator/LangInstaller.pm (-24 / +26 lines)
Lines 150-182 sub po_filename { Link Here
150
}
150
}
151
151
152
sub get_trans_text {
152
sub get_trans_text {
153
    my ( $self, $msgid, $default ) = @_;
153
    my ( $self, $msgctxt, $msgid ) = @_;
154
154
155
    my $po = $self->{po}->{ Locale::PO->quote($msgid) };
155
    my $key = ($msgctxt // '') . ";$msgid";
156
157
    my $po = $self->{po}->{$key};
156
    if ( $po and not defined( $po->fuzzy() ) ) {
158
    if ( $po and not defined( $po->fuzzy() ) ) {
157
        my $msgstr = Locale::PO->dequote( $po->msgstr );
159
        my $msgstr = Locale::PO->dequote( $po->msgstr );
158
        if ( $msgstr and length($msgstr) > 0 ) {
160
159
            return $msgstr;
161
        return $msgstr || $msgid;
160
        }
161
    }
162
    }
162
163
163
    return $default;
164
    return $msgid;
164
}
165
}
165
166
166
sub get_translated_tab_content {
167
sub get_translated_tab_content {
167
    my ( $self, $file, $tab_content ) = @_;
168
    my ( $self, $tab, $tab_content ) = @_;
168
169
169
    if ( ref($tab_content) eq 'ARRAY' ) {
170
    if ( ref($tab_content) eq 'ARRAY' ) {
170
        return $self->get_translated_prefs( $file, $tab_content );
171
        return $self->get_translated_prefs( $tab, $tab_content );
171
    }
172
    }
172
173
173
    my $translated_tab_content = {
174
    my $translated_tab_content = {
174
        map {
175
        map {
175
            my $section  = $_;
176
            my $section  = $_;
176
            my $sysprefs = $tab_content->{$section};
177
            my $sysprefs = $tab_content->{$section};
177
            my $msgid    = sprintf( '%s %s', $file, $section );
178
            my $context = "$tab > $section";
178
179
179
            $self->get_trans_text( $msgid, $section ) => $self->get_translated_prefs( $file, $sysprefs );
180
            $self->get_trans_text( $tab, $section ) => $self->get_translated_prefs( $context, $sysprefs );
180
        } keys %$tab_content
181
        } keys %$tab_content
181
    };
182
    };
182
183
Lines 197-210 sub get_translated_tab_content { Link Here
197
}
198
}
198
199
199
sub get_translated_prefs {
200
sub get_translated_prefs {
200
    my ( $self, $file, $sysprefs ) = @_;
201
    my ( $self, $context, $sysprefs ) = @_;
201
202
202
    my $translated_prefs = [
203
    my $translated_prefs = [
203
        map {
204
        map {
204
            my ($pref_elt) = grep { ref($_) eq 'HASH' && exists $_->{pref} } @$_;
205
            my ($pref_elt) = grep { ref($_) eq 'HASH' && exists $_->{pref} } @$_;
205
            my $pref_name = $pref_elt ? $pref_elt->{pref} : '';
206
            my $pref_name = $pref_elt ? $pref_elt->{pref} : '';
206
207
207
            my $translated_syspref = [ map { $self->get_translated_pref( $file, $pref_name, $_ ); } @$_ ];
208
            my $translated_syspref = [ map { $self->get_translated_pref( "$context > $pref_name", $_ ); } @$_ ];
208
209
209
            $translated_syspref;
210
            $translated_syspref;
210
        } @$sysprefs
211
        } @$sysprefs
Lines 214-225 sub get_translated_prefs { Link Here
214
}
215
}
215
216
216
sub get_translated_pref {
217
sub get_translated_pref {
217
    my ( $self, $file, $pref_name, $syspref ) = @_;
218
    my ( $self, $context, $syspref ) = @_;
218
219
219
    unless ( ref($syspref) ) {
220
    unless ( ref($syspref) ) {
220
        $syspref //= '';
221
        return $self->get_trans_text( $context, $syspref // '' );
221
        my $msgid = sprintf( '%s#%s# %s', $file, $pref_name, $syspref );
222
        return $self->get_trans_text( $msgid, $syspref );
223
    }
222
    }
224
223
225
    my $translated_pref = {
224
    my $translated_pref = {
Lines 229-240 sub get_translated_pref { Link Here
229
228
230
            my $translated_value = $value;
229
            my $translated_value = $value;
231
            if ( ( $key eq 'choices' || $key eq 'multiple' || $key eq 'multiple_sortable' ) && ref($value) eq 'HASH' ) {
230
            if ( ( $key eq 'choices' || $key eq 'multiple' || $key eq 'multiple_sortable' ) && ref($value) eq 'HASH' ) {
232
                $translated_value = {
231
                $translated_value = { map { $_ => $self->get_trans_text( $context, $value->{$_} ) } keys %$value };
233
                    map {
234
                        my $msgid = sprintf( '%s#%s# %s', $file, $pref_name, $value->{$_} );
235
                        $_ => $self->get_trans_text( $msgid, $value->{$_} )
236
                    } keys %$value
237
                };
238
            }
232
            }
239
233
240
            $key => $translated_value
234
            $key => $translated_value
Lines 252-258 sub install_prefs { Link Here
252
        exit;
246
        exit;
253
    }
247
    }
254
248
255
    $self->{po} = Locale::PO->load_file_ashash( $self->po_filename("-pref.po"), 'utf8' );
249
    my @po_entries = @{ Locale::PO->load_file_asarray( $self->po_filename("-pref.po"), 'utf8' ) };
250
    $self->{po} = {
251
        map {
252
            my $msgctxt = $_->msgctxt ? Locale::PO->dequote( $_->msgctxt ) : '';
253
            my $msgid   = Locale::PO->dequote( $_->msgid );
254
255
            "$msgctxt;$msgid" => $_;
256
        } @po_entries
257
    };
256
258
257
    for my $file ( @{ $self->{pref_files} } ) {
259
    for my $file ( @{ $self->{pref_files} } ) {
258
        my $pref = YAML::XS::LoadFile( $self->{path_pref_en} . "/$file" );
260
        my $pref = YAML::XS::LoadFile( $self->{path_pref_en} . "/$file" );
Lines 262-268 sub install_prefs { Link Here
262
                my $tab         = $_;
264
                my $tab         = $_;
263
                my $tab_content = $pref->{$tab};
265
                my $tab_content = $pref->{$tab};
264
266
265
                $self->get_trans_text( $file, $tab ) => $self->get_translated_tab_content( $file, $tab_content );
267
                $self->get_trans_text( undef, $tab ) => $self->get_translated_tab_content( $tab, $tab_content );
266
            } keys %$pref
268
            } keys %$pref
267
        };
269
        };
268
270
(-)a/misc/translator/xgettext-pref (-11 / +9 lines)
Lines 45-51 display this help and exit Link Here
45
45
46
use Modern::Perl;
46
use Modern::Perl;
47
47
48
use File::Basename;
49
use Getopt::Long;
48
use Getopt::Long;
50
use Locale::PO;
49
use Locale::PO;
51
use Pod::Usage;
50
use Pod::Usage;
Lines 89-103 my $pot = { Link Here
89
for my $file (@files) {
88
for my $file (@files) {
90
    my $pref = YAML::XS::LoadFile($file);
89
    my $pref = YAML::XS::LoadFile($file);
91
    while ( my ($tab, $tab_content) = each %$pref ) {
90
    while ( my ($tab, $tab_content) = each %$pref ) {
92
        add_po(undef, basename($file));
91
        add_po($file, undef, $tab);
93
92
94
        if ( ref($tab_content) eq 'ARRAY' ) {
93
        if ( ref($tab_content) eq 'ARRAY' ) {
95
            add_prefs( $file, $tab, $tab_content );
94
            add_prefs( $file, $tab, $tab_content );
96
        } else {
95
        } else {
97
            while ( my ($section, $sysprefs) = each %$tab_content ) {
96
            while ( my ($section, $sysprefs) = each %$tab_content ) {
98
                my $context = "$tab > $section";
97
                my $context = "$tab > $section";
99
                my $msgid = sprintf('%s %s', basename($file), $section);
98
                add_po($file, $tab, $section);
100
                add_po($tab, $msgid);
101
                add_prefs( $file, $context, $sysprefs );
99
                add_prefs( $file, $context, $sysprefs );
102
            }
100
            }
103
        }
101
        }
Lines 123-148 sub add_prefs { Link Here
123
                    next unless $key eq 'choices' or $key eq 'multiple' or $key eq 'multiple_sortable';
121
                    next unless $key eq 'choices' or $key eq 'multiple' or $key eq 'multiple_sortable';
124
                    next unless ref($value) eq 'HASH';
122
                    next unless ref($value) eq 'HASH';
125
                    for my $ckey ( keys %$value ) {
123
                    for my $ckey ( keys %$value ) {
126
                        my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $value->{$ckey});
124
                        add_po( $file, "$context > $pref_name", $value->{$ckey} );
127
                        add_po( "$context > $pref_name", $msgid );
128
                    }
125
                    }
129
                }
126
                }
130
            }
127
            }
131
            elsif ($element) {
128
            elsif ($element) {
132
                my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $element);
129
                add_po( $file, "$context > $pref_name", $element );
133
                add_po( "$context > $pref_name", $msgid );
134
            }
130
            }
135
        }
131
        }
136
    }
132
    }
137
}
133
}
138
134
139
sub add_po {
135
sub add_po {
140
    my ($comment, $msgid ) = @_;
136
    my ( $reference, $msgctxt, $msgid ) = @_;
141
137
142
    return unless $msgid;
138
    return unless $msgid;
143
139
144
    $pot->{$msgid} = Locale::PO->new(
140
    my $key = ($msgctxt // '') . ";$msgid";
145
        -comment   => $comment,
141
    $pot->{$key} = Locale::PO->new(
142
        -reference => $reference,
143
        -msgctxt   => $msgctxt,
146
        -msgid     => $msgid,
144
        -msgid     => $msgid,
147
        -msgstr    => '',
145
        -msgstr    => '',
148
    );
146
    );
(-)a/t/db_dependent/misc/translator/xgettext-pref.t (-9 / +15 lines)
Lines 20-47 my $pot = Locale::PO->load_file_asarray("$tempdir/Koha.pot"); Link Here
20
20
21
my @expected = (
21
my @expected = (
22
    {
22
    {
23
        msgid => '"sample.pref"',
23
        msgid => '"Section"',
24
    },
24
    },
25
    {
25
    {
26
        msgid => '"sample.pref Subsection"',
26
        msgctxt => '"Section > Subsection > MultiplePref"',
27
        msgid => '"Bar"',
27
    },
28
    },
28
    {
29
    {
29
        msgid => '"sample.pref#MultiplePref# Bar"',
30
        msgctxt => '"Section > Subsection > MultiplePref"',
31
        msgid => '"Baz"',
30
    },
32
    },
31
    {
33
    {
32
        msgid => '"sample.pref#MultiplePref# Baz"',
34
        msgctxt => '"Section > Subsection > MultiplePref"',
35
        msgid => '"Foo ツ"',
33
    },
36
    },
34
    {
37
    {
35
        msgid => '"sample.pref#MultiplePref# Foo ツ"',
38
        msgctxt => '"Section > Subsection > SamplePref"',
39
        msgid => '"Do"',
36
    },
40
    },
37
    {
41
    {
38
        msgid => '"sample.pref#SamplePref# Do"',
42
        msgctxt => '"Section > Subsection > SamplePref"',
43
        msgid => '"Do not do"',
39
    },
44
    },
40
    {
45
    {
41
        msgid => '"sample.pref#SamplePref# Do not do"',
46
        msgctxt => '"Section > Subsection > SamplePref"',
47
        msgid => '"that thing"',
42
    },
48
    },
43
    {
49
    {
44
        msgid => '"sample.pref#SamplePref# that thing"',
50
        msgctxt => '"Section"',
51
        msgid => '"Subsection"',
45
    },
52
    },
46
);
53
);
47
54
48
- 

Return to bug 26547