Skip to content

Commit

Permalink
Extract lang-related logic from the personal panel
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Jul 3, 2018
1 parent b7b2f6e commit dcd7ffd
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 111 deletions.
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 <dubiniuk@owncloud.com>
*
* @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 translation is
$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
11 changes: 0 additions & 11 deletions settings/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@
use OC\Settings\Controller\AuthSettingsController;
use OC\Settings\Controller\CertificateController;
use OC\Settings\Controller\CheckSetupController;
use OC\Settings\Controller\GroupsController;
use OC\Settings\Controller\LegalSettingsController;
use OC\Settings\Controller\LogSettingsController;
use OC\Settings\Controller\MailSettingsController;
use OC\Settings\Controller\UsersController;
use OC\Settings\Middleware\SubadminMiddleware;
use OCP\AppFramework\App;
use OCP\IContainer;
use OCP\Util;
Expand All @@ -62,14 +59,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
128 changes: 32 additions & 96 deletions settings/Panels/Personal/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace OC\Settings\Panels\Personal;

use OC\Helper\LocaleHelper;
use OCP\Settings\ISettings;
use OCP\Template;
use OCP\IGroupManager;
Expand All @@ -42,77 +43,51 @@ class Profile implements ISettings {
/** @var IFactory */
protected $lfactory;

/**
* @var LocaleHelper
*/
private $localeHelper;

/**
* Profile constructor.
*
* @param IConfig $config
* @param IGroupManager $groupManager
* @param IUserSession $userSession
* @param IFactory $lfactory
* @param LocaleHelper $localeHelper
*/
public function __construct(IConfig $config,
IGroupManager $groupManager,
IUserSession $userSession,
IFactory $lfactory) {
IGroupManager $groupManager,
IUserSession $userSession,
IFactory $lfactory,
LocaleHelper $localeHelper
) {
$this->config = $config;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->lfactory = $lfactory;
$this->localeHelper = $localeHelper;
}

public function getPriority() {
return 100;
}

public function getPanel() {
$tmpl = new Template('settings', 'panels/personal/profile');
$activeLangCode = $this->config->getUserValue(
$this->userSession->getUser()->getUID(),
'core',
'lang',
$this->lfactory->findLanguage()
);

// Assign some data
$lang = $this->lfactory->findLanguage();
$userLang = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'core', 'lang', $lang);
$languageCodes = $this->lfactory->findAvailableLanguages();
// array of common languages
$commonLangCodes = [
'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'
];
$languageNames = $this->getLanguageCodes();
$languages= [];
$commonLanguages = [];
foreach ($languageCodes as $lang) {
$l = $this->lfactory->get('settings', $lang);
// TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
$potentialName = (string) $l->t('__language_name__');
if ($l->getLanguageCode() === $lang && \substr($potentialName, 0, 1) !== '_') {//first check if the language name is in the translation file
$ln = ['code'=>$lang, 'name'=> $potentialName];
} elseif (isset($languageNames[$lang])) {
$ln = ['code'=>$lang, 'name'=>$languageNames[$lang]];
} else {//fallback to language code
$ln = ['code'=>$lang, 'name'=>$lang];
}
// put appropriate languages into appropriate arrays, to print them sorted
// used language -> common languages -> divider -> other languages
if ($lang === $userLang) {
$userLang = $ln;
} elseif (\in_array($lang, $commonLangCodes)) {
$commonLanguages[\array_search($lang, $commonLangCodes)]=$ln;
} else {
$languages[]=$ln;
}
}
// if user language is not available but set somehow: show the actual code as name
if (!\is_array($userLang)) {
$userLang = [
'code' => $userLang,
'name' => $userLang,
];
}
\ksort($commonLanguages);
// sort now by displayed language not the iso-code
\usort($languages, function ($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']);
});
list($userLang, $commonLanguages, $languages) = $this->localeHelper->getNormalizedLanguages(
$this->lfactory,
$activeLangCode
);

$tmpl = new Template('settings', 'panels/personal/profile');
$tmpl->assign('email', $this->userSession->getUser()->getEMailAddress());
$tmpl->assign('languages', $languages);
$tmpl->assign('commonlanguages', $commonLanguages);
Expand All @@ -128,45 +103,6 @@ public function getPanel() {
return $tmpl;
}

protected function getLanguageCodes() {
return [
'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)'
];
}

public function getSectionID() {
return 'general';
}
Expand Down
Loading

0 comments on commit dcd7ffd

Please sign in to comment.