Spin off from #1738374: Provide a cue to enable the language switcher when adding a language.

If you create a new language object and only define it with a langcode then the name and direction will fall back to English. So if you create a language object like so:
$language = new Language(array('langcode' => $langcode));

The language name and direction will fall back to "English", however if you pass along a NULL ofr the name like so:
$language = new Language(array('langcode' => $langcode, 'name' => NULL));

We end up with the correct name for that language after we save it via language_save();

Comments

pp’s picture

Assigned: Unassigned » pp
pp’s picture

Status: Active » Needs review
StatusFileSize
new13.74 KB

first try

Status: Needs review » Needs work

The last submitted patch, 1739994_use_Language_object.patch, failed testing.

penyaskito’s picture

+++ b/core/modules/system/lib/Drupal/system/Tests/System/DateTimeTest.phpundefined
@@ -170,10 +170,10 @@ class DateTimeTest extends WebTestBase {
+    $language = new Language(array(

Needs to declare that will "use \Drupal\Core\Language", so test fails.

pp’s picture

Status: Needs work » Needs review
StatusFileSize
new16.7 KB

@penyaskito thank's

I rerolled the patch.

pp’s picture

Status: Needs review » Needs work

the patch is wrong

pp’s picture

Status: Needs work » Needs review
StatusFileSize
new16.05 KB

reroll the patch

Status: Needs review » Needs work

The last submitted patch, 1739994_use_Language_object-6.patch, failed testing.

pp’s picture

Status: Needs work » Needs review
StatusFileSize
new16.83 KB

tricky custom language... ehh..

Status: Needs review » Needs work

The last submitted patch, 1739994_use_Language_object-9.patch, failed testing.

pp’s picture

Status: Needs work » Needs review
StatusFileSize
new17.04 KB

If name isn't present, it set to English... eh..

zserno’s picture

Status: Needs review » Needs work

Nice patch. My remarks:

+++ b/core/modules/language/language.admin.incundefined
@@ -348,9 +349,11 @@ function language_admin_add_predefined_form_submit($form, &$form_state) {
+    'direction' => isset($predefined[$langcode][2]) && $predefined[$langcode][2] == LANGUAGE_RTL ? LANGUAGE_RTL : 0,

LANGUAGE_LTR should be used instead of 0 for better readability.

+++ b/core/modules/locale/locale.bulk.incundefined
@@ -101,9 +102,11 @@ function locale_translate_import_form_submit($form, &$form_state) {
+        'direction' => isset($predefined[$form_state['values']['langcode']][2]) ? $predefined[$form_state['values']['langcode']][2] : 0,

Similarly, LANGUAGE_LTR should be used here too.

Also note that #1280996: New language_select element type for form API got committed in the meantime which added a few occurrences of the old stdObj instances. A quick git grep 'language = (object) array' reveals those.

pp’s picture

Status: Needs work » Needs review
StatusFileSize
new19.78 KB

Thank you for your review! I totally agree with you.

I rerolled the patch.

Status: Needs review » Needs work
Issue tags: -D8MI, -sprint, -language-base

The last submitted patch, 1739994_use_Language_object-13.patch, failed testing.

pp’s picture

Status: Needs work » Needs review
Issue tags: +D8MI, +sprint, +language-base
das-peter’s picture

I just gave this a review. There where two cases where the replacement ended up in new Language((object) array( - that's fixed in the new patch.
Further I found other locations on which I think the replacement was missing.
Please see the interdiff to just see the changes between the patches #13 & #16.

Status: Needs review » Needs work

The last submitted patch, 1739994-interdiff-13-16.diff, failed testing.

das-peter’s picture

Status: Needs work » Needs review
StatusFileSize
new22.34 KB

Ouch, looks like use Drupal\Core\Language\Language; was missing on two locations.
Updated patch attached.

pp’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/modules/language/lib/Drupal/language/Tests/LanguageBrowserDetectionUnitTest.php
@@ -8,6 +8,7 @@
+use Drupal\Core\Language\Language;
 
 /**
  * Test browser language detection.
@@ -31,32 +32,32 @@ class LanguageBrowserDetectionUnitTest extends UnitTestBase {

@@ -31,32 +32,32 @@ class LanguageBrowserDetectionUnitTest extends UnitTestBase {
 
     $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',
-      ),
+      )),
     );

It isn't correct, because you not set the name property, but test run well and Language class will be refactored.

Thank's for your update. I think it is good for committing.

das-peter’s picture

Assigned: pp » das-peter
Status: Reviewed & tested by the community » Needs work

I'm still working on some enhancements Gábor an I have discussed.

das-peter’s picture

Assigned: das-peter » Unassigned
Status: Needs work » Needs review
StatusFileSize
new4.73 KB
new24.81 KB

Moved some logic from language_save() into the Language constructor.
It takes now care of adding sane default properties if such are missing.
Helped to clean up some code and makes the usage of the language object easier.

pp’s picture

Status: Needs review » Reviewed & tested by the community

It is looking good for me.

pp’s picture

das-peter’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new568 bytes
new24.9 KB

Gábor and I just had another discussion about the possibility of having a complete empty Language object by default.
Earlier this wasn't possible due some needs from the dependency injection container. But since this uses now the LanguageHandler the need for having English as predefined properties of the Language object is obsolete:

$container->register('language_manager', 'Drupal\Core\Language\LanguageManager')
      ->addArgument(new Reference('request'))
      ->setScope('request');

The LanguageHandler always sets appropriate properties - thus we can get rid of the currently set defaults. :)
Summary: Another change to the patch...

pp’s picture

oh, yes 'English' and 'en' must be deleted here.

I think about to use $langcode = NULL instead of $langcode = '' but I think it isn't necessary.

The patch is good.

pp’s picture

Status: Needs review » Reviewed & tested by the community

tests run well.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Wow, great work! This is so much easier to read now.

Unfortunately it no longer applies. :( Can we get a quick re-roll?

gábor hojtsy’s picture

Status: Needs work » Needs review
StatusFileSize
new1012 bytes
new24.15 KB

Quick reroll. Also the part that did not apply in interdiff form.

gábor hojtsy’s picture

Status: Needs review » Reviewed & tested by the community
webchick’s picture

Status: Reviewed & tested by the community » Fixed

Hooray!! Thanks a ton!

Committed and pushed to 8.x.

gábor hojtsy’s picture

Issue tags: -sprint

Added a change notice node for this at http://drupal.org/node/1776678 - don't think it merits a changelog.txt entry, so moving off of sprint.

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

updating summary