From 2b89431cca7369aa8f89276c2ece6edeeb359169 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Fri, 11 May 2018 17:53:40 +0200 Subject: [PATCH] TT#36404 restructure form-translation translate forms in NGCP::Panel::Form (the caching module) instead of in the templates. This gives us better control to avoid translating cached forms multiple times. Multiple translations lead to errors due to escaping of special symbols and simply fails from one (non-english) language to another. Change-Id: I234b22cb70dc068530e4a9f241cb9bb5653e1959 --- lib/NGCP/Panel/Controller/Root.pm | 3 +++ lib/NGCP/Panel/Form.pm | 8 ++++++++ lib/NGCP/Panel/Form/CallforwardSourceSet.pm | 2 +- lib/NGCP/Panel/Utils/Preferences.pm | 5 +++++ lib/NGCP/Panel/View/HTML.pm | 6 ------ share/templates/administrator/list.tt | 2 +- share/templates/billing/peaktimes.tt | 1 - share/templates/callrouting/verify.tt | 4 +--- share/templates/customer/details.tt | 6 +++--- share/templates/helpers/ajax_form_modal.tt | 2 +- share/templates/helpers/datatables.tt | 1 - share/templates/helpers/pref_table.tt | 3 --- share/templates/login/login.tt | 2 +- share/templates/ncos/list.tt | 2 +- share/templates/peeringoverview/edit.tt | 2 +- share/templates/reseller/branding.tt | 2 +- share/templates/reseller/details.tt | 2 +- share/templates/rewrite/rules_list.tt | 2 +- share/templates/sound/handles_list.tt | 2 +- share/templates/statistics/list.tt | 2 +- share/templates/subscriber/edit_webpass.tt | 2 +- share/templates/subscriber/master.tt | 2 +- share/templates/subscriber/preferences.tt | 4 ++-- share/templates/subscriber/recoverpassword.tt | 2 +- 24 files changed, 36 insertions(+), 33 deletions(-) diff --git a/lib/NGCP/Panel/Controller/Root.pm b/lib/NGCP/Panel/Controller/Root.pm index 392fbd51d5..8839b65282 100644 --- a/lib/NGCP/Panel/Controller/Root.pm +++ b/lib/NGCP/Panel/Controller/Root.pm @@ -10,6 +10,7 @@ use Scalar::Util qw(blessed); use NGCP::Panel::Utils::DateTime qw(); use NGCP::Panel::Utils::Statistics qw(); use NGCP::Panel::Utils::Admin; +use NGCP::Panel::Form qw(); use DateTime qw(); use Time::HiRes qw(); use DateTime::Format::RFC3339 qw(); @@ -38,6 +39,8 @@ sub auto :Private { $c->response->cookies->{ngcp_panel_lang} = { value => $c->request->params->{lang}, expires => '+3M', }; $c->log->debug("Setting language to ". $c->request->params->{lang}); } + # clear form cache (they need to be properly re-translated) + NGCP::Panel::Form::clear_form_cache(); } if (defined $c->session->{lang}) { $c->languages([$c->session->{lang}, "i-default"]); diff --git a/lib/NGCP/Panel/Form.pm b/lib/NGCP/Panel/Form.pm index 89a1a926f8..bb51e7e415 100644 --- a/lib/NGCP/Panel/Form.pm +++ b/lib/NGCP/Panel/Form.pm @@ -4,6 +4,7 @@ use warnings; use strict; use Module::Load::Conditional qw/can_load/; +use NGCP::Panel::Utils::I18N qw//; my %forms = (); @@ -22,8 +23,15 @@ sub get { return; } $form = $forms{$name} = $name->new(ctx => $c); + # translate form here to prevent multiple translations which leads to errors and doesn't work since the + # source IDs (english) are no longer present + NGCP::Panel::Utils::I18N->translate_form($c, $form); } return $form; } +sub clear_form_cache { + %forms = (); +} + 1; diff --git a/lib/NGCP/Panel/Form/CallforwardSourceSet.pm b/lib/NGCP/Panel/Form/CallforwardSourceSet.pm index 3cbcfbc6cb..49360afae1 100644 --- a/lib/NGCP/Panel/Form/CallforwardSourceSet.pm +++ b/lib/NGCP/Panel/Form/CallforwardSourceSet.pm @@ -58,7 +58,7 @@ has_field 'source.source' => ( wrapper_class => [qw/hfh-rep-field/], element_attr => { rel => ['tooltip'], - title => ['The calling party number in E164 format to match. Shell patterns like 431* or 49123~~~[1-5~~~]67 are possible. ' . + title => ['The calling party number in E164 format to match. Shell patterns like 431* or 49123~[1-5~]67 are possible. ' . 'Use "anonymous" to match suppressed numbers.'], }, ); diff --git a/lib/NGCP/Panel/Utils/Preferences.pm b/lib/NGCP/Panel/Utils/Preferences.pm index aabad02c41..7fcb207f13 100644 --- a/lib/NGCP/Panel/Utils/Preferences.pm +++ b/lib/NGCP/Panel/Utils/Preferences.pm @@ -5,6 +5,7 @@ use Sipwise::Base; use Data::Validate::IP qw/is_ipv4 is_ipv6/; use NGCP::Panel::Form::Preferences; use NGCP::Panel::Utils::Generic qw(:all); +use NGCP::Panel::Utils::I18N qw//; use NGCP::Panel::Utils::Sems; use constant _DYNAMIC_PREFERENCE_PREFIX => '__'; @@ -354,6 +355,10 @@ sub create_preference_form { }], }); $form->create_structure([$c->stash->{preference_meta}->attribute]); + # we have to translate this form separately since it bypasses caching in NGCP::Panel::Form + if ( $c->stash->{preference_meta}->attribute !~ '(ncos|sound_set|emergency_mapping_container)$' ) { + NGCP::Panel::Utils::I18N->translate_form($c, $form); + } my $posted = ($c->request->method eq 'POST'); $form->process( diff --git a/lib/NGCP/Panel/View/HTML.pm b/lib/NGCP/Panel/View/HTML.pm index b676b9ee6c..ecaf3c2273 100644 --- a/lib/NGCP/Panel/View/HTML.pm +++ b/lib/NGCP/Panel/View/HTML.pm @@ -17,14 +17,8 @@ __PACKAGE__->config( URI::Escape::uri_unescape(@_); }, }, - expose_methods => [qw/translate_form/], ); -sub translate_form { - my $self = shift; - NGCP::Panel::Utils::I18N->translate_form(@_); -} - =head1 NAME NGCP::Panel::View::HTML - TT View for NGCP::Panel diff --git a/share/templates/administrator/list.tt b/share/templates/administrator/list.tt index 2cc225bd5f..df57517c97 100644 --- a/share/templates/administrator/list.tt +++ b/share/templates/administrator/list.tt @@ -35,7 +35,7 @@ IF api_modal_flag; PROCESS "helpers/modal.tt"; modal_header(m.name = "API key"); - translate_form(helper.form_object).render; + helper.form_object.render; modal_footer(); modal_script(m.close_target = helper.close_target); END; diff --git a/share/templates/billing/peaktimes.tt b/share/templates/billing/peaktimes.tt index 3b42acc55f..3c37dc6b2d 100644 --- a/share/templates/billing/peaktimes.tt +++ b/share/templates/billing/peaktimes.tt @@ -82,7 +82,6 @@ [% PROCESS "helpers/modal.tt"; modal_header(m.name = weekday.name); - form = translate_form(form); -%]