aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStruan Donald <struan@exo.org.uk>2017-08-04 17:32:15 +0100
committerMatthew Somerville <matthew-github@dracos.co.uk>2017-08-10 11:30:41 +0100
commit6db4832ad660a942c77a246b366c224c09d66b9f (patch)
tree464be7f9e058866efcd49dc51a2789cfacf72bb0
parentcc3e99fa9878bfb722e61d4a09d94839b152c5b7 (diff)
fetch and create translation methods for translatable
Add a `translation_for` method and an `add_transation` method to Translatable to help hide the details when editing and listing translations. `translation_for` returns all translations if only passed a column or a single translation if passed with a column and a language. `add_translation` will add, or update, a translation if passed in the column, language and string.
-rw-r--r--perllib/FixMyStreet/Roles/Translatable.pm45
-rw-r--r--t/roles/translatable.t10
2 files changed, 55 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/Roles/Translatable.pm b/perllib/FixMyStreet/Roles/Translatable.pm
index cc66f9621..49f10d51a 100644
--- a/perllib/FixMyStreet/Roles/Translatable.pm
+++ b/perllib/FixMyStreet/Roles/Translatable.pm
@@ -49,4 +49,49 @@ sub _translate {
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;
diff --git a/t/roles/translatable.t b/t/roles/translatable.t
index bbe05ce9f..71e39c360 100644
--- a/t/roles/translatable.t
+++ b/t/roles/translatable.t
@@ -51,6 +51,16 @@ is $body->name, "Dunkirk";
is $contact->category_display, "Schlaglöcher";
is $problem->category_display, "Schlaglöcher";
+is $contact->translation_for('category', 'de')->msgstr, "Schlaglöcher";
+is $body->translation_for('name', 'fr')->msgstr, "Dunkerque";
+
+ok $body->add_translation_for('name', 'es', 'Dunkerque');
+
+FixMyStreet::DB->schema->lang("es");
+is $body->name, "Dunkerque";
+
+is $body->translation_for('name')->count, 2;
+
FixMyStreet::override_config {
ALLOWED_COBRANDS => [ 'fiksgatami' ],
}, sub {