I have setup language and translation in Drupal8 that affects my URL for example


What I need to do if find out which country is being accessed so I can add a variable to the twig theme layer

This is the hook that is working fine

 * Implements hook_preprocess_HOOK() for menu.html.twig.
function pnc_preprocess_menu(&$variables) {
    $code_to_access_language_url = 'this is a test';
    $variables['access_translation'] = $code_to_access_language_url;
    //print $code_to_access_language_url;exit;

I have found that under kint() there a lot of public methods for translation but I am not sure how I access them or which one to access. I know I could regex the url, but there has to be a cleaner way to do it?

  • $languages = \Drupal::languageManager()->getLanguages();
    – purencool
    Commented Oct 3, 2016 at 7:38

1 Answer 1


I found the answer myself.

if($variables['menu_name'] == 'navigation') {     
  $langcode = \Drupal::languageManager()->getCurrentLanguage()->getName();
  // Add the logic here.

Your Answer

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

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