Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit a96ec435 authored by escuriola's avatar escuriola
Browse files

OEL-1160: Add language filter to twig.

parent 35ac9391
No related branches found
No related tags found
1 merge request!71OEL-1160: Header refactor.
......@@ -6,6 +6,7 @@ namespace Drupal\oe_whitelabel_helper\TwigExtension;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\oe_whitelabel_helper\EuropeanUnionLanguages;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
......@@ -35,6 +36,7 @@ class TwigExtension extends AbstractExtension {
public function getFilters(): array {
return [
new TwigFilter('bcl_timeago', [$this, 'bclTimeAgo']),
new TwigFilter('to_internal_language_id', [$this, 'toInternalLanguageId']),
];
}
......@@ -176,4 +178,21 @@ class TwigExtension extends AbstractExtension {
return $block_plugin->build();
}
/**
* Get an internal language ID given its code.
*
* @param string $language_code
* The language code as defined by the W3C language tags document.
*
* @return string
* The internal language ID, or the given language code if none found.
*/
public function toInternalLanguageId($language_code): string {
if (EuropeanUnionLanguages::hasLanguage($language_code)) {
return EuropeanUnionLanguages::getInternalLanguageCode($language_code);
}
return $language_code;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment