From dc7613329c275cd158fdde8faf1c0e301f5b7202 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Thu, 14 Sep 2017 21:01:56 +0100 Subject: Fix issues with Open/Closed translated too early. It was possible for _hardcoded_states to be reached with no locale selected (e.g. bin/update-all-reports), causing an error trying to translate Open/Closed. But translating those there wasn't correct, because that would then be cached and returned even if a different language was being used. Caching was ignored in testing, which did not help. We no longer translate the state names in their objects, only upon display at the point that we will know the locale. We can't simply return the translation of Open/Closed because there may be entries in the translation table as well. --- perllib/FixMyStreet/DB/ResultSet/State.pm | 12 +++++++++--- t/app/model/state.t | 27 ++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/perllib/FixMyStreet/DB/ResultSet/State.pm b/perllib/FixMyStreet/DB/ResultSet/State.pm index 8b6a8963e..ac13ec2a4 100644 --- a/perllib/FixMyStreet/DB/ResultSet/State.pm +++ b/perllib/FixMyStreet/DB/ResultSet/State.pm @@ -7,8 +7,9 @@ use Memcached; sub _hardcoded_states { my $rs = shift; - my $open = $rs->new({ id => -1, label => 'confirmed', type => 'open', name => _("Open") }); - my $closed = $rs->new({ id => -2, label => 'closed', type => 'closed', name => _("Closed") }); + # These are translated on use, not here + my $open = $rs->new({ id => -1, label => 'confirmed', type => 'open', name => "Open" }); + my $closed = $rs->new({ id => -2, label => 'closed', type => 'closed', name => "Closed" }); return ($open, $closed); } @@ -23,7 +24,7 @@ sub states { my $rs = shift; my $states = Memcached::get('states'); - if ($states && !FixMyStreet->test_mode) { + if ($states) { # Need to reattach schema $states->[0]->result_source->schema( $rs->result_source->schema ) if $states->[0]; return $states; @@ -62,10 +63,15 @@ sub display { 'fixed - council' => _("Fixed - Council"), 'fixed - user' => _("Fixed - User"), }; + my $translate_now = { + confirmed => _("Open"), + closed => _("Closed"), + }; $label = 'fixed' if $single_fixed && $label =~ /^fixed - (council|user)$/; return $unchanging->{$label} if $unchanging->{$label}; my ($state) = $rs->_filter(sub { $_->label eq $label }); return $label unless $state; + $state->name($translate_now->{$label}) if $translate_now->{$label}; return $state->msgstr; } diff --git a/t/app/model/state.t b/t/app/model/state.t index 1653e36e2..35f3d4fb3 100644 --- a/t/app/model/state.t +++ b/t/app/model/state.t @@ -1,5 +1,5 @@ use FixMyStreet::Test; -use Test::More; +use FixMyStreet::Cobrand; my $rs = FixMyStreet::DB->resultset('State'); my $trans_rs = FixMyStreet::DB->resultset('Translation'); @@ -14,6 +14,9 @@ for ( $trans_rs->create({ tbl => 'state', col => 'name', object_id => $obj->id, lang => $lang, msgstr => "$lang $_->{label}" }); } +$trans_rs->create({ tbl => 'state', col => 'name', object_id => -1, lang => 'en-gb', msgstr => "Open Eng trans" }); + +$rs->clear; my $states = $rs->states; my %states = map { $_->label => $_ } @$states; @@ -25,6 +28,8 @@ subtest 'Open/closed database data is as expected' => sub { is @$closed, 5; }; +# No language set at this point + is $rs->display('investigating'), 'Investigating'; is $rs->display('bad'), 'bad'; is $rs->display('confirmed'), 'Open'; @@ -41,6 +46,9 @@ subtest 'default name is untranslated' => sub { }; subtest 'msgstr gets translated if available when the language changes' => sub { + FixMyStreet::DB->schema->lang('en-gb'); + is $states{confirmed}->name, 'Open'; + is $states{confirmed}->msgstr, 'Open Eng trans'; FixMyStreet::DB->schema->lang('de'); is $states{'in progress'}->name, 'In progress'; is $states{'in progress'}->msgstr, 'de in progress'; @@ -50,8 +58,6 @@ subtest 'msgstr gets translated if available when the language changes' => sub { is $states{'unable to fix'}->msgstr, 'No further action'; }; -$rs->clear; - is_deeply [ sort FixMyStreet::DB::Result::Problem->open_states ], ['action scheduled', 'confirmed', 'in progress', 'investigating', 'planned'], 'open states okay'; is_deeply [ sort FixMyStreet::DB::Result::Problem->closed_states ], @@ -59,4 +65,19 @@ is_deeply [ sort FixMyStreet::DB::Result::Problem->closed_states ], is_deeply [ sort FixMyStreet::DB::Result::Problem->fixed_states ], ['fixed', 'fixed - council', 'fixed - user'], 'fixed states okay'; +FixMyStreet::override_config { + LANGUAGES => [ 'en-gb,English,en_GB', 'nb,Norwegian,nb_NO' ], +}, sub { + subtest 'translation of open works both ways (file/db)' => sub { + # Note at this point the states have been cached + my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker('default')->new; + my $lang = $cobrand->set_lang_and_domain('nb', 1, FixMyStreet->path_to('locale')->stringify); + is $lang, 'nb'; + is $rs->display('confirmed'), "Ă…pen"; + $lang = $cobrand->set_lang_and_domain('en-gb', 1, FixMyStreet->path_to('locale')->stringify); + is $lang, 'en-gb'; + is $rs->display('confirmed'), "Open Eng trans"; + }; +}; + done_testing(); -- cgit v1.2.3