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

(-)a/Koha/DBIx/Component/L10nSource.pm (-18 / +32 lines)
Lines 42-73 Koha::DBIx::Component::L10nSource Link Here
42
42
43
=head2 update_l10n_source
43
=head2 update_l10n_source
44
44
45
    $self->update_l10n_source($context, @sources)
45
    $self->update_l10n_source($group, $key, $text)
46
46
47
Ensure that all sources in C<@sources>and only them are present in l10n_source
47
Update or create an entry in l10n_source
48
for a given C<$context>
49
48
50
=cut
49
=cut
51
50
52
sub update_l10n_source {
51
sub update_l10n_source {
53
    my ($self, $context, @sources) = @_;
52
    my ($self, $group, $key, $text) = @_;
53
54
    my $l10n_source_rs = $self->result_source->schema->resultset('L10nSource');
55
    $l10n_source_rs->update_or_create(
56
        {
57
            group => $group,
58
            key   => $key,
59
            text  => $text,
60
        },
61
        {
62
            key => 'group_key',
63
        }
64
    );
65
}
54
66
55
    my $l10n_source_rs = $self->result_source->schema->resultset('L10nSource')->search({ context => $context });
67
=head2 delete_l10n_source
56
68
57
    # Insert missing l10n_source rows
69
    $self->delete_l10n_source($group, $key, $text)
58
    foreach my $source (@sources) {
59
        my $count = $l10n_source_rs->search({ source => $source })->count;
60
        if ($count == 0) {
61
            $l10n_source_rs->create({ source => $source });
62
        }
63
    }
64
70
65
    # Remove l10n_source rows that do not match an existing source
71
Remove an entry from l10n_source
66
    my %sources = map { $_ => undef } @sources;
72
67
    my @l10n_sources = grep { !exists $sources{$_->source} } $l10n_source_rs->all;
73
=cut
68
    foreach my $l10n_source (@l10n_sources) {
74
69
        $l10n_source->delete;
75
sub delete_l10n_source {
70
    }
76
    my ($self, $group, $key) = @_;
77
78
    my $l10n_source_rs = $self->result_source->schema->resultset('L10nSource');
79
    $l10n_source_rs->search(
80
        {
81
            group => $group,
82
            key   => $key,
83
        }
84
    )->delete();
71
}
85
}
72
86
73
1;
87
1;
(-)a/Koha/I18N.pm (-10 / +13 lines)
Lines 207-227 sub _expand { Link Here
207
207
208
=head2 db_t
208
=head2 db_t
209
209
210
    db_t($context, $source)
210
    db_t($group, $text)
211
    db_t('itemtype', $itemtype->description)
211
    db_t('itemtype', $itemtype->description)
212
212
213
Search for a translation in l10n_target table and return it if found
213
Search for a translation in l10n_target table and return it if found
214
Return source string otherwise
214
Return C<$text> otherwise
215
215
216
=cut
216
=cut
217
217
218
sub db_t {
218
sub db_t {
219
    my ($context, $source) = @_;
219
    my ($group, $text) = @_;
220
220
221
    my $cache = Koha::Caches->get_instance();
221
    my $cache = Koha::Caches->get_instance();
222
    my $language = C4::Languages::getlanguage();
222
    unless ($cache->is_cache_active) {
223
    my $cache_key = sprintf('%s:%s', $context, $language);
223
        $cache = Koha::Cache::Memory::Lite->get_instance();
224
    }
224
225
226
    my $language = C4::Languages::getlanguage();
227
    my $cache_key = sprintf('l10n:%s:%s', $group, $language);
225
    my $translations = $cache->get_from_cache($cache_key);
228
    my $translations = $cache->get_from_cache($cache_key);
226
    unless ($translations) {
229
    unless ($translations) {
227
        my $schema = Koha::Database->new->schema;
230
        my $schema = Koha::Database->new->schema;
Lines 229-235 sub db_t { Link Here
229
232
230
        my @targets = $rs->search(
233
        my @targets = $rs->search(
231
            {
234
            {
232
                'l10n_source.context' => $context,
235
                'l10n_source.group' => $group,
233
                language => $language,
236
                language => $language,
234
            },
237
            },
235
            {
238
            {
Lines 238-253 sub db_t { Link Here
238
                result_class => 'DBIx::Class::ResultClass::HashRefInflator',
241
                result_class => 'DBIx::Class::ResultClass::HashRefInflator',
239
            },
242
            },
240
        );
243
        );
241
        $translations = { map { $_->{l10n_source}->{source} => $_->{translation} } @targets };
244
        $translations = { map { $_->{l10n_source}->{text} => $_->{translation} } @targets };
242
245
243
        $cache->set_in_cache($cache_key, $translations);
246
        $cache->set_in_cache($cache_key, $translations);
244
    }
247
    }
245
248
246
    if (exists $translations->{$source}) {
249
    if (exists $translations->{$text}) {
247
        return $translations->{$source};
250
        return $translations->{$text};
248
    }
251
    }
249
252
250
    return $source;
253
    return $text;
251
}
254
}
252
255
253
1;
256
1;
(-)a/Koha/Schema/Result/Itemtype.pm (-6 / +3 lines)
Lines 244-251 sub insert { Link Here
244
244
245
    my $result = $self->next::method(@_);
245
    my $result = $self->next::method(@_);
246
246
247
    my @sources = $self->result_source->resultset->get_column('description')->all;
247
    $self->update_l10n_source('itemtype', $self->itemtype, $self->description);
248
    $self->update_l10n_source('itemtype', @sources);
249
248
250
    return $result;
249
    return $result;
251
}
250
}
Lines 258-265 sub update { Link Here
258
    my $result = $self->next::method(@_);
257
    my $result = $self->next::method(@_);
259
258
260
    if ($is_description_changed) {
259
    if ($is_description_changed) {
261
        my @sources = $self->result_source->resultset->get_column('description')->all;
260
        $self->update_l10n_source('itemtype', $self->itemtype, $self->description);
262
        $self->update_l10n_source('itemtype', @sources);
263
    }
261
    }
264
262
265
    return $result;
263
    return $result;
Lines 270-277 sub delete { Link Here
270
268
271
    my $result = $self->next::method(@_);
269
    my $result = $self->next::method(@_);
272
270
273
    my @sources = $self->result_source->resultset->get_column('description')->all;
271
    $self->delete_l10n_source('itemtype', $self->itemtype);
274
    $self->update_l10n_source('itemtype', @sources);
275
272
276
    return $result;
273
    return $result;
277
}
274
}
(-)a/installer/data/mysql/atomicupdate/bug-24975.perl (-7 / +9 lines)
Lines 3-12 if( CheckVersion( $DBversion ) ) { Link Here
3
    $dbh->do(q{
3
    $dbh->do(q{
4
        CREATE TABLE IF NOT EXISTS l10n_source (
4
        CREATE TABLE IF NOT EXISTS l10n_source (
5
            l10n_source_id INT(11) NOT NULL AUTO_INCREMENT,
5
            l10n_source_id INT(11) NOT NULL AUTO_INCREMENT,
6
            context VARCHAR(100) NULL DEFAULT NULL,
6
            `group` VARCHAR(100) NULL DEFAULT NULL,
7
            source TEXT NOT NULL,
7
            `key` VARCHAR(100) NOT NULL,
8
            `text` TEXT NOT NULL,
8
            PRIMARY KEY (l10n_source_id),
9
            PRIMARY KEY (l10n_source_id),
9
            KEY context_source (context, source(60))
10
            UNIQUE KEY group_key (`group`, `key`),
11
            KEY group_text (`group`, `text`(60))
10
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
12
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
11
    });
13
    });
12
14
Lines 25-41 if( CheckVersion( $DBversion ) ) { Link Here
25
27
26
    # Populate l10n_source and l10n_target for itemtypes
28
    # Populate l10n_source and l10n_target for itemtypes
27
    $dbh->do(q{
29
    $dbh->do(q{
28
        INSERT INTO l10n_source (context, source)
30
        INSERT IGNORE INTO l10n_source (`group`, `key`, `text`)
29
        SELECT DISTINCT 'itemtype', description FROM itemtypes
31
        SELECT DISTINCT 'itemtype', itemtype, description FROM itemtypes
30
    });
32
    });
31
33
32
    if (TableExists('localization')) {
34
    if (TableExists('localization')) {
33
        $dbh->do(q{
35
        $dbh->do(q{
34
            INSERT INTO l10n_target (l10n_source_id, language, translation)
36
            INSERT IGNORE INTO l10n_target (l10n_source_id, language, translation)
35
            SELECT DISTINCT l10n_source_id, lang, translation
37
            SELECT DISTINCT l10n_source_id, lang, translation
36
            FROM localization
38
            FROM localization
37
            JOIN itemtypes ON (localization.code = itemtypes.itemtype)
39
            JOIN itemtypes ON (localization.code = itemtypes.itemtype)
38
            JOIN l10n_source ON (itemtypes.description = l10n_source.source AND l10n_source.context = 'itemtype')
40
            JOIN l10n_source ON (itemtypes.itemtype = l10n_source.`key` AND l10n_source.`group` = 'itemtype')
39
            WHERE entity = 'itemtypes'
41
            WHERE entity = 'itemtypes'
40
        });
42
        });
41
43
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/localization.tt (-7 / +6 lines)
Lines 43-50 Link Here
43
                    <table id="localization">
43
                    <table id="localization">
44
                        <thead>
44
                        <thead>
45
                            <tr>
45
                            <tr>
46
                                <th>Context</th>
46
                                <th>Group</th>
47
                                <th>Source</th>
47
                                <th>Text</th>
48
                                <th>Translations</th>
48
                                <th>Translations</th>
49
                            </tr>
49
                            </tr>
50
                        </thead>
50
                        </thead>
Lines 87-98 Link Here
87
                },
87
                },
88
                columns: [
88
                columns: [
89
                    {
89
                    {
90
                        name: 'context',
90
                        name: 'group',
91
                        data: 'context',
91
                        data: 'group',
92
                    },
92
                    },
93
                    {
93
                    {
94
                        name: 'source',
94
                        name: 'text',
95
                        data: 'source',
95
                        data: 'text',
96
                    },
96
                    },
97
                    {
97
                    {
98
                        name: 'targets',
98
                        name: 'targets',
Lines 118-124 Link Here
118
                                translateLink.attr('href', '#');
118
                                translateLink.attr('href', '#');
119
                                translateLink.attr('data-l10n-source-id', row.l10n_source_id);
119
                                translateLink.attr('data-l10n-source-id', row.l10n_source_id);
120
                                translateLink.attr('data-language', language.rfc4646_subtag);
120
                                translateLink.attr('data-language', language.rfc4646_subtag);
121
                                translateLink.attr('data-source', row.source);
122
                                if (translations[language.rfc4646_subtag]) {
121
                                if (translations[language.rfc4646_subtag]) {
123
                                    translateLink.attr('data-translation', translations[language.rfc4646_subtag].translation);
122
                                    translateLink.attr('data-translation', translations[language.rfc4646_subtag].translation);
124
                                }
123
                                }
(-)a/svc/localization (-4 / +3 lines)
Lines 24-30 sub get_translations { Link Here
24
        my @and;
24
        my @and;
25
        my @words = split /\s+/, $search;
25
        my @words = split /\s+/, $search;
26
        foreach my $word (@words) {
26
        foreach my $word (@words) {
27
            push @and, \['source LIKE ? COLLATE utf8mb4_unicode_ci', "%$word%"];
27
            push @and, \['text LIKE ? COLLATE utf8mb4_unicode_ci', "%$word%"];
28
        }
28
        }
29
        $cond->{-and} = \@and;
29
        $cond->{-and} = \@and;
30
    }
30
    }
Lines 37-43 sub get_translations { Link Here
37
        result_class => 'DBIx::Class::ResultClass::HashRefInflator',
37
        result_class => 'DBIx::Class::ResultClass::HashRefInflator',
38
        rows => $length,
38
        rows => $length,
39
        offset => $start,
39
        offset => $start,
40
        order_by => { -asc => ['context', 'source'] },
40
        order_by => { -asc => ['group', 'text'] },
41
    });
41
    });
42
42
43
    $response->param(
43
    $response->param(
Lines 73-79 sub update_translation { Link Here
73
    }
73
    }
74
74
75
    my $source = $schema->resultset('L10nSource')->find($id);
75
    my $source = $schema->resultset('L10nSource')->find($id);
76
    my $cache_key = sprintf('%s:%s', $source->context, $lang);
76
    my $cache_key = sprintf('l10n:%s:%s', $source->group, $lang);
77
    Koha::Caches->get_instance()->clear_from_cache($cache_key);
77
    Koha::Caches->get_instance()->clear_from_cache($cache_key);
78
78
79
    C4::Service->return_success( $response );
79
    C4::Service->return_success( $response );
80
- 

Return to bug 24975