Score:0

Add/enable language to Language Module from code

ht flag

I'd like to add/enable a language, just like one would from the admin ui at /admin/config/regional/language/add

but, from code. I've found old options such as locale_add_language which no longer exists, since Drupal 5 or 6. Everything else I can find is in Tests, which seems like a bad route to take and I'm still a little new to Drupal development to understand how to make it happen, or if I should.

I'm building an app that checks the server for content in the device's language and, if it doesn't have it, the server hits Google Translate and stores the translation, and sends it on to the app. The problem is that if that language hasn't been enabled in Drupal, it doesn't store properly and effectively doesn't exist. If I pre-add the language, no problem.

Therefore, unless there's a code solution to adding a language when a new language is encountered, I'd have to enable all languages on the server. I'm not against that, but I'm also wondering if there is overhead on that, or other problems it could cause.

Thank you for any help or suggestions you might have!

Jaypan avatar
de flag
This doesn't sound like a language issue to be honest. I would post your end goal in the Drupal.org forums, and ask for how Drupal users would approach such a problem. You can explain how you did it in Drupal 7 for context, though the Drupal 8 solution is almost definitely very different. Unfortunately, such questions are not allowed on Drupal Answers, but they are welcome on the Drupal forums.
id flag
Do what LanguageAddForm does.
John avatar
ht flag
@Jaypan Obviously being very new here, aside from lurking, may I ask why this question isn't allowed on Drupal Answers? Also, I'm new to Drupal as of 8, so I only found the locale_add_language from digging in the API.
Jaypan avatar
de flag
Drupal Answers has a specific format, one part of which is that open-ended, or opinion-based questions are not allowed. Such questions get closed by admins. I try to point people at the forums in such a case.
John avatar
ht flag
The end of the question did go open-ended, true. Thanks @Jaypan
cn flag
Small (but kind of important) correction: posts get closed by the community, not admins. Stack Exchange sites are collaboratively edited Q+A. Jaypan is right that this post is probably a bit too broad for that format at the moment, as there are several steps, not all straightforward (downloading and installing the various translations in a batch job, for example). I think cilefen's suggestion to look at the existing form code is a good one
Jaypan avatar
de flag
I wasn't saying this question is open-ended, it's fairly specific. However, I don't think it sounds like the solution you are aiming for with your question would be the best way to go about it in Drupal 8+. I had suggested opening a thread on the topic of 'how would you solve this problem in D8+, that I used to solve this way in D7-?', but such a question would be closed on Drupal Answers, while allowed on Drupal forums. Sorry for the confusion.
Score:3
ht flag

Languages can be added the same way as any other entity. The entity type is configurable_language and you can use the LanguageManager to get the proper settings (name, LTR/RTL) from the stock languages in admin.

So, where $lang is the language code sent by the device:

$standard_languages = LanguageManager::getStandardLanguageList();
$label = $standard_languages[$lang][0];
$direction = $standard_languages[$lang][2] ?? ConfigurableLanguage::DIRECTION_LTR;

$entity = \Drupal::entityTypeManager()->getStorage('configurable_language')->create();
$entity->set('id', $lang);
$entity->set('label', $label);
$entity->set('direction', $direction);
$languages = \Drupal::languageManager()->getLanguages(ConfigurableLanguage::STATE_CONFIGURABLE);
$last_language = end($languages);
$entity->setWeight($last_language->getWeight() + 1);
$entity->save();

What threw me off was that you do not enable a pre-defined language. The languages shown in the admin UI come from a variable in json format in Core, and they're added as entities when you enable them.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.