Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Stable10] Add UI for switching public mail notification language #32004

Merged
merged 3 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions lib/private/Helper/LocaleHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php
/**
* @author Viktar Dubiniuk <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Helper;

use OCP\L10N\IFactory;

class LocaleHelper {
/**
* @var string[]
*/
private $commonLanguages = [
'en',
'es',
'fr',
'de',
'de_DE',
'ja',
'ar',
'ru',
'nl',
'it',
'pt_BR',
'pt_PT',
'da',
'fi_FI',
'nb_NO',
'sv',
'tr',
'zh_CN',
'ko'
];

/**
* @var string[]
*/
private $languageCodes = [
'el' => 'Ελληνικά',
'en' => 'English',
'fa' => 'فارسى',
'fi_FI' => 'Suomi',
'hi' => 'हिन्दी',
'id' => 'Bahasa Indonesia',
'lb' => 'Lëtzebuergesch',
'ms_MY' => 'Bahasa Melayu',
'nb_NO' => 'Norwegian Bokmål',
'pt_BR' => 'Português brasileiro',
'pt_PT' => 'Português',
'ro' => 'română',
'sr@latin' => 'Srpski',
'sv' => 'Svenska',
'hu_HU' => 'Magyar',
'hr' => 'Hrvatski',
'ar' => 'العربية',
'lv' => 'Latviešu',
'mk' => 'македонски',
'uk' => 'Українська',
'vi' => 'Tiếng Việt',
'zh_TW' => '正體中文(臺灣)',
'af_ZA' => 'Afrikaans',
'bn_BD' => 'Bengali',
'ta_LK' => 'தமிழ்',
'zh_HK' => '繁體中文(香港)',
'is' => 'Icelandic',
'ka_GE' => 'Georgian for Georgia',
'ku_IQ' => 'Kurdish Iraq',
'si_LK' => 'Sinhala',
'be' => 'Belarusian',
'ka' => 'Kartuli (Georgian)',
'my_MM' => 'Burmese - MYANMAR',
'ur_PK' => 'Urdu (Pakistan)'
];

/**
* Get available languages split to the current, common and the rest
*
* @param IFactory $langFactory
* @param string $activeLangCode
*
* @return array
*/
public function getNormalizedLanguages(IFactory $langFactory, $activeLangCode) {
$userLang = null;
$commonLanguages = [];
$languages = [];

$availableCodes = $langFactory->findAvailableLanguages();
foreach ($availableCodes as $languageCode) {
$l = $langFactory->get('settings', $languageCode);

// TRANSLATORS this is a self-name of your language for the language switcher
$endonym = (string)$l->t('__language_name__');
// Check if the language name is in the translation file
// Fallback to hardcoded language name if it isn't
$languageName = ($l->getLanguageCode() === $languageCode
&& \substr($endonym, 0, 1) !== '_'
) ? $endonym
: $this->getLanguageNameByCode($languageCode);

$ln = [
'code' => $languageCode,
'name' => $languageName
];
$languageWeight = $this->getCommonLanguageWeight($languageCode);
if ($languageCode === $activeLangCode) {
$userLang = $ln;
} elseif ($languageWeight !== false) {
$commonLanguages[$languageWeight] = $ln;
} else {
$languages[] = $ln;
}
}

// if user language is not available but set somehow
// show the actual code as name
if ($userLang === null) {
$userLang = [
'code' => $activeLangCode,
'name' => $activeLangCode,
];
}

\ksort($commonLanguages);
\usort($languages, [$this, 'compareLanguagesByName']);

return [$userLang, $commonLanguages, $languages];
}

/**
* Get common language weight or false if language is not common
*
* @param string $languageCode
*
* @return false|int
*/
public function getCommonLanguageWeight($languageCode) {
return \array_search($languageCode, $this->commonLanguages);
}

/**
* Get language name by code
*
* @param string $code
*
* @return string
*/
public function getLanguageNameByCode($code) {
return (isset($this->languageCodes[$code]))
? $this->languageCodes[$code]
: $code;
}

/**
* Compare language arrays by name
* both arrays should have 'code' and 'name' keys
*
* @param string[] $a
* @param string[] $b
*
* @return int
*/
protected function compareLanguagesByName($a, $b) {
if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
// If a doesn't have a name, but b does, list b before a
return 1;
}
if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
// If a does have a name, but b doesn't, list a before b
return -1;
}
// Otherwise compare the names
return \strcmp($a['name'], $b['name']);
}
}
4 changes: 3 additions & 1 deletion lib/private/Settings/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ public function getBuiltInPanel($className) {
$this->config,
$this->groupManager,
$this->userSession,
$this->lfactory),
$this->lfactory,
new \OC\Helper\LocaleHelper()
),
LegacyPersonal::class => new LegacyPersonal($this->helper),
Clients::class => new Clients($this->config, $this->defaults),
Version::class => new Version(),
Expand Down
16 changes: 0 additions & 16 deletions settings/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
use OC\Settings\Controller\LegalSettingsController;
use OC\Settings\Controller\LogSettingsController;
use OC\Settings\Controller\MailSettingsController;
use OC\Settings\Controller\SecuritySettingsController;
use OC\Settings\Controller\UsersController;
use OC\Settings\Middleware\SubadminMiddleware;
use OCP\AppFramework\App;
Expand All @@ -64,14 +63,6 @@ public function __construct(array $urlParams=[]) {

$container = $this->getContainer();

$container->registerService('Profile', function (IContainer $c) {
return new \OC\Settings\Panels\Personal\Profile(
$c->query('Config'),
$c->query('GroupManager'),
$c->query('ServerContainer')->getURLGenerator()
);
});

/**
* Controllers
*/
Expand Down Expand Up @@ -129,13 +120,6 @@ public function __construct(array $urlParams=[]) {
$c->query('UserId')
);
});
$container->registerService('SecuritySettingsController', function (IContainer $c) {
return new SecuritySettingsController(
$c->query('AppName'),
$c->query('Request'),
$c->query('Config')
);
});
$container->registerService('CertificateController', function (IContainer $c) {
return new CertificateController(
$c->query('AppName'),
Expand Down
34 changes: 34 additions & 0 deletions settings/Panels/Admin/FileSharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace OC\Settings\Panels\Admin;

use OC\Helper\LocaleHelper;
use OC\Settings\Panels\Helper;
use OCP\IConfig;
use OCP\Settings\ISettings;
Expand All @@ -47,6 +48,38 @@ public function getPriority() {
}

public function getPanel() {
$this->lfactory = \OC::$server->getL10NFactory();
$activeLangCode = $this->config->getAppValue(
'core',
'shareapi_public_notification_lang',
'owner'
);
$this->localeHelper = new LocaleHelper();
list($userLang, $commonLanguages, $languages) = $this->localeHelper->getNormalizedLanguages(
$this->lfactory,
$activeLangCode
);

// Allow reset to the defaults when mail notification is sent in the lang of owner
if ($userLang['code'] === "owner") {
$userLang['name'] = $this->l->t("Owner language");
} else {
\array_push(
$commonLanguages,
[
'code' => 'owner',
'name' => $this->l->t("Owner language")
]
);
}

$selector = new Template('settings', 'language');
$selector->assign('selectName', 'shareapi_public_notification_lang');
$selector->assign('selectId', 'shareapiPublicNotificationLang');
$selector->assign('activelanguage', $userLang);
$selector->assign('commonlanguages', $commonLanguages);
$selector->assign('languages', $languages);

$template = new Template('settings', 'panels/admin/filesharing');
$template->assign('allowResharing', $this->config->getAppValue('core', 'shareapi_allow_resharing', 'yes'));
$template->assign('shareAPIEnabled', $this->config->getAppValue('core', 'shareapi_enabled', 'yes'));
Expand All @@ -57,6 +90,7 @@ public function getPanel() {
$template->assign('enforceLinkPasswordWriteOnly', $this->config->getAppValue('core', 'shareapi_enforce_links_password_write_only', 'no'));
$template->assign('shareDefaultExpireDateSet', $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no'));
$template->assign('allowPublicMailNotification', $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no'));
$template->assign('publicMailNotificationLang', $selector->fetchPage());
$template->assign('allowSocialShare', $this->config->getAppValue('core', 'shareapi_allow_social_share', 'yes'));
$template->assign('allowGroupSharing', $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes'));
$template->assign('onlyShareWithGroupMembers', $this->helper->shareWithGroupMembersOnly());
Expand Down
Loading