diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-07-16 21:52:54 +0100 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2017-08-09 17:14:49 +0100 |
commit | fe4153e367bcbb74eebed9c8ac0126d94709506c (patch) | |
tree | 692cb8c92ccbc894809b89f63d17c028fd026745 | |
parent | e00c75d7f19bc45879c254ea77dce32df825f3a7 (diff) |
Set up translatable body name.
-rw-r--r-- | perllib/FixMyStreet/App.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Around.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Reports.pm | 13 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Body.pm | 7 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Schema.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/Roles/Translatable.pm | 39 | ||||
-rw-r--r-- | t/app/controller/reports.t | 19 | ||||
-rw-r--r-- | t/roles/translatable.t | 22 |
9 files changed, 112 insertions, 4 deletions
diff --git a/perllib/FixMyStreet/App.pm b/perllib/FixMyStreet/App.pm index c1628d010..2365118ea 100644 --- a/perllib/FixMyStreet/App.pm +++ b/perllib/FixMyStreet/App.pm @@ -168,6 +168,9 @@ template paths, maps, languages etc, etc. sub setup_request { my $c = shift; + # Set the Catalyst model schema to the same as the DB schema + $c->model("DB")->schema( FixMyStreet::DB->schema ); + $c->setup_dev_overrides(); my $cobrand = $c->cobrand; diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index bd9e80dc7..561a6c2e3 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -196,8 +196,8 @@ sub display_location : Private { my @pins; unless ($c->get_param('no_pins')) { @pins = map { - # Here we might have a DB::Problem or a DB::Nearby, we always want the problem. - my $p = (ref $_ eq 'FixMyStreet::App::Model::DB::Nearby') ? $_->problem : $_; + # Here we might have a DB::Problem or a DB::Result::Nearby, we always want the problem. + my $p = (ref $_ eq 'FixMyStreet::DB::Result::Nearby') ? $_->problem : $_; $p->pin_data($c, 'around'); } @$on_map_all, @$nearby; } @@ -311,8 +311,8 @@ sub ajax : Path('/ajax') { # create a list of all the pins my @pins = map { - # Here we might have a DB::Problem or a DB::Nearby, we always want the problem. - my $p = (ref $_ eq 'FixMyStreet::App::Model::DB::Nearby') ? $_->problem : $_; + # Here we might have a DB::Problem or a DB::Result::Nearby, we always want the problem. + my $p = (ref $_ eq 'FixMyStreet::DB::Result::Nearby') ? $_->problem : $_; my $colour = $c->cobrand->pin_colour( $p, 'around' ); my $title = $c->cobrand->call_hook(pin_hover_title => $p, $p->title_safe) || $p->title_safe; [ $p->latitude, $p->longitude, diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index 8f068f0ec..c3616eed0 100644 --- a/perllib/FixMyStreet/App/Controller/Reports.pm +++ b/perllib/FixMyStreet/App/Controller/Reports.pm @@ -318,6 +318,19 @@ sub body_check : Private { } } + my @translations = $c->model('DB::Translation')->search( { + tbl => 'body', + col => 'name', + msgstr => $q_body + } )->all; + + if (@translations == 1) { + if ( my $body = $c->model('DB::Body')->find( { id => $translations[0]->object_id } ) ) { + $c->stash->{body} = $body; + return; + } + } + # No result, bad body name. $c->detach( 'redirect_index' ); } diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 4a886204c..1a0bbb0c8 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -269,6 +269,8 @@ sub set_lang_and_domain { DateTime->DefaultLocale( 'en_US' ); } + FixMyStreet::DB->schema->lang($set_lang); + return $set_lang; } sub languages { FixMyStreet->config('LANGUAGES') || [] } diff --git a/perllib/FixMyStreet/DB/Result/Body.pm b/perllib/FixMyStreet/DB/Result/Body.pm index 9a64d1608..db7777053 100644 --- a/perllib/FixMyStreet/DB/Result/Body.pm +++ b/perllib/FixMyStreet/DB/Result/Body.pm @@ -121,12 +121,19 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-02-13 15:11:11 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:BOJANVwg3kR/1VjDq0LykA +use Moo; +use namespace::clean; + +with 'FixMyStreet::Roles::Translatable'; + sub url { my ( $self, $c, $args ) = @_; # XXX $areas_info was used here for Norway parent - needs body parents, I guess return $c->uri_for( '/reports/' . $c->cobrand->short_name( $self ), $args || {} ); } +around name => \&translate_around; + sub areas { my $self = shift; my %ids = map { $_->area_id => 1 } $self->body_areas->all; diff --git a/perllib/FixMyStreet/DB/Schema.pm b/perllib/FixMyStreet/DB/Schema.pm index 7833ee68c..45d731c33 100644 --- a/perllib/FixMyStreet/DB/Schema.pm +++ b/perllib/FixMyStreet/DB/Schema.pm @@ -18,8 +18,11 @@ __PACKAGE__->load_namespaces( # Created by DBIx::Class::Schema::Loader v0.07035 @ 2017-07-13 14:15:09 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:UpH30RXb6SbCqRv2FPmpkg +use Moo; use FixMyStreet; __PACKAGE__->connection(FixMyStreet->dbic_connect_info); +has lang => ( is => 'rw' ); + 1; diff --git a/perllib/FixMyStreet/Roles/Translatable.pm b/perllib/FixMyStreet/Roles/Translatable.pm new file mode 100644 index 000000000..43cb063d1 --- /dev/null +++ b/perllib/FixMyStreet/Roles/Translatable.pm @@ -0,0 +1,39 @@ +package FixMyStreet::Roles::Translatable; + +use Moo::Role; + +sub translate_around { + my ($orig, $self) = (shift, shift); + my $fallback = $self->$orig(@_); + (my $col = (caller(2))[3]) =~ s/.*:://; + $self->_translate($col, $fallback); +} + +sub translate { + my ($self, $col) = (shift, shift); + my $fallback = $self->$col(@_); + $self->_translate($col, $fallback); +} + +sub _translate { + my ($self, $col, $fallback) = @_; + + my $schema = $self->result_source->schema; + my $table = lc $self->result_source->source_name; + my $id = $self->id; + + if (ref $schema) { + my $translation = $schema->resultset('Translation')->find({ + lang => $schema->lang, + tbl => $table, + object_id => $id, + col => $col + }); + return $translation->msgstr if $translation; + } else { + warn "Can't use translation on this call to $table.$col"; + } + return $fallback; +}; + +1; diff --git a/t/app/controller/reports.t b/t/app/controller/reports.t index dd84d3d2e..9f28a6c89 100644 --- a/t/app/controller/reports.t +++ b/t/app/controller/reports.t @@ -25,6 +25,16 @@ my @edinburgh_problems = $mech->create_problems_for_body(3, $body_edin_id, 'All my @westminster_problems = $mech->create_problems_for_body(5, $body_west_id, 'All reports', { category => 'Graffiti' }); my @fife_problems = $mech->create_problems_for_body(15, $body_fife_id, 'All reports', { category => 'Flytipping' }); +my $west_trans = FixMyStreet::DB->resultset('Translation')->find_or_create({ + tbl => 'body', + object_id => $body_west_id, + col => 'name', + lang => 'de', + msgstr => 'De Westminster' +}); + +ok $west_trans, 'created westminster translation'; + is scalar @westminster_problems, 5, 'correct number of westminster problems created'; is scalar @edinburgh_problems, 3, 'correct number of edinburgh problems created'; is scalar @fife_problems, 15, 'correct number of fife problems created'; @@ -267,4 +277,13 @@ subtest "it lists shortlisted reports" => sub { }; }; +subtest "can use translated body name" => sub { + FixMyStreet::override_config { + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->get_ok('/reports/De Westminster'); + $mech->title_like(qr/Westminster City Council/); + }; +}; + done_testing(); diff --git a/t/roles/translatable.t b/t/roles/translatable.t new file mode 100644 index 000000000..a33b2d9d0 --- /dev/null +++ b/t/roles/translatable.t @@ -0,0 +1,22 @@ +use FixMyStreet::TestMech; +my $mech = FixMyStreet::TestMech->new; + +my $body = FixMyStreet::DB->resultset("Body")->create({ name => 'Dunkirk' }); + +FixMyStreet::DB->resultset("Translation")->create({ + lang => "fr", + tbl => "body", + object_id => $body->id, + col => "name", + msgstr => "Dunkerque", +}); + +is $body->name, "Dunkirk"; + +FixMyStreet::DB->schema->lang("fr"); +is $body->name, "Dunkerque"; + +FixMyStreet::DB->schema->lang("de"); +is $body->name, "Dunkirk"; + +done_testing; |