Permalink
Browse files

Replace languagedb logic with intl component

  • Loading branch information...
flack committed Oct 27, 2017
1 parent 196984a commit ea58c4ec890193ec1a68631ffff3347aa277228a
View
@@ -46,6 +46,7 @@
"symfony/finder": "~2.8",
"symfony/form": "~2.8",
"symfony/http-foundation": "~2.5",
"symfony/intl": "~2.8",
"symfony/options-resolver": "~2.6",
"symfony/security-csrf": "~2.5",
"symfony/translation": "~2.3",

This file was deleted.

Oops, something went wrong.
@@ -327,7 +327,6 @@ class midcom_config implements arrayaccess
'cron_hour_minutes' => 30,
// I18n Subsystem configuration
'i18n_language_db_path' => 'file:/midcom/config/language_db.inc',
'i18n_available_languages' => null,
'i18n_fallback_language' => 'en',
@@ -74,13 +74,6 @@ class midcom_services_i18n_l10n
*/
private $_component_name;
/**
* A copy of the language DB from i18n.
*
* @var Array
*/
private $_language_db;
/**
* Fallback language, in case the selected language is not available.
*
@@ -135,7 +128,6 @@ public function __construct($library, $database)
$this->_library = $library . $database;
$this->_component_name = $library;
$this->_language_db = midcom::get()->i18n->get_language_db();
$this->_fallback_language = midcom::get()->i18n->get_fallback_language();
if (!isset(self::$_localedb[$this->_library])) {
@@ -297,22 +289,13 @@ private function _check_for_language($lang)
/**
* Set output language.
*
* This will set the character encoding to the language's default
* encoding and will also set the system locale to the one
* specified in the language database.
*
* This is usually set through midcom_services_i18n.
*
* @param string $lang Language code.
* @see midcom_services_i18n::set_language()
*/
public function set_language($lang)
{
if (!array_key_exists($lang, $this->_language_db)) {
debug_add("Language {$lang} not found in the language database.", MIDCOM_LOG_ERROR);
return false;
}
$this->_language = $lang;
}
@@ -434,14 +417,4 @@ public function get_stringdb($language)
}
return $this->_stringdb[$language];
}
public function get_language_name($lang)
{
return $this->_language_db[$lang]['enname'];
}
public function get_languages()
{
return $this->_language_db;
}
}
@@ -6,6 +6,8 @@
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
use Symfony\Component\Intl\Intl;
/**
* This is a basic MidCOM Service which provides an interfaces to the
* various I18n facilities of MidCOM.
@@ -15,9 +17,6 @@
* of language data using HTTP Content-Negotiation along with a cookie-based
* fallback.
*
* A good deal of major languages are predefined, see the snippet
* /lib/midcom/config/language_db.inc for details.
*
* This class is able to run independently from midcom_application
* due to the fact that it is used in the cache_hit code.
*
@@ -31,13 +30,6 @@
*/
class midcom_services_i18n
{
/**
* The language database, loaded from /lib/midcom/config/language_db.inc
*
* @var Array
*/
private $_language_db;
/**
* Fallback language, in case the selected language is not available.
*
@@ -64,15 +56,13 @@ class midcom_services_i18n
*
* @var string
*/
private $_current_charset;
private $_current_charset = 'utf-8';
/**
* Initialize the available i18n framework by determining the desired language
* from these different sources: HTTP Content Negotiation, Client side language cookie.
*
* It uses the MidCOM Language database now located at
* /lib/midcom/config/language_db.inc for any decisions. Its two
* parameters set the default language in case that none is supplied
* Its two parameters set the default language in case that none is supplied
* via HTTP Content Negotiation or through Cookies.
*
* The default language set on startup is currently hardcoded to 'en',
@@ -84,10 +74,6 @@ class midcom_services_i18n
*/
public function __construct()
{
$path = midcom::get()->config->get('i18n_language_db_path');
$data = midcom_helper_misc::get_snippet_content($path);
$this->_language_db = midcom_helper_misc::parse_config($data);
$this->_fallback_language = midcom::get()->config->get('i18n_fallback_language');
$this->set_language($this->_fallback_language);
@@ -108,8 +94,7 @@ private function _set_startup_langs()
if ($http_langs = $this->_read_http_negotiation()) {
foreach (array_keys($http_langs) as $name) {
if (array_key_exists($name, $this->_language_db)) {
$this->set_language($name);
if ($this->set_language($name)) {
break;
}
}
@@ -217,23 +202,19 @@ public function set_charset($charset)
*/
public function set_language($lang)
{
if (!array_key_exists($lang, $this->_language_db)) {
debug_add("Language {$lang} not found in the language database.", MIDCOM_LOG_ERROR);
if (Intl::getLocaleBundle()->getLocaleName($lang) === null) {
debug_add("Language {$lang} not found.", MIDCOM_LOG_ERROR);
return false;
}
$this->_current_language = $lang;
$this->_current_charset = strtolower($this->_language_db[$lang]['encoding']);
/**
* NOTE: setlocale can take an array of locales as value, it will use
* the first name valid for the system
*/
setlocale(LC_ALL, $this->_language_db[$lang]['locale']);
setlocale(LC_ALL, $lang);
foreach ($this->_obj_l10n as $object) {
$object->set_language($lang);
}
return true;
}
/**
@@ -249,16 +230,6 @@ public function set_fallback_language($lang)
}
}
/**
* Returns the language database.
*
* @return Array
*/
public function get_language_db()
{
return $this->_language_db;
}
/**
* Returns the current language code
*
@@ -392,12 +363,11 @@ private function _load_l10n_db($component, $database)
*/
public function list_languages()
{
$languages = [];
foreach ($this->_language_db as $identifier => $language) {
if ($language['enname'] != $language['localname']) {
$languages[$identifier] = "{$language['enname']} ({$language['localname']})";
} else {
$languages[$identifier] = $language['enname'];
$languages = Intl::getLanguageBundle()->getLanguageNames('en');
foreach ($languages as $identifier => &$language) {
$localname = Intl::getLanguageBundle()->getLanguageName($identifier, null, $identifier);
if ($localname != $language) {
$language = $language . ' (' . $localname . ')';
}
}
return $languages;

0 comments on commit ea58c4e

Please sign in to comment.