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 @@
use Drupal\Core\DrupalKernel;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Install\TaskException;
use Drupal\Core\Language\Language;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
......@@ -1325,9 +1326,9 @@ function install_select_language_form($form, &$form_state, $files) {
$select_options[$file->langcode] = $file->langcode;
}
// Build a list of languages simulated for browser detection.
$languages[$file->langcode] = (object) array(
$languages[$file->langcode] = new Language(array(
'langcode' => $file->langcode,
);
));
}
$browser_langcode = language_from_browser($languages);
......@@ -1479,19 +1480,19 @@ function install_import_translations(&$install_state) {
if (!isset($standard_languages[$langcode])) {
// Drupal does not know about this language, so we prefill its values with
// our best guess. The user will be able to edit afterwards.
$language = (object) array(
$language = new Language(array(
'langcode' => $langcode,
'name' => $langcode,
'default' => TRUE,
);
));
language_save($language);
}
else {
// A known predefined language, details will be filled in properly.
$language = (object) array(
$language = new Language(array(
'langcode' => $langcode,
'default' => TRUE,
);
));
language_save($language);
}
......
......@@ -18,9 +18,9 @@
*/
class Language {
// Properties within the Language are set up as the default language.
public $name = 'English';
public $langcode = 'en';
public $direction = 0;
public $name = '';
public $langcode = '';
public $direction = LANGUAGE_LTR;
public $weight = 0;
public $default = FALSE;
public $method_id = NULL;
......@@ -33,10 +33,23 @@ class Language {
* The properties used to construct the language.
*/
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) {
$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 @@
use Exception;
use InvalidArgumentException;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/**
* Tests entity translation.
......@@ -60,10 +61,10 @@ function setUp() {
// Create test languages.
$this->langcodes = array();
for ($i = 0; $i < 3; ++$i) {
$language = (object) array(
$language = new Language(array(
'langcode' => 'l' . $i,
'name' => $this->randomString(),
);
));
$this->langcodes[$i] = $language->langcode;
language_save($language);
}
......
......@@ -7,6 +7,8 @@
namespace Drupal\field\Tests;
use Drupal\Core\Language\Language;
/**
* Unit test class for the multilanguage fields logic.
*
......@@ -55,10 +57,10 @@ function setUp() {
$this->instance = field_read_instance('test_entity', $this->field_name, 'test_bundle');
for ($i = 0; $i < 3; ++$i) {
$language = (object) array(
$language = new Language(array(
'langcode' => 'l' . $i,
'name' => $this->randomString(),
);
));
language_save($language);
}
}
......
......@@ -5,6 +5,7 @@
* Administration functions for language.module.
*/
use Drupal\Core\Language\Language;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
......@@ -249,7 +250,7 @@ function language_admin_edit_form($form, &$form_state, $language) {
*/
function _language_admin_common_controls(&$form, $language = NULL) {
if (!is_object($language)) {
$language = new stdClass();
$language = new Language(array('langcode' => NULL, 'name' => NULL,));
}
if (isset($language->langcode)) {
$form['langcode_view'] = array(
......@@ -268,8 +269,8 @@ function _language_admin_common_controls(&$form, $language = NULL) {
'#title' => t('Language code'),
'#maxlength' => 12,
'#required' => TRUE,
'#default_value' => @$language->langcode,
'#disabled' => (isset($language->langcode)),
'#default_value' => '',
'#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/')),
);
}
......@@ -332,18 +333,14 @@ function language_admin_add_form_submit($form, &$form_state) {
if ($langcode == 'custom') {
$langcode = $form_state['values']['langcode'];
// Custom language form.
$language = (object) array(
$language = new Language(array(
'langcode' => $langcode,
'name' => $form_state['values']['name'],
'direction' => $form_state['values']['direction'],
);
));
}
else {
include_once DRUPAL_ROOT . '/core/includes/standard.inc';
$predefined = standard_language_list();
$language = (object) array(
'langcode' => $langcode,
);
$language = new Language(array('langcode' => $langcode));
}
// Save the language and inform the user that it happened.
$language = language_save($language);
......
......@@ -208,14 +208,6 @@ function language_process_language_select($element) {
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();
// 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.
module_invoke_all('language_presave', $language);
......
......@@ -8,6 +8,7 @@
namespace Drupal\language\Tests;
use Drupal\simpletest\UnitTestBase;
use Drupal\Core\Language\Language;
/**
* Test browser language detection.
......@@ -31,32 +32,32 @@ function testLanguageFromBrowser() {
$languages = array(
// In our test case, 'en' has priority over 'en-US'.
'en' => (object) array(
'en' => new Language(array(
'langcode' => 'en',
),
'en-US' => (object) array(
)),
'en-US' => new Language(array(
'langcode' => 'en-US',
),
)),
// But 'fr-CA' has priority over 'fr'.
'fr-CA' => (object) array(
'fr-CA' => new Language(array(
'langcode' => 'fr-CA',
),
'fr' => (object) array(
)),
'fr' => new Language(array(
'langcode' => 'fr',
),
)),
// 'es-MX' is alone.
'es-MX' => (object) array(
'es-MX' => new Language(array(
'langcode' => 'es-MX',
),
)),
// 'pt' is alone.
'pt' => (object) array(
'pt' => new Language(array(
'langcode' => 'pt',
),
)),
// 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' => (object) array(
'eh-oh-laa-laa' => new Language(array(
'langcode' => 'eh-oh-laa-laa',
),
)),
);
$test_cases = array(
......
......@@ -8,6 +8,7 @@
namespace Drupal\language\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/**
* Test UI language negotiation
......@@ -83,13 +84,13 @@ function testUILanguageNegotiation() {
$language_domain = 'example.cn';
// Setup the site languages by installing two languages.
$language = (object) array(
$language = new Language(array(
'langcode' => $langcode_browser_fallback,
);
));
language_save($language);
$language = (object) array(
$language = new Language(array(
'langcode' => $langcode,
);
));
language_save($language);
// We will look for this string in the admin/config screen to see if the
......@@ -254,9 +255,9 @@ protected function runTest($test) {
function testUrlLanguageFallback() {
// Add the Italian language.
$langcode_browser_fallback = 'it';
$language = (object) array(
$language = new Language(array(
'langcode' => $langcode_browser_fallback,
);
));
language_save($language);
$languages = language_list();
......@@ -282,7 +283,7 @@ function testUrlLanguageFallback() {
// Access the front page without specifying any valid URL language prefix
// and having as browser language preference a non-default language.
$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);
// Check that the language switcher active link matches the given browser
......@@ -302,9 +303,9 @@ function testUrlLanguageFallback() {
function testLanguageDomain() {
// Add the Italian language.
$langcode = 'it';
$language = (object) array(
$language = new Language(array(
'langcode' => $langcode,
);
));
language_save($language);
$languages = language_list();
......
......@@ -8,6 +8,7 @@
namespace Drupal\locale\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/**
* Locale uninstall with English UI functional test.
......@@ -49,11 +50,11 @@ function setUp() {
function testUninstallProcess() {
$locale_module = array('locale', 'language');
$language = (object) array(
$language = new Language(array(
'langcode' => 'fr',
'name' => 'French',
'default' => $this->langcode == 'fr',
);
));
language_save($language);
// Reset statically cached language objects.
language(NULL, TRUE);
......
......@@ -8,6 +8,7 @@
use Drupal\Component\Gettext\PoStreamWriter;
use Drupal\locale\Gettext;
use Drupal\locale\PoDatabaseReader;
use Drupal\Core\Language\Language;
/**
......@@ -99,11 +100,9 @@ function locale_translate_import_form_submit($form, &$form_state) {
// Add language, if not yet supported.
$language = language_load($form_state['values']['langcode']);
if (empty($language)) {
include_once DRUPAL_ROOT . '/core/includes/standard.inc';
$predefined = standard_language_list();
$language = (object) array(
'langcode' => $form_state['values']['langcode'],
);
$language = new Language(array(
'langcode' => $form_state['values']['langcode']
));
$language = language_save($language);
drupal_set_message(t('The language %language has been created.', array('%language' => t($language->name))));
}
......
......@@ -7,6 +7,8 @@
namespace Drupal\node\Tests;
use Drupal\Core\Language\Language;
/**
* Test case to verify node_access functionality for multiple languages.
*/
......@@ -51,13 +53,13 @@ function setUp() {
*/
function testNodeAccess() {
// Add Hungarian and Catalan.
$language = (object) array(
$language = new Language(array(
'langcode' => 'hu',
);
));
language_save($language);
$language = (object) array(
$language = new Language(array(
'langcode' => 'ca',
);
));
language_save($language);
// Tests the default access provided for a published Hungarian node.
......
......@@ -8,6 +8,7 @@
namespace Drupal\node\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/**
* Functional test for multilingual fields.
......@@ -40,10 +41,10 @@ function setUp() {
$this->drupalLogin($admin_user);
// Add a new language.
$language = (object) array(
$language = new Language(array(
'langcode' => 'it',
'name' => 'Italian',
);
));
language_save($language);
// Enable URL language detection and selection.
......
......@@ -7,6 +7,8 @@
namespace Drupal\openid\Tests;
use Drupal\Core\Language\Language;
/**
* Test account registration using Simple Registration and Attribute Exchange.
*/
......@@ -54,13 +56,13 @@ function testRegisterUserWithEmailVerification() {
// process should pick 'pt' based on the sreg.language being 'pt-BR'
// (and falling back on least specific language given no pt-br available
// locally).
$language = (object) array(
$language = new Language(array(
'langcode' => 'pt',
);
));
language_save($language);
$language = (object) array(
$language = new Language(array(
'langcode' => 'pt-pt',
);
));
language_save($language);
// Use a User-supplied Identity that is the URL of an XRDS document.
......@@ -109,9 +111,9 @@ function testRegisterUserWithoutEmailVerification() {
// Save Portuguese, Brazil as an optional language. The process should pick
// 'pt-br' based on the sreg.language later.
$language = (object) array(
$language = new Language(array(
'langcode' => 'pt-br',
);
));
language_save($language);
// Use a User-supplied Identity that is the URL of an XRDS document.
......@@ -246,13 +248,13 @@ function testRegisterUserWithAXButNoSREG() {
// Save Portuguese and Portuguese, Portugal as optional languages. The
// process should pick 'pt-pt' as the more specific language.
$language = (object) array(
$language = new Language(array(
'langcode' => 'pt',
);
));
language_save($language);
$language = (object) array(
$language = new Language(array(
'langcode' => 'pt-pt',
);
));
language_save($language);
// Use a User-supplied Identity that is the URL of an XRDS document.
......
......@@ -7,6 +7,8 @@
namespace Drupal\search\Tests;
use Drupal\Core\Language\Language;
/**
* Tests entities with multilingual fields.
*/
......@@ -33,15 +35,15 @@ function setUp() {
parent::setUp();
// Add two new languages.
$language = (object) array(
$language = new Language(array(
'langcode' => 'hu',
'name' => 'Hungarian',
);
));
language_save($language);
$language = (object) array(
$language = new Language(array(
'langcode' => 'sv',
'name' => 'Swedish',
);
));
language_save($language);
// Make the body field translatable.
......
......@@ -8,6 +8,7 @@
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/**
* Tests drupal_http_request().
......@@ -133,10 +134,10 @@ function testDrupalHTTPRequestHeaders() {
$this->assertEqual($request->headers['content-language'], 'en', t('Content-Language HTTP header is English.'));
// Add French language.
$language = (object) array(
$language = new Language(array(
'langcode' => 'fr',
'name' => 'French',
);
));
language_save($language);
// Request front page in French and check for matching Content-language.
......
......@@ -35,16 +35,16 @@ public static function getInfo() {
*/
function testLanguageSelectElementOptions() {
// Add some languages.
$language = (object) array(
$language = new Language(array(
'langcode' => 'aaa',
'name' => $this->randomName(),
);
));
language_save($language);
$language = (object) array(
$language = new Language(array(
'langcode' => 'bbb',
'name' => $this->randomName(),
);
));
language_save($language);
$this->drupalGet('form-test/language_select');
......
......@@ -8,6 +8,7 @@
namespace Drupal\system\Tests\System;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
/**
* Tests generic date and time handling capabilities of Drupal.
......@@ -170,10 +171,10 @@ function testDateFormatStorage() {
$this->assertFalse($format, 'Unlocalized date format resides not in localized table.');
// Enable German language
$language = (object) array(
$language = new Language(array(
'langcode' => 'de',
'default' => TRUE,
);
));
language_save($language);
$date_format = array(
......
......@@ -7,6 +7,8 @@
namespace Drupal\taxonomy\Tests;
use Drupal\Core\Language\Language;
/**
* Tests for the language feature on taxonomy terms.
*/
......@@ -35,16 +37,16 @@ function setUp() {
function testTermLanguage() {
// Add first some custom languages.
$language = (object) array(
$language = new Language(array(
'langcode' => 'aa',
'name' => $this->randomName(),
);
));
language_save($language);
$language = (object) array(
$language = new Language(array(
'langcode' => 'bb',
'name' => $this->randomName(),
);
));
language_save($language);
// Add a term.
......
......@@ -7,6 +7,8 @@
namespace Drupal\taxonomy\Tests;
use Drupal\Core\Language\Language;
/**
* Tests for the language feature on vocabularies.
*/
......@@ -32,16 +34,16 @@ function setUp() {
function testVocabularyLanguage() {
// Add first some custom languages.
$language = (object) array(
$language = new Language(array(
'langcode' => 'aa',
'name' => $this->randomName(),
);
));
language_save($language);
$language = (object) array(
$language = new Language(array(
'langcode' => 'bb',
'name' => $this->randomName(),
);
));
language_save($language);
$this->drupalGet('admin/structure/taxonomy/add');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment