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 228-248 sub _expand { Link Here
228
228
229
=head2 db_t
229
=head2 db_t
230
230
231
    db_t($context, $source)
231
    db_t($group, $text)
232
    db_t('itemtype', $itemtype->description)
232
    db_t('itemtype', $itemtype->description)
233
233
234
Search for a translation in l10n_target table and return it if found
234
Search for a translation in l10n_target table and return it if found
235
Return source string otherwise
235
Return C<$text> otherwise
236
236
237
=cut
237
=cut
238
238
239
sub db_t {
239
sub db_t {
240
    my ($context, $source) = @_;
240
    my ($group, $text) = @_;
241
241
242
    my $cache = Koha::Caches->get_instance();
242
    my $cache = Koha::Caches->get_instance();
243
    my $language = C4::Languages::getlanguage();
243
    unless ($cache->is_cache_active) {
244
    my $cache_key = sprintf('%s:%s', $context, $language);
244
        $cache = Koha::Cache::Memory::Lite->get_instance();
245
    }
245
246
247
    my $language = C4::Languages::getlanguage();
248
    my $cache_key = sprintf('l10n:%s:%s', $group, $language);
246
    my $translations = $cache->get_from_cache($cache_key);
249
    my $translations = $cache->get_from_cache($cache_key);
247
    unless ($translations) {
250
    unless ($translations) {
248
        my $schema = Koha::Database->new->schema;
251
        my $schema = Koha::Database->new->schema;
Lines 250-256 sub db_t { Link Here
250
253
251
        my @targets = $rs->search(
254
        my @targets = $rs->search(
252
            {
255
            {
253
                'l10n_source.context' => $context,
256
                'l10n_source.group' => $group,
254
                language => $language,
257
                language => $language,
255
            },
258
            },
256
            {
259
            {
Lines 259-274 sub db_t { Link Here
259
                result_class => 'DBIx::Class::ResultClass::HashRefInflator',
262
                result_class => 'DBIx::Class::ResultClass::HashRefInflator',
260
            },
263
            },
261
        );
264
        );
262
        $translations = { map { $_->{l10n_source}->{source} => $_->{translation} } @targets };
265
        $translations = { map { $_->{l10n_source}->{text} => $_->{translation} } @targets };
263
266
264
        $cache->set_in_cache($cache_key, $translations);
267
        $cache->set_in_cache($cache_key, $translations);
265
    }
268
    }
266
269
267
    if (exists $translations->{$source}) {
270
    if (exists $translations->{$text}) {
268
        return $translations->{$source};
271
        return $translations->{$text};
269
    }
272
    }
270
273
271
    return $source;
274
    return $text;
272
}
275
}
273
276
274
1;
277
1;
(-)a/Koha/Schema/Result/Itemtype.pm (-6 / +3 lines)
Lines 356-363 sub insert { Link Here
356
356
357
    my $result = $self->next::method(@_);
357
    my $result = $self->next::method(@_);
358
358
359
    my @sources = $self->result_source->resultset->get_column('description')->all;
359
    $self->update_l10n_source('itemtype', $self->itemtype, $self->description);
360
    $self->update_l10n_source('itemtype', @sources);
361
360
362
    return $result;
361
    return $result;
363
}
362
}
Lines 370-377 sub update { Link Here
370
    my $result = $self->next::method(@_);
369
    my $result = $self->next::method(@_);
371
370
372
    if ($is_description_changed) {
371
    if ($is_description_changed) {
373
        my @sources = $self->result_source->resultset->get_column('description')->all;
372
        $self->update_l10n_source('itemtype', $self->itemtype, $self->description);
374
        $self->update_l10n_source('itemtype', @sources);
375
    }
373
    }
376
374
377
    return $result;
375
    return $result;
Lines 382-389 sub delete { Link Here
382
380
383
    my $result = $self->next::method(@_);
381
    my $result = $self->next::method(@_);
384
382
385
    my @sources = $self->result_source->resultset->get_column('description')->all;
383
    $self->delete_l10n_source('itemtype', $self->itemtype);
386
    $self->update_l10n_source('itemtype', @sources);
387
384
388
    return $result;
385
    return $result;
389
}
386
}
(-)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 45-52 Link Here
45
                    <table id="localization">
45
                    <table id="localization">
46
                        <thead>
46
                        <thead>
47
                            <tr>
47
                            <tr>
48
                                <th>Context</th>
48
                                <th>Group</th>
49
                                <th>Source</th>
49
                                <th>Text</th>
50
                                <th>Translations</th>
50
                                <th>Translations</th>
51
                            </tr>
51
                            </tr>
52
                        </thead>
52
                        </thead>
Lines 89-100 Link Here
89
                },
89
                },
90
                columns: [
90
                columns: [
91
                    {
91
                    {
92
                        name: 'context',
92
                        name: 'group',
93
                        data: 'context',
93
                        data: 'group',
94
                    },
94
                    },
95
                    {
95
                    {
96
                        name: 'source',
96
                        name: 'text',
97
                        data: 'source',
97
                        data: 'text',
98
                    },
98
                    },
99
                    {
99
                    {
100
                        name: 'targets',
100
                        name: 'targets',
Lines 120-126 Link Here
120
                                translateLink.attr('href', '#');
120
                                translateLink.attr('href', '#');
121
                                translateLink.attr('data-l10n-source-id', row.l10n_source_id);
121
                                translateLink.attr('data-l10n-source-id', row.l10n_source_id);
122
                                translateLink.attr('data-language', language.rfc4646_subtag);
122
                                translateLink.attr('data-language', language.rfc4646_subtag);
123
                                translateLink.attr('data-source', row.source);
124
                                if (translations[language.rfc4646_subtag]) {
123
                                if (translations[language.rfc4646_subtag]) {
125
                                    translateLink.attr('data-translation', translations[language.rfc4646_subtag].translation);
124
                                    translateLink.attr('data-translation', translations[language.rfc4646_subtag].translation);
126
                                }
125
                                }
(-)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