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

(-)a/misc/translator/LangInstaller.pm (-20 / +24 lines)
Lines 148-180 sub po_filename { Link Here
148
}
148
}
149
149
150
sub get_trans_text {
150
sub get_trans_text {
151
    my ($self, $msgid, $default) = @_;
151
    my ($self, $msgctxt, $msgid) = @_;
152
152
153
    my $po = $self->{po}->{Locale::PO->quote($msgid)};
153
    my $key = ($msgctxt // '') . ";$msgid";
154
155
    my $po = $self->{po}->{$key};
154
    if ($po) {
156
    if ($po) {
155
        my $msgstr = Locale::PO->dequote($po->msgstr);
157
        my $msgstr = Locale::PO->dequote($po->msgstr);
156
        if ($msgstr and length($msgstr) > 0) {
158
157
            return $msgstr;
159
        return $msgstr || $msgid;
158
        }
159
    }
160
    }
160
161
161
    return $default;
162
    return $msgid;
162
}
163
}
163
164
164
sub get_translated_tab_content {
165
sub get_translated_tab_content {
165
    my ($self, $file, $tab_content) = @_;
166
    my ($self, $tab, $tab_content) = @_;
166
167
167
    if ( ref($tab_content) eq 'ARRAY' ) {
168
    if ( ref($tab_content) eq 'ARRAY' ) {
168
        return $self->get_translated_prefs($file, $tab_content);
169
        return $self->get_translated_prefs($tab, $tab_content);
169
    }
170
    }
170
171
171
    my $translated_tab_content = {
172
    my $translated_tab_content = {
172
        map {
173
        map {
173
            my $section = $_;
174
            my $section = $_;
174
            my $sysprefs = $tab_content->{$section};
175
            my $sysprefs = $tab_content->{$section};
175
            my $msgid = sprintf('%s %s', $file, $section);
176
            my $context = "$tab > $section";
176
177
177
            $self->get_trans_text($msgid, $section) => $self->get_translated_prefs($file, $sysprefs);
178
            $self->get_trans_text($tab, $section) => $self->get_translated_prefs($context, $sysprefs);
178
        } keys %$tab_content
179
        } keys %$tab_content
179
    };
180
    };
180
181
Lines 182-188 sub get_translated_tab_content { Link Here
182
}
183
}
183
184
184
sub get_translated_prefs {
185
sub get_translated_prefs {
185
    my ($self, $file, $sysprefs) = @_;
186
    my ($self, $context, $sysprefs) = @_;
186
187
187
    my $translated_prefs = [
188
    my $translated_prefs = [
188
        map {
189
        map {
Lines 191-197 sub get_translated_prefs { Link Here
191
192
192
            my $translated_syspref = [
193
            my $translated_syspref = [
193
                map {
194
                map {
194
                    $self->get_translated_pref($file, $pref_name, $_);
195
                    $self->get_translated_pref("$context > $pref_name", $_ );
195
                } @$_
196
                } @$_
196
            ];
197
            ];
197
198
Lines 203-214 sub get_translated_prefs { Link Here
203
}
204
}
204
205
205
sub get_translated_pref {
206
sub get_translated_pref {
206
    my ($self, $file, $pref_name, $syspref) = @_;
207
    my ($self, $context, $syspref) = @_;
207
208
208
    unless (ref($syspref)) {
209
    unless (ref($syspref)) {
209
        $syspref //= '';
210
        return $self->get_trans_text($context, $syspref // '');
210
        my $msgid = sprintf('%s#%s# %s', $file, $pref_name, $syspref);
211
        return $self->get_trans_text($msgid, $syspref);
212
    }
211
    }
213
212
214
    my $translated_pref = {
213
    my $translated_pref = {
Lines 220-227 sub get_translated_pref { Link Here
220
            if (($key eq 'choices' || $key eq 'multiple') && ref($value) eq 'HASH') {
219
            if (($key eq 'choices' || $key eq 'multiple') && ref($value) eq 'HASH') {
221
                $translated_value = {
220
                $translated_value = {
222
                    map {
221
                    map {
223
                        my $msgid = sprintf('%s#%s# %s', $file, $pref_name, $value->{$_});
222
                        $_ => $self->get_trans_text($context, $value->{$_})
224
                        $_ => $self->get_trans_text($msgid, $value->{$_})
225
                    } keys %$value
223
                    } keys %$value
226
                }
224
                }
227
            }
225
            }
Lines 241-247 sub install_prefs { Link Here
241
        exit;
239
        exit;
242
    }
240
    }
243
241
244
    $self->{po} = Locale::PO->load_file_ashash($self->po_filename("-pref.po"), 'utf8');
242
    my @po_entries = @{ Locale::PO->load_file_asarray($self->po_filename("-pref.po"), 'utf8') };
243
    $self->{po} = { map {
244
        my $msgctxt = $_->msgctxt ? Locale::PO->dequote($_->msgctxt) : '';
245
        my $msgid = Locale::PO->dequote($_->msgid);
246
247
        "$msgctxt;$msgid" => $_;
248
    } @po_entries };
245
249
246
    for my $file ( @{$self->{pref_files}} ) {
250
    for my $file ( @{$self->{pref_files}} ) {
247
        my $pref = LoadFile( $self->{path_pref_en} . "/$file" );
251
        my $pref = LoadFile( $self->{path_pref_en} . "/$file" );
Lines 251-257 sub install_prefs { Link Here
251
                my $tab = $_;
255
                my $tab = $_;
252
                my $tab_content = $pref->{$tab};
256
                my $tab_content = $pref->{$tab};
253
257
254
                $self->get_trans_text($file, $tab) => $self->get_translated_tab_content($file, $tab_content);
258
                $self->get_trans_text(undef, $tab) => $self->get_translated_tab_content($tab, $tab_content);
255
            } keys %$pref
259
            } keys %$pref
256
        };
260
        };
257
261
(-)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 91-105 my $pot = { Link Here
91
for my $file (@files) {
90
for my $file (@files) {
92
    my $pref = LoadFile($file);
91
    my $pref = LoadFile($file);
93
    while ( my ($tab, $tab_content) = each %$pref ) {
92
    while ( my ($tab, $tab_content) = each %$pref ) {
94
        add_po(undef, basename($file));
93
        add_po($file, undef, $tab);
95
94
96
        if ( ref($tab_content) eq 'ARRAY' ) {
95
        if ( ref($tab_content) eq 'ARRAY' ) {
97
            add_prefs( $file, $tab, $tab_content );
96
            add_prefs( $file, $tab, $tab_content );
98
        } else {
97
        } else {
99
            while ( my ($section, $sysprefs) = each %$tab_content ) {
98
            while ( my ($section, $sysprefs) = each %$tab_content ) {
100
                my $context = "$tab > $section";
99
                my $context = "$tab > $section";
101
                my $msgid = sprintf('%s %s', basename($file), $section);
100
                add_po($file, $tab, $section);
102
                add_po($tab, $msgid);
103
                add_prefs( $file, $context, $sysprefs );
101
                add_prefs( $file, $context, $sysprefs );
104
            }
102
            }
105
        }
103
        }
Lines 125-150 sub add_prefs { Link Here
125
                    next unless $key eq 'choices' or $key eq 'multiple';
123
                    next unless $key eq 'choices' or $key eq 'multiple';
126
                    next unless ref($value) eq 'HASH';
124
                    next unless ref($value) eq 'HASH';
127
                    for my $ckey ( keys %$value ) {
125
                    for my $ckey ( keys %$value ) {
128
                        my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $value->{$ckey});
126
                        add_po( $file, "$context > $pref_name", $value->{$ckey} );
129
                        add_po( "$context > $pref_name", $msgid );
130
                    }
127
                    }
131
                }
128
                }
132
            }
129
            }
133
            elsif ($element) {
130
            elsif ($element) {
134
                my $msgid = sprintf('%s#%s# %s', basename($file), $pref_name, $element);
131
                add_po( $file, "$context > $pref_name", $element );
135
                add_po( "$context > $pref_name", $msgid );
136
            }
132
            }
137
        }
133
        }
138
    }
134
    }
139
}
135
}
140
136
141
sub add_po {
137
sub add_po {
142
    my ($comment, $msgid ) = @_;
138
    my ( $reference, $msgctxt, $msgid ) = @_;
143
139
144
    return unless $msgid;
140
    return unless $msgid;
145
141
146
    $pot->{$msgid} = Locale::PO->new(
142
    my $key = ($msgctxt // '') . ";$msgid";
147
        -comment   => $comment,
143
    $pot->{$key} = Locale::PO->new(
144
        -reference => $reference,
145
        -msgctxt   => $msgctxt,
148
        -msgid     => $msgid,
146
        -msgid     => $msgid,
149
        -msgstr    => '',
147
        -msgstr    => '',
150
    );
148
    );
(-)a/t/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