From 4afffd01bd018db0b1cf575a352f3023f2646d1c Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 10 Mar 2015 03:11:53 +0100 Subject: [PATCH] Added basic language support for sending out invitations: - This allows for example sending German mails from an English instance - Added some checked type-hints (all around language stuff) Some code improvement: - array $foo=null and then an is_null($foo) makes no sense, better use array $foo=array() directly. - onTitle()'s parameter $paths now always must be an array Signed-off-by: Roland Haeder --- actions/invite.php | 7 ++-- lib/docfile.php | 32 +++++++++---------- lib/inviteform.php | 12 +++++++ .../EmailRegistrationPlugin.php | 4 +-- plugins/EmailReminder/EmailReminderPlugin.php | 3 +- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/actions/invite.php b/actions/invite.php index c3464efb54..0deb7a76ee 100644 --- a/actions/invite.php +++ b/actions/invite.php @@ -74,6 +74,7 @@ class InviteAction extends Action $bestname = $profile->getBestName(); $sitename = common_config('site', 'name'); $personal = $this->trimmed('personal'); + $language = $this->trimmed('language'); $addresses = explode("\n", $this->trimmed('addresses')); foreach ($addresses as $email) { @@ -128,7 +129,7 @@ class InviteAction extends Action } catch (NoSuchUserException $e) { // If email was not known, let's send an invite! $this->sent[] = $email; - $this->sendInvitation($email, $user, $personal); + $this->sendInvitation($email, $user, $personal, $language); } } @@ -248,7 +249,7 @@ class InviteAction extends Action } } - function sendInvitation($email, $user, $personal) + function sendInvitation($email, $user, $personal, $language) { $profile = $user->getProfile(); $bestname = $profile->getBestName(); @@ -284,7 +285,7 @@ class InviteAction extends Action $title = (empty($personal)) ? 'invite' : 'invitepersonal'; // @todo FIXME: i18n issue. - $inviteTemplate = DocFile::forTitle($title, DocFile::mailPaths()); + $inviteTemplate = DocFile::forTitle($title, DocFile::mailPaths(), $language); $body = $inviteTemplate->toHTML(array('inviter' => $bestname, 'inviterurl' => $profile->profileurl, diff --git a/lib/docfile.php b/lib/docfile.php index 316aebb872..583f7ebc9b 100644 --- a/lib/docfile.php +++ b/lib/docfile.php @@ -55,12 +55,8 @@ class DocFile $this->filename = $filename; } - static function forTitle($title, $paths) + static function forTitle($title, array $paths, $language=null) { - if (!is_array($paths)) { - $paths = array($paths); - } - $filename = null; if (Event::handle('StartDocFileForTitle', array($title, &$paths, &$filename))) { @@ -80,7 +76,7 @@ class DocFile } if (!empty($lang) || !empty($def)) { - $filename = self::negotiateLanguage($lang, $def); + $filename = self::negotiateLanguage($lang, $def, $language); break; } } @@ -95,12 +91,8 @@ class DocFile } } - function toHTML(array $args=null) + function toHTML(array $args=array()) { - if (is_null($args)) { - $args = array(); - } - if (empty($this->contents)) { $this->contents = file_get_contents($this->filename); } @@ -114,7 +106,7 @@ class DocFile INSTALLDIR.'/doc-src/'); $site = GNUsocial::currentSite(); - + if (!empty($site)) { array_unshift($paths, INSTALLDIR.'/local/doc-src/'.$site.'/'); } @@ -128,7 +120,7 @@ class DocFile INSTALLDIR.'/mail-src/'); $site = GNUsocial::currentSite(); - + if (!empty($site)) { array_unshift($paths, INSTALLDIR.'/local/mail-src/'.$site.'/'); } @@ -136,12 +128,20 @@ class DocFile return $paths; } - static function negotiateLanguage($filenames, $defaultFilename=null) + private static function negotiateLanguage(array $filenames, $defaultFilename=null, $language = null) { - // XXX: do this better - + // Default is current language $langcode = common_language(); + // Is a language set? + if (!empty($language)) { + // And is it valid? + if (common_valid_language($language)) { + // Use this as language (e.g. from form) + $langcode = strval($language); + } + } + foreach ($filenames as $filename) { if (preg_match('/\.'.$langcode.'$/', $filename)) { return $filename; diff --git a/lib/inviteform.php b/lib/inviteform.php index 364ca75b9b..be4dc163b0 100644 --- a/lib/inviteform.php +++ b/lib/inviteform.php @@ -93,6 +93,7 @@ class InviteForm extends Form function formData() { $this->out->elementStart('ul', 'form_data'); + $this->out->elementStart('li'); $this->out->textarea( 'addresses', @@ -103,6 +104,7 @@ class InviteForm extends Form _('Addresses of friends to invite (one per line).') ); $this->out->elementEnd('li'); + $this->out->elementStart('li'); $this->out->textarea( // TRANS: Field label for a personal message to send to invitees. @@ -112,6 +114,16 @@ class InviteForm extends Form _('Optionally add a personal message to the invitation.') ); $this->out->elementEnd('li'); + + $language = common_language(); + + $this->out->elementStart('li'); + $this->out->dropdown('language', _('Language'), + // TRANS: Tooltip for dropdown list label in form for profile settings. + get_nice_language_list(), _('Preferred language.'), + false, $language); + $this->out->elementEnd('li'); + $this->out->elementEnd('ul'); } diff --git a/plugins/EmailRegistration/EmailRegistrationPlugin.php b/plugins/EmailRegistration/EmailRegistrationPlugin.php index 330f72b15b..77ac763bd0 100644 --- a/plugins/EmailRegistration/EmailRegistrationPlugin.php +++ b/plugins/EmailRegistration/EmailRegistrationPlugin.php @@ -72,7 +72,7 @@ class EmailRegistrationPlugin extends Plugin $dir = dirname(__FILE__); // @todo FIXME: i18n issue. - $docFile = DocFile::forTitle($title, $dir.'/doc-src/'); + $docFile = DocFile::forTitle($title, array($dir . '/doc-src/')); if (!empty($docFile)) { $output = $docFile->toHTML(); @@ -163,7 +163,7 @@ class EmailRegistrationPlugin extends Plugin mail_send($recipients, $headers, $body); } - function onEndDocFileForTitle($title, $paths, &$filename) + function onEndDocFileForTitle($title, array $paths, &$filename) { if ($title == 'confirmemailreg' && empty($filename)) { $filename = dirname(__FILE__).'/mail-src/'.$title; diff --git a/plugins/EmailReminder/EmailReminderPlugin.php b/plugins/EmailReminder/EmailReminderPlugin.php index a415865ab9..9ba3d125e1 100644 --- a/plugins/EmailReminder/EmailReminderPlugin.php +++ b/plugins/EmailReminder/EmailReminderPlugin.php @@ -77,7 +77,7 @@ class EmailReminderPlugin extends Plugin return true; } - function onEndDocFileForTitle($title, $paths, &$filename) + function onEndDocFileForTitle($title, array $paths, &$filename) { if (empty($filename)) { $filename = dirname(__FILE__) . '/mail-src/' . $title; @@ -127,6 +127,7 @@ class EmailReminderPlugin extends Plugin * Send a real live email reminder * * @todo This would probably be better as two or more sep functions + * @todo Add language support? * * @param string $type type of reminder * @param mixed $object Confirm_address or Invitation object -- 2.39.5