0

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!

7
  • 2
    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.
    – Jaypan
    Nov 16, 2022 at 15:58
  • 1
    Do what LanguageAddForm does.
    – cilefen
    Nov 16, 2022 at 16:09
  • @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.
    – John
    Nov 16, 2022 at 16:14
  • 1
    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.
    – Jaypan
    Nov 16, 2022 at 16:18
  • 1
    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
    – Clive
    Nov 16, 2022 at 16:32

1 Answer 1

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.