[Rt-commit] rt branch, 4.4/dashboard-language, created. rt-4.4.0rc2-23-g9e7da56

Shawn Moore shawn at bestpractical.com
Fri May 13 13:38:28 EDT 2016


The branch, 4.4/dashboard-language has been created
        at  9e7da569747c0d635467460888645d83c56894cc (commit)

- Log -----------------------------------------------------------------
commit 415264de9234ab08f0c66d29e4c1d076678b8a40
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Tue Nov 10 18:00:53 2015 +0000

    Add language selector for dashboard subscriptions

diff --git a/lib/RT/Dashboard/Mailer.pm b/lib/RT/Dashboard/Mailer.pm
index 1786c06..fae8c27 100644
--- a/lib/RT/Dashboard/Mailer.pm
+++ b/lib/RT/Dashboard/Mailer.pm
@@ -109,6 +109,7 @@ sub MailDashboards {
             my $recipients_groups = $recipients->{Groups};
 
             my @emails;
+            my %recipient_language;
 
             # add users' emails to email list
             for my $user_id (@{ $recipients_users || [] }) {
@@ -117,6 +118,7 @@ sub MailDashboards {
                 next unless $user->id;
 
                 push @emails, $user->EmailAddress;
+                $recipient_language{$user->EmailAddress} = $user->Lang;
             }
 
             # add emails for every group's members
@@ -128,12 +130,19 @@ sub MailDashboards {
                 my $users = $group->UserMembersObj;
                 while (my $user = $users->Next) {
                     push @emails, $user->EmailAddress;
+                    $recipient_language{$user->EmailAddress} = $user->Lang;
                 }
             }
 
             my $email_success = 0;
             for my $email (uniq @emails) {
                 eval {
+                    my $lang = $subscription->SubValue('Language')
+                            || $recipient_language{$email}
+                            || 'en';
+
+                    $currentuser->{'LangHandle'} = RT::I18N->get_handle($lang);
+
                     $self->SendDashboard(
                         %args,
                         CurrentUser  => $currentuser,
diff --git a/share/html/Dashboards/Render.html b/share/html/Dashboards/Render.html
index 0306077..37ee4fc 100644
--- a/share/html/Dashboards/Render.html
+++ b/share/html/Dashboards/Render.html
@@ -55,6 +55,15 @@
 <& /Elements/Tabs &>
 % }
 
+% # honor the chosen language for just the dashboard content
+% my $original_handle;
+% if ($SubscriptionObj->id) {
+%     if (my $lang = $SubscriptionObj->SubValue('Language')) {
+%         $original_handle = $session{'CurrentUser'}->{'LangHandle'};
+%         $session{'CurrentUser'}->{'LangHandle'} = RT::I18N->get_handle($lang);
+%     }
+% }
+
 % $m->callback(CallbackName => 'BeforeTable', Dashboard => $Dashboard, show_cb => $show_cb);
 
 <table class="dashboard" id="dashboard-<%$id%>">
@@ -91,6 +100,10 @@
 </html>
 % }
 
+% # restore the original language for anything else on the page
+% if ($original_handle) {
+%     $session{'CurrentUser'}->{'LangHandle'} = $original_handle;
+% }
 <%INIT>
 
 
diff --git a/share/html/Dashboards/Subscription.html b/share/html/Dashboards/Subscription.html
index cb45f1b..73f2557 100644
--- a/share/html/Dashboards/Subscription.html
+++ b/share/html/Dashboards/Subscription.html
@@ -165,6 +165,15 @@
 (<%$timezone%>)
 </td></tr>
 <tr><td class="label">
+<&|/l&>Language</&>:
+</td><td class="value">
+<& /Elements/SelectLang,
+    Name => 'Language',
+    Default => $fields{'Language'},
+    ShowNullOption => 1,
+ &>
+</td></tr>
+<tr><td class="label">
 <&|/l&>Rows</&>:
 </td><td class="value">
 <select name="Rows">
@@ -228,6 +237,7 @@ my %fields = (
     Recipients  => { Users => [], Groups => [] },
     Fow         => 1,
     Counter     => 0,
+    Language    => '',
 );
 
 # update any fields with the values from the subscription object
@@ -360,6 +370,8 @@ $Hour        => undef
 $Dow         => undef
 $Dom         => undef
 $Rows        => undef
+$Recipient   => undef
+$Language    => undef
 
 $UserField => undef
 $UserOp => undef
diff --git a/t/mail/dashboards.t b/t/mail/dashboards.t
index 3d004ae..6f803ed 100644
--- a/t/mail/dashboards.t
+++ b/t/mail/dashboards.t
@@ -91,6 +91,8 @@ my ($dashboard_id, $subscription_id) = get_dash_sub_ids();
 sub produces_dashboard_mail_ok { # {{{
     my %args = @_;
     my $subject = delete $args{Subject};
+    my $body_like = delete $args{BodyLike};
+    my $body_unlike = delete $args{BodyUnlike};
 
     local $Test::Builder::Level = $Test::Builder::Level + 1;
 
@@ -107,8 +109,20 @@ sub produces_dashboard_mail_ok { # {{{
     is($mail->head->get('X-RT-Dashboard-Subscription-Id'), "$subscription_id\n");
 
     my $body = $mail->bodyhandle->as_string;
-    like($body, qr{My dashboards});
+    like($body, qr{My dashboards}) if !$body_like && !$body_unlike;
     like($body, qr{<a href="http://[^/]+/Dashboards/\d+/Testing!">Testing!</a>});
+
+    if ($body_like) {
+        for my $re (ref($body_like) eq 'ARRAY' ? @$body_like : $body_like) {
+            ok($body =~ $re, "body should match $re");
+        }
+    }
+
+    if ($body_unlike) {
+        for my $re (ref($body_unlike) eq 'ARRAY' ? @$body_unlike : $body_unlike) {
+            ok($body !~ $re, "body should not match $re");
+        }
+    }
 } # }}}
 
 sub produces_no_dashboard_mail_ok { # {{{
@@ -318,6 +332,7 @@ create_dashboard($baseurl, $m);
 create_subscription($baseurl, $m,
     Frequency => 'monthly',
     Hour => '06:00',
+    Language => 'fr',
 );
 
 ($dashboard_id, $subscription_id) = get_dash_sub_ids();
@@ -326,8 +341,10 @@ $good_time = 1291201200;        # dec 1
 $bad_time = $good_time - 86400; # day before (i.e. different month)
 
 produces_dashboard_mail_ok(
-    Time    => $good_time,
-    Subject =>  "[example.com] a Monthly b Testing! c\n",
+    Time       => $good_time,
+    Subject    => "[example.com] a Mensuel b Testing! c\n",
+    BodyLike   => qr/Mes tableaux de bord/,
+    BodyUnlike => qr/My dashboards/,
 );
 
 produces_no_dashboard_mail_ok(

commit 9e7da569747c0d635467460888645d83c56894cc
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Fri May 13 17:16:21 2016 +0000

    Add config option EmailDashboardLanguageOrder

diff --git a/etc/RT_Config.pm.in b/etc/RT_Config.pm.in
index 35ac0a2..b683629 100644
--- a/etc/RT_Config.pm.in
+++ b/etc/RT_Config.pm.in
@@ -760,6 +760,33 @@ mailed dashboards.
 
 Set(@EmailDashboardRemove, ());
 
+=item C<@EmailDashboardLanguageOrder>
+
+A list that specifies which language to use for dashboard subscription email.
+There are several special keys:
+
+* _subscription: the language chosen on the dashboard subscription page
+* _recipient: the recipient's language, as chosen on their "About Me" page
+* _subscriber: the subscriber's language, as chosen on their "About Me" page
+
+The first key that produces a value is used for the email. Be aware that users
+may not actually have a language set on their "About Me" page, since RT falls
+back to the language their web browser specifies (and of course in a scheduled
+email dashboard, there is no web browser).
+
+You may also include a specific language as a fallback when there is no
+language specified otherwise. Using a specific language never fails to produce
+a value, so subsequent values in the list will never be considered.
+
+By default, RT examines the subscription, then the recipient, then subscriber,
+then finally falls back to English.
+
+See also L</@LexiconLanguages>.
+
+=cut
+
+Set(@EmailDashboardLanguageOrder, qw(_subscription _recipient _subscriber en));
+
 =back
 
 
diff --git a/lib/RT/Config.pm b/lib/RT/Config.pm
index ded8e81..427598f 100644
--- a/lib/RT/Config.pm
+++ b/lib/RT/Config.pm
@@ -795,6 +795,7 @@ our %META;
     },
     GnuPGOptions => { Type => 'HASH' },
     ReferrerWhitelist => { Type => 'ARRAY' },
+    EmailDashboardLanguageOrder  => { Type => 'ARRAY' },
     WebPath => {
         PostLoadCheck => sub {
             my $self  = shift;
diff --git a/lib/RT/Dashboard/Mailer.pm b/lib/RT/Dashboard/Mailer.pm
index fae8c27..0b43a1e 100644
--- a/lib/RT/Dashboard/Mailer.pm
+++ b/lib/RT/Dashboard/Mailer.pm
@@ -94,6 +94,8 @@ sub MailDashboards {
         my $currentuser = RT::CurrentUser->new;
         $currentuser->LoadByName($user->Name);
 
+        my $subscriber_lang = $user->Lang;
+
         # look through this user's subscriptions, are any supposed to be generated
         # right now?
         for my $subscription ($user->Attributes->Named('Subscription')) {
@@ -137,9 +139,42 @@ sub MailDashboards {
             my $email_success = 0;
             for my $email (uniq @emails) {
                 eval {
-                    my $lang = $subscription->SubValue('Language')
-                            || $recipient_language{$email}
-                            || 'en';
+                    my $lang;
+                    for my $langkey (RT->Config->Get('EmailDashboardLanguageOrder')) {
+                        if ($langkey eq '_subscription') {
+                            if ($lang = $subscription->SubValue('Language')) {
+                                $RT::Logger->debug("Using subscription's specified language '$lang'");
+                                last;
+                            }
+                        }
+                        elsif ($langkey eq '_recipient') {
+                            if ($lang = $recipient_language{$email}) {
+                                $RT::Logger->debug("Using recipient's preferred language '$lang'");
+                                last;
+                            }
+                        }
+                        elsif ($langkey eq '_subscriber') {
+                            if ($lang = $subscriber_lang) {
+                                $RT::Logger->debug("Using subscriber's preferred language '$lang'");
+                                last;
+                            }
+                        }
+                        else { # specific language name
+                            $lang = $langkey;
+                            $RT::Logger->debug("Using EmailDashboardLanguageOrder fallback language '$lang'");
+                            last;
+                        }
+                    }
+
+                    # use English as the absolute fallback. Though the config
+                    # lets you specify a site-specific fallback, it also lets
+                    # you not specify a fallback, and we don't want to
+                    # accidentally reuse whatever language the previous
+                    # recipient happened to have
+                    if (!$lang) {
+                        $RT::Logger->debug("Using RT's fallback language 'en'. You may specify a different fallback language in your config with EmailDashboardLanguageOrder.");
+                        $lang = 'en';
+                    }
 
                     $currentuser->{'LangHandle'} = RT::I18N->get_handle($lang);
 
diff --git a/t/mail/dashboards.t b/t/mail/dashboards.t
index 6f803ed..fde81c6 100644
--- a/t/mail/dashboards.t
+++ b/t/mail/dashboards.t
@@ -325,6 +325,7 @@ RT::Test->clean_caught_mails;
 RT::Test->stop_server;
 
 RT->Config->Set('EmailDashboardRemove' => ());
+RT->Config->Set('EmailDashboardLanguageOrder' => qw(_subscription _recipient _subscriber fr));
 RT->Config->Set('DashboardAddress' => 'root');
 ($baseurl, $m) = RT::Test->started_ok;
 $m->login;
@@ -332,7 +333,47 @@ create_dashboard($baseurl, $m);
 create_subscription($baseurl, $m,
     Frequency => 'monthly',
     Hour => '06:00',
-    Language => 'fr',
+);
+
+($dashboard_id, $subscription_id) = get_dash_sub_ids();
+
+$good_time = 1291201200;        # dec 1
+
+produces_dashboard_mail_ok(
+    Time       => $good_time,
+    Subject    => "[example.com] a Mensuel b Testing! c\n",
+    BodyLike   => qr/Mes tableaux de bord/,
+    BodyUnlike => qr/My dashboards/,
+);
+
+
+
+ at mails = RT::Test->fetch_caught_mails;
+is(@mails, 0, "no mail leftover");
+
+$m->no_warnings_ok;
+RT::Test->stop_server;
+RT->Config->Set('DashboardSubject' => 'a %s b %s c');
+RT->Config->Set('DashboardAddress' => 'dashboard at example.com');
+RT->Config->Set('EmailDashboardRemove' => (qr/My dashboards/, "Testing!"));
+($baseurl, $m) = RT::Test->started_ok;
+
+delete_dashboard($dashboard_id);
+
+RT::Test->clean_caught_mails;
+
+RT::Test->stop_server;
+
+RT->Config->Set('EmailDashboardRemove' => ());
+RT->Config->Set('EmailDashboardLanguage' => 'ja');
+RT->Config->Set('DashboardAddress' => 'root');
+($baseurl, $m) = RT::Test->started_ok;
+$m->login;
+create_dashboard($baseurl, $m);
+create_subscription($baseurl, $m,
+    Frequency => 'monthly',
+    Hour => '06:00',
+    Language => 'fr', # overrides EmailDashboardLanguage
 );
 
 ($dashboard_id, $subscription_id) = get_dash_sub_ids();

-----------------------------------------------------------------------


More information about the rt-commit mailing list