Commit 79b9a36c authored by webchick's avatar webchick

Issue #1739994 by pp, das-peter, Gábor Hojtsy: Use the Language class...

Issue #1739994 by pp, das-peter, Gábor Hojtsy: Use the Language class universally instead of stdObj instances.
parent 90d5e41f
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
use Drupal\Core\DrupalKernel; use Drupal\Core\DrupalKernel;
use Drupal\Core\Database\Database; use Drupal\Core\Database\Database;
use Drupal\Core\Database\Install\TaskException; use Drupal\Core\Database\Install\TaskException;
use Drupal\Core\Language\Language;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
...@@ -1325,9 +1326,9 @@ function install_select_language_form($form, &$form_state, $files) { ...@@ -1325,9 +1326,9 @@ function install_select_language_form($form, &$form_state, $files) {
$select_options[$file->langcode] = $file->langcode; $select_options[$file->langcode] = $file->langcode;
} }
// Build a list of languages simulated for browser detection. // Build a list of languages simulated for browser detection.
$languages[$file->langcode] = (object) array( $languages[$file->langcode] = new Language(array(
'langcode' => $file->langcode, 'langcode' => $file->langcode,
); ));
} }
$browser_langcode = language_from_browser($languages); $browser_langcode = language_from_browser($languages);
...@@ -1479,19 +1480,19 @@ function install_import_translations(&$install_state) { ...@@ -1479,19 +1480,19 @@ function install_import_translations(&$install_state) {
if (!isset($standard_languages[$langcode])) { if (!isset($standard_languages[$langcode])) {
// Drupal does not know about this language, so we prefill its values with // Drupal does not know about this language, so we prefill its values with
// our best guess. The user will be able to edit afterwards. // our best guess. The user will be able to edit afterwards.
$language = (object) array( $language = new Language(array(
'langcode' => $langcode, 'langcode' => $langcode,
'name' => $langcode, 'name' => $langcode,
'default' => TRUE, 'default' => TRUE,
); ));
language_save($language); language_save($language);
} }
else { else {
// A known predefined language, details will be filled in properly. // A known predefined language, details will be filled in properly.
$language = (object) array( $language = new Language(array(
'langcode' => $langcode, 'langcode' => $langcode,
'default' => TRUE, 'default' => TRUE,
); ));
language_save($language); language_save($language);
} }
......
...@@ -18,9 +18,9 @@ ...@@ -18,9 +18,9 @@
*/ */
class Language { class Language {
// Properties within the Language are set up as the default language. // Properties within the Language are set up as the default language.
public $name = 'English'; public $name = '';
public $langcode = 'en'; public $langcode = '';
public $direction = 0; public $direction = LANGUAGE_LTR;
public $weight = 0; public $weight = 0;
public $default = FALSE; public $default = FALSE;
public $method_id = NULL; public $method_id = NULL;
...@@ -33,10 +33,23 @@ class Language { ...@@ -33,10 +33,23 @@ class Language {
* The properties used to construct the language. * The properties used to construct the language.
*/ */
public function __construct(array $options = array()) { public function __construct(array $options = array()) {
// Set all the properties for the language. // Set all the provided properties for the language.
foreach ($options as $name => $value) { foreach ($options as $name => $value) {
$this->$name = $value; $this->$name = $value;
} }
// If some options were not set, set sane defaults of a predefined language.
if (!isset($options['name']) || !isset($options['direction'])) {
include_once DRUPAL_ROOT . '/core/includes/standard.inc';
$predefined = standard_language_list();
if (isset($predefined[$this->langcode])) {
if (!isset($options['name'])) {
$this->name = $predefined[$this->langcode][0];
}
if (!isset($options['direction']) && isset($predefined[$this->langcode][2])) {
$this->direction = $predefined[$this->langcode][2];
}
}
}
} }
/** /**
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
use Exception; use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/** /**
* Tests entity translation. * Tests entity translation.
...@@ -60,10 +61,10 @@ function setUp() { ...@@ -60,10 +61,10 @@ function setUp() {
// Create test languages. // Create test languages.
$this->langcodes = array(); $this->langcodes = array();
for ($i = 0; $i < 3; ++$i) { for ($i = 0; $i < 3; ++$i) {
$language = (object) array( $language = new Language(array(
'langcode' => 'l' . $i, 'langcode' => 'l' . $i,
'name' => $this->randomString(), 'name' => $this->randomString(),
); ));
$this->langcodes[$i] = $language->langcode; $this->langcodes[$i] = $language->langcode;
language_save($language); language_save($language);
} }
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
namespace Drupal\field\Tests; namespace Drupal\field\Tests;
use Drupal\Core\Language\Language;
/** /**
* Unit test class for the multilanguage fields logic. * Unit test class for the multilanguage fields logic.
* *
...@@ -55,10 +57,10 @@ function setUp() { ...@@ -55,10 +57,10 @@ function setUp() {
$this->instance = field_read_instance('test_entity', $this->field_name, 'test_bundle'); $this->instance = field_read_instance('test_entity', $this->field_name, 'test_bundle');
for ($i = 0; $i < 3; ++$i) { for ($i = 0; $i < 3; ++$i) {
$language = (object) array( $language = new Language(array(
'langcode' => 'l' . $i, 'langcode' => 'l' . $i,
'name' => $this->randomString(), 'name' => $this->randomString(),
); ));
language_save($language); language_save($language);
} }
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
* Administration functions for language.module. * Administration functions for language.module.
*/ */
use Drupal\Core\Language\Language;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/** /**
...@@ -249,7 +250,7 @@ function language_admin_edit_form($form, &$form_state, $language) { ...@@ -249,7 +250,7 @@ function language_admin_edit_form($form, &$form_state, $language) {
*/ */
function _language_admin_common_controls(&$form, $language = NULL) { function _language_admin_common_controls(&$form, $language = NULL) {
if (!is_object($language)) { if (!is_object($language)) {
$language = new stdClass(); $language = new Language(array('langcode' => NULL, 'name' => NULL,));
} }
if (isset($language->langcode)) { if (isset($language->langcode)) {
$form['langcode_view'] = array( $form['langcode_view'] = array(
...@@ -268,8 +269,8 @@ function _language_admin_common_controls(&$form, $language = NULL) { ...@@ -268,8 +269,8 @@ function _language_admin_common_controls(&$form, $language = NULL) {
'#title' => t('Language code'), '#title' => t('Language code'),
'#maxlength' => 12, '#maxlength' => 12,
'#required' => TRUE, '#required' => TRUE,
'#default_value' => @$language->langcode, '#default_value' => '',
'#disabled' => (isset($language->langcode)), '#disabled' => FALSE,
'#description' => t('Use language codes as <a href="@w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')), '#description' => t('Use language codes as <a href="@w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array('@w3ctags' => 'http://www.w3.org/International/articles/language-tags/')),
); );
} }
...@@ -332,18 +333,14 @@ function language_admin_add_form_submit($form, &$form_state) { ...@@ -332,18 +333,14 @@ function language_admin_add_form_submit($form, &$form_state) {
if ($langcode == 'custom') { if ($langcode == 'custom') {
$langcode = $form_state['values']['langcode']; $langcode = $form_state['values']['langcode'];
// Custom language form. // Custom language form.
$language = (object) array( $language = new Language(array(
'langcode' => $langcode, 'langcode' => $langcode,
'name' => $form_state['values']['name'], 'name' => $form_state['values']['name'],
'direction' => $form_state['values']['direction'], 'direction' => $form_state['values']['direction'],
); ));
} }
else { else {
include_once DRUPAL_ROOT . '/core/includes/standard.inc'; $language = new Language(array('langcode' => $langcode));
$predefined = standard_language_list();
$language = (object) array(
'langcode' => $langcode,
);
} }
// Save the language and inform the user that it happened. // Save the language and inform the user that it happened.
$language = language_save($language); $language = language_save($language);
......
...@@ -208,14 +208,6 @@ function language_process_language_select($element) { ...@@ -208,14 +208,6 @@ function language_process_language_select($element) {
function language_save($language) { function language_save($language) {
$language->is_new = !(bool) db_query_range('SELECT 1 FROM {language} WHERE langcode = :langcode', 0, 1, array(':langcode' => $language->langcode))->fetchField(); $language->is_new = !(bool) db_query_range('SELECT 1 FROM {language} WHERE langcode = :langcode', 0, 1, array(':langcode' => $language->langcode))->fetchField();
// If name was not set, we add a predefined language.
if (!isset($language->name)) {
include_once DRUPAL_ROOT . '/core/includes/standard.inc';
$predefined = standard_language_list();
$language->name = $predefined[$language->langcode][0];
$language->direction = isset($predefined[$language->langcode][2]) ? $predefined[$language->langcode][2] : LANGUAGE_LTR;
}
// Let other modules modify $language before saved. // Let other modules modify $language before saved.
module_invoke_all('language_presave', $language); module_invoke_all('language_presave', $language);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\language\Tests; namespace Drupal\language\Tests;
use Drupal\simpletest\UnitTestBase; use Drupal\simpletest\UnitTestBase;
use Drupal\Core\Language\Language;
/** /**
* Test browser language detection. * Test browser language detection.
...@@ -31,32 +32,32 @@ function testLanguageFromBrowser() { ...@@ -31,32 +32,32 @@ function testLanguageFromBrowser() {
$languages = array( $languages = array(
// In our test case, 'en' has priority over 'en-US'. // In our test case, 'en' has priority over 'en-US'.
'en' => (object) array( 'en' => new Language(array(
'langcode' => 'en', 'langcode' => 'en',
), )),
'en-US' => (object) array( 'en-US' => new Language(array(
'langcode' => 'en-US', 'langcode' => 'en-US',
), )),
// But 'fr-CA' has priority over 'fr'. // But 'fr-CA' has priority over 'fr'.
'fr-CA' => (object) array( 'fr-CA' => new Language(array(
'langcode' => 'fr-CA', 'langcode' => 'fr-CA',
), )),
'fr' => (object) array( 'fr' => new Language(array(
'langcode' => 'fr', 'langcode' => 'fr',
), )),
// 'es-MX' is alone. // 'es-MX' is alone.
'es-MX' => (object) array( 'es-MX' => new Language(array(
'langcode' => 'es-MX', 'langcode' => 'es-MX',
), )),
// 'pt' is alone. // 'pt' is alone.
'pt' => (object) array( 'pt' => new Language(array(
'langcode' => 'pt', 'langcode' => 'pt',
), )),
// Language codes with more then one dash are actually valid. // Language codes with more then one dash are actually valid.
// eh-oh-laa-laa is the official language code of the Teletubbies. // eh-oh-laa-laa is the official language code of the Teletubbies.
'eh-oh-laa-laa' => (object) array( 'eh-oh-laa-laa' => new Language(array(
'langcode' => 'eh-oh-laa-laa', 'langcode' => 'eh-oh-laa-laa',
), )),
); );
$test_cases = array( $test_cases = array(
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\language\Tests; namespace Drupal\language\Tests;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/** /**
* Test UI language negotiation * Test UI language negotiation
...@@ -83,13 +84,13 @@ function testUILanguageNegotiation() { ...@@ -83,13 +84,13 @@ function testUILanguageNegotiation() {
$language_domain = 'example.cn'; $language_domain = 'example.cn';
// Setup the site languages by installing two languages. // Setup the site languages by installing two languages.
$language = (object) array( $language = new Language(array(
'langcode' => $langcode_browser_fallback, 'langcode' => $langcode_browser_fallback,
); ));
language_save($language); language_save($language);
$language = (object) array( $language = new Language(array(
'langcode' => $langcode, 'langcode' => $langcode,
); ));
language_save($language); language_save($language);
// We will look for this string in the admin/config screen to see if the // We will look for this string in the admin/config screen to see if the
...@@ -254,9 +255,9 @@ protected function runTest($test) { ...@@ -254,9 +255,9 @@ protected function runTest($test) {
function testUrlLanguageFallback() { function testUrlLanguageFallback() {
// Add the Italian language. // Add the Italian language.
$langcode_browser_fallback = 'it'; $langcode_browser_fallback = 'it';
$language = (object) array( $language = new Language(array(
'langcode' => $langcode_browser_fallback, 'langcode' => $langcode_browser_fallback,
); ));
language_save($language); language_save($language);
$languages = language_list(); $languages = language_list();
...@@ -282,7 +283,7 @@ function testUrlLanguageFallback() { ...@@ -282,7 +283,7 @@ function testUrlLanguageFallback() {
// Access the front page without specifying any valid URL language prefix // Access the front page without specifying any valid URL language prefix
// and having as browser language preference a non-default language. // and having as browser language preference a non-default language.
$http_header = array("Accept-Language: $langcode_browser_fallback;q=1"); $http_header = array("Accept-Language: $langcode_browser_fallback;q=1");
$language = (object) array('langcode' => ''); $language = new Language(array('langcode' => ''));
$this->drupalGet('', array('language' => $language), $http_header); $this->drupalGet('', array('language' => $language), $http_header);
// Check that the language switcher active link matches the given browser // Check that the language switcher active link matches the given browser
...@@ -302,9 +303,9 @@ function testUrlLanguageFallback() { ...@@ -302,9 +303,9 @@ function testUrlLanguageFallback() {
function testLanguageDomain() { function testLanguageDomain() {
// Add the Italian language. // Add the Italian language.
$langcode = 'it'; $langcode = 'it';
$language = (object) array( $language = new Language(array(
'langcode' => $langcode, 'langcode' => $langcode,
); ));
language_save($language); language_save($language);
$languages = language_list(); $languages = language_list();
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\locale\Tests; namespace Drupal\locale\Tests;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/** /**
* Locale uninstall with English UI functional test. * Locale uninstall with English UI functional test.
...@@ -49,11 +50,11 @@ function setUp() { ...@@ -49,11 +50,11 @@ function setUp() {
function testUninstallProcess() { function testUninstallProcess() {
$locale_module = array('locale', 'language'); $locale_module = array('locale', 'language');
$language = (object) array( $language = new Language(array(
'langcode' => 'fr', 'langcode' => 'fr',
'name' => 'French', 'name' => 'French',
'default' => $this->langcode == 'fr', 'default' => $this->langcode == 'fr',
); ));
language_save($language); language_save($language);
// Reset statically cached language objects. // Reset statically cached language objects.
language(NULL, TRUE); language(NULL, TRUE);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
use Drupal\Component\Gettext\PoStreamWriter; use Drupal\Component\Gettext\PoStreamWriter;
use Drupal\locale\Gettext; use Drupal\locale\Gettext;
use Drupal\locale\PoDatabaseReader; use Drupal\locale\PoDatabaseReader;
use Drupal\Core\Language\Language;
/** /**
...@@ -99,11 +100,9 @@ function locale_translate_import_form_submit($form, &$form_state) { ...@@ -99,11 +100,9 @@ function locale_translate_import_form_submit($form, &$form_state) {
// Add language, if not yet supported. // Add language, if not yet supported.
$language = language_load($form_state['values']['langcode']); $language = language_load($form_state['values']['langcode']);
if (empty($language)) { if (empty($language)) {
include_once DRUPAL_ROOT . '/core/includes/standard.inc'; $language = new Language(array(
$predefined = standard_language_list(); 'langcode' => $form_state['values']['langcode']
$language = (object) array( ));
'langcode' => $form_state['values']['langcode'],
);
$language = language_save($language); $language = language_save($language);
drupal_set_message(t('The language %language has been created.', array('%language' => t($language->name)))); drupal_set_message(t('The language %language has been created.', array('%language' => t($language->name))));
} }
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
namespace Drupal\node\Tests; namespace Drupal\node\Tests;
use Drupal\Core\Language\Language;
/** /**
* Test case to verify node_access functionality for multiple languages. * Test case to verify node_access functionality for multiple languages.
*/ */
...@@ -51,13 +53,13 @@ function setUp() { ...@@ -51,13 +53,13 @@ function setUp() {
*/ */
function testNodeAccess() { function testNodeAccess() {
// Add Hungarian and Catalan. // Add Hungarian and Catalan.
$language = (object) array( $language = new Language(array(
'langcode' => 'hu', 'langcode' => 'hu',
); ));
language_save($language); language_save($language);
$language = (object) array( $language = new Language(array(
'langcode' => 'ca', 'langcode' => 'ca',
); ));
language_save($language); language_save($language);
// Tests the default access provided for a published Hungarian node. // Tests the default access provided for a published Hungarian node.
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\node\Tests; namespace Drupal\node\Tests;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/** /**
* Functional test for multilingual fields. * Functional test for multilingual fields.
...@@ -40,10 +41,10 @@ function setUp() { ...@@ -40,10 +41,10 @@ function setUp() {
$this->drupalLogin($admin_user); $this->drupalLogin($admin_user);
// Add a new language. // Add a new language.
$language = (object) array( $language = new Language(array(
'langcode' => 'it', 'langcode' => 'it',
'name' => 'Italian', 'name' => 'Italian',
); ));
language_save($language); language_save($language);
// Enable URL language detection and selection. // Enable URL language detection and selection.
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
namespace Drupal\openid\Tests; namespace Drupal\openid\Tests;
use Drupal\Core\Language\Language;
/** /**
* Test account registration using Simple Registration and Attribute Exchange. * Test account registration using Simple Registration and Attribute Exchange.
*/ */
...@@ -54,13 +56,13 @@ function testRegisterUserWithEmailVerification() { ...@@ -54,13 +56,13 @@ function testRegisterUserWithEmailVerification() {
// process should pick 'pt' based on the sreg.language being 'pt-BR' // process should pick 'pt' based on the sreg.language being 'pt-BR'
// (and falling back on least specific language given no pt-br available // (and falling back on least specific language given no pt-br available
// locally). // locally).
$language = (object) array( $language = new Language(array(
'langcode' => 'pt', 'langcode' => 'pt',
); ));
language_save($language); language_save($language);
$language = (object) array( $language = new Language(array(
'langcode' => 'pt-pt', 'langcode' => 'pt-pt',
); ));
language_save($language); language_save($language);
// Use a User-supplied Identity that is the URL of an XRDS document. // Use a User-supplied Identity that is the URL of an XRDS document.
...@@ -109,9 +111,9 @@ function testRegisterUserWithoutEmailVerification() { ...@@ -109,9 +111,9 @@ function testRegisterUserWithoutEmailVerification() {
// Save Portuguese, Brazil as an optional language. The process should pick // Save Portuguese, Brazil as an optional language. The process should pick
// 'pt-br' based on the sreg.language later. // 'pt-br' based on the sreg.language later.
$language = (object) array( $language = new Language(array(
'langcode' => 'pt-br', 'langcode' => 'pt-br',
); ));