Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Custom languages can be added to any given install by using the "Custom language..." option at the bottom of the language select list on the "Add language" form.
If you add a language using this feature, you will get an added configured language which will be available in the switch language block an which will be listed in /admin/config/regional/language
But how to add a custom language without adding a language for the site?
This is the goal of this feature request.
Comment | File | Size | Author |
---|---|---|---|
#10 | add_custom_disabled_languages-2848356-10.patch | 3.28 KB | jacktonkin |
Comments
Comment #2
DuneBLHere is a patch that solve this issue:
1-Most of the patch is made of a new ConfigEntityBase named "CustomLanguage": it will store the added languages and it will provide forms to edit, update or delete the languages.
I will also provide a list view of the added language at
admin/config/languagefield
2-The added languages are merged with the "core" languages with those lines in LanguageItem.php:
a)Adding the custom languages at construct time (stored in $customLanguages):
b)Add custom languages in the options (one line added)
c)Fix the getNativeName function (one line added)
3-The patch adds also a permission to view, edit, create or delete the custom language: "CRUD languages for languagefield"
4-The patch adds a menu item under "system.admin_config_regional" to see the custom language list
5-The patch adds a schema.yml file to allow the creation of custom languages in the form of yml text files
Let me know if you think that this patch goes in a right direction.
Note: I am not sure the exact process to make this patch working, but here is my idea:
1-Clear the cache (drush cr)
2-Update entities (drush entup)
Comment #3
DuneBLI have added 2 more changes in this patch:
1-Add a getLanguage() public function in LanguageItem class
This function is getting the language "the normal way" through
createFromLangcode
, and if the language code exists in thecustomLanguages
list, it will update the label property with the name of the custom language.2-Update the FieldFormatter
viewValue
function:I change:
$language = ConfigurableLanguage::createFromLangcode($langcode);
into
In other word, I used the newly added function to get the language object
Comment #4
johnvI have been working on this.
Needs some more work;
- filter_xss on user input.
- no new permission, bu re-use existing field permission. (or not)
- the edit form needs work. The machine name is not correct. I have de-duplicate ID and langcode.
Comment #5
johnvComment #7
DuneBLI found a small error in:
We should add
Before the foreach in the case there isn't any language enoded.
Comment #9
johnvFixed. Thanks.
Can you check my remarks in #4?
Comment #10
jacktonkin CreditAttribution: jacktonkin at ISSUP commentedThis is so much better than attempting to override LanguageManager::getStandardLanguageList like I was doing before. Thank-you!
In response to your remarks in #4:
'administer languages'
for the permission since language module is a dependency. Custom languages could be used with fields on any kind of entity so inheriting field permissions would add too much complexity. To be honest I think it's fine for a new config entity to have its own 'administer' permission. Perhaps consider renaming to'administer custom languages'
?The attached patch copies liberally from
Drupal\language\Form\LanguageFormBase
to address 1 & 3. Marking as needs review for those, I'd be happy to re-roll if you want to change the permission used.Comment #13
johnv#11 commits the patch from #10.
#12 reuses core's 'administer languages' permission.