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

Skip to content
Snippets Groups Projects
Unverified Commit bb0bc7a1 authored by Francesco SARDARA's avatar Francesco SARDARA
Browse files

OEL-494: The union languages helper class is now available in the parent theme.

parent d6aaabe5
No related branches found
No related tags found
4 merge requests!121OEL-1421: Fix cosmetic changes.,!116OEL-1394: Merge 1.x into EPIC-1293-Project, align with new version of card:search pattern,!115Upate the EPIC-1293-project,!108OEL-494: Style document paragraph.
<?php
declare(strict_types = 1);
namespace Drupal\oe_whitelabel_helper;
/**
* Helper class storing European Union languages information.
*
* @see https://github.com/openeuropa/oe_theme/blob/HEAD/modules/oe_theme_helper/src/EuropeanUnionLanguages.php
*/
class EuropeanUnionLanguages {
/**
* List of European Union languages.
*
* Each entry includes:
*
* - The language name in English
* - The language name in its native form
* - The internal language ID, used on URLs, asset names, etc.
*
* @var array
*/
protected static $languages = [
'bg' => ['Bulgarian', 'български', 'bg'],
'cs' => ['Czech', 'čeština', 'cs'],
'da' => ['Danish', 'dansk', 'da'],
'de' => ['German', 'Deutsch', 'de'],
'et' => ['Estonian', 'eesti', 'et'],
'el' => ['Greek', 'ελληνικά', 'el'],
'en' => ['English', 'English', 'en'],
'es' => ['Spanish', 'español', 'es'],
'fr' => ['French', 'français', 'fr'],
'ga' => ['Irish', 'Gaeilge', 'ga'],
'hr' => ['Croatian', 'hrvatski', 'hr'],
'it' => ['Italian', 'italiano', 'it'],
'lt' => ['Lithuanian', 'lietuvių', 'lt'],
'lv' => ['Latvian', 'latviešu', 'lv'],
'hu' => ['Hungarian', 'magyar', 'hu'],
'mt' => ['Maltese', 'Malti', 'mt'],
'nl' => ['Dutch', 'Nederlands', 'nl'],
'pl' => ['Polish', 'polski', 'pl'],
'pt-pt' => ['Portuguese', 'português', 'pt'],
'ro' => ['Romanian', 'română', 'ro'],
'sk' => ['Slovak', 'slovenčina', 'sk'],
'sl' => ['Slovenian', 'slovenščina', 'sl'],
'fi' => ['Finnish', 'suomi', 'fi'],
'sv' => ['Swedish', 'svenska', 'sv'],
];
/**
* Returns a list of language data.
*
* This is the data that is expected to be returned by the overridden language
* manager as supplied by the OpenEuropa Multilingual module.
*
* @return array
* An array with language codes as keys, and English and native language
* names as values.
*/
public static function getLanguageList(): array {
return self::$languages;
}
/**
* Assert whether the given language is a European Union one.
*
* @param string $language_code
* The language code as defined by the W3C language tags document.
*
* @return bool
* Whereas the given language is a European Union one.
*/
public static function hasLanguage(string $language_code): bool {
return isset(self::$languages[$language_code]);
}
/**
* Get the language name in English given its W3C code.
*
* @param string $language_code
* The language code as defined by the W3C language tags document.
*
* @return string
* The language name in English if any, an empty string otherwise.
*/
public static function getEnglishLanguageName(string $language_code): string {
return self::$languages[$language_code][0] ?? '';
}
/**
* Get the native language name given its W3C code.
*
* @param string $language_code
* The language code as defined by the W3C language tags document.
*
* @return string
* The native language name if any, an empty string otherwise.
*/
public static function getNativeLanguageName(string $language_code): string {
return self::$languages[$language_code][1] ?? '';
}
/**
* Get the internal language code given its W3C code.
*
* Internal language codes may differ from the standard ones.
*
* @param string $language_code
* The language code as defined by the W3C language tags document.
*
* @return string
* The internal language code if any, an empty string otherwise.
*/
public static function getInternalLanguageCode(string $language_code): string {
return self::$languages[$language_code][2] ?? '';
}
}
......@@ -6,7 +6,6 @@ namespace Drupal\oe_whitelabel_helper\TwigExtension;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\oe_whitelabel_helper\EuropeanUnionLanguages;
use Drupal\Core\Url;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
......@@ -37,7 +36,6 @@ class TwigExtension extends AbstractExtension {
public function getFilters(): array {
return [
new TwigFilter('bcl_timeago', [$this, 'bclTimeAgo']),
new TwigFilter('to_internal_language_id', [$this, 'toInternalLanguageId']),
];
}
......@@ -184,21 +182,4 @@ 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(string $language_code): string {
if (EuropeanUnionLanguages::hasLanguage($language_code)) {
return EuropeanUnionLanguages::getInternalLanguageCode($language_code);
}
return $language_code;
}
}
......@@ -8,7 +8,7 @@
declare(strict_types = 1);
use Drupal\Component\Utility\Html;
use Drupal\oe_whitelabel_helper\EuropeanUnionLanguages;
use Drupal\oe_bootstrap_theme_helper\EuropeanUnionLanguages;
/**
* Implements hook_preprocess_links().
......
......@@ -9,8 +9,8 @@ declare(strict_types = 1);
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\oe_bootstrap_theme_helper\EuropeanUnionLanguages;
use Drupal\oe_whitelabel\DocumentMediaWrapper;
use Drupal\oe_whitelabel_helper\EuropeanUnionLanguages;
// Include all files from the includes directory.
$includes_path = __DIR__ . '/includes/*.inc';
......
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