aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Roles/Translatable.pm
blob: 0c84bbf0f3d10c3868a16f12de3176e7643de151 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package FixMyStreet::Roles::Translatable;

use Moo::Role;
use FixMyStreet;

has _translated  => (is => 'rw');

sub translated {
    my $self = shift;
    $self->_translated or $self->_translated({});
}

sub translate_around {
    my ($orig, $self) = (shift, shift);
    my $fallback = $self->$orig(@_);
    (my $col = (caller(2))[3]) =~ s/.*:://;
    $self->_translate($col, $fallback);
}

sub translate_column {
    my ($self, $col) = (shift, shift);
    my $fallback = $self->$col(@_);
    $self->_translate($col, $fallback);
}

sub _translate {
    my ($self, $col, $fallback) = @_;

    my $langs = FixMyStreet->config('LANGUAGES');
    return $fallback if !$langs || @$langs < 2;

    my %cols = $self->get_columns;
    return $cols{msgstr} if $cols{msgstr};

    my $schema = $self->result_source->schema;
    my $table = lc $self->result_source->source_name;
    my $id = $self->id;
    my $lang = $schema->lang || '';

    my $translated = $self->translated->{$col}{$lang};
    return $translated if $translated;

    if (ref $schema) {
        my $translation = $schema->resultset('Translation')->find({
            lang => $lang,
            tbl => $table,
            object_id => $id,
            col => $col
        });
        $fallback = $translation->msgstr if $translation;
    } else {
        warn "Can't use translation on this call to $table.$col";
    }
    $self->translated->{$col}{$lang} = $fallback;
    return $fallback;
};

# These next two functions (translation_for and and_translation_for) are
# convenience methods for use in the translation interface in the admin.
# They shouldn't be used else where as they don't take account of things
# like denormalised strings (e.g report category)
sub translation_for {
    my ($self, $col, $lang) = @_;

    my $schema = $self->result_source->schema;

    my $props = {
        tbl => lc $self->result_source->source_name,
        object_id => $self->id,
        col => $col
    };

    if ($lang) {
        $props->{lang} = $lang;
    }

    my $translations = $schema->resultset('Translation')->search($props);

    return $lang ? $translations->first : $translations;
}

sub add_translation_for {
    my ($self, $col, $lang, $msgstr) = @_;

    my $schema = $self->result_source->schema;

    my $props = {
        tbl => lc $self->result_source->source_name,
        object_id => $self->id,
        col => $col,
        lang => $lang,
        msgstr => $msgstr,
    };

    my $translation = $schema->resultset('Translation')->update_or_create(
        $props,
        { key => 'translation_tbl_object_id_col_lang_key' }
    );

    return $translation;
}

1;