/** * @return mixed array */ public function getStandardLanguages() { $standard_languages = LanguageManager::getStandardLanguageList(); $languages = []; foreach ($standard_languages as $langcode => $standard_language) { $languages[$langcode] = $standard_language[0]; } return $languages; }
/** * Installer step: Select language. */ protected function setUpLanguage() { // Place a custom local translation in the translations directory. mkdir(\Drupal::root() . '/' . $this->siteDirectory . '/files/translations', 0777, TRUE); touch(\Drupal::root() . '/' . $this->siteDirectory . '/files/translations/drupal-8.0.0.xoxo.po'); // Check that all predefined languages show up with their native names. $this->drupalGet($GLOBALS['base_url'] . '/core/install.php'); foreach (LanguageManager::getStandardLanguageList() as $langcode => $names) { $this->assertOption('edit-langcode', $langcode); $this->assertRaw('>' . $names[1] . '<'); } // Check that our custom one shows up with the file name indicated language. $this->assertOption('edit-langcode', 'xoxo'); $this->assertRaw('>xoxo<'); parent::setUpLanguage(); }
/** * {@inheritdoc} */ public function getAllDefinedLanguages() { // Get list of all configured languages. $languages = []; // See Drupal\language\ConfigurableLanguageManager::getLanguages() for details $predefined = LanguageManager::getStandardLanguageList(); foreach ($predefined as $key => $value) { $languages[$key] = new TranslatableMarkup($value[0]); } $config_ids = $this->configFactory->listAll('language.entity.'); foreach ($this->configFactory->loadMultiple($config_ids) as $config) { $data = $config->get(); $languages[$data['id']] = new TranslatableMarkup($data['label']); } asort($languages); return $languages; }
/** * Constructs a new class instance. * * @param array $values * An array of property values, keyed by property name, used to construct * the language. */ public function __construct(array $values = array()) { // Set all the provided properties for the language. foreach ($values as $key => $value) { if (property_exists($this, $key)) { $this->{$key} = $value; } } // If some values were not set, set sane defaults of a predefined language. if (!isset($values['name']) || !isset($values['direction'])) { $predefined = LanguageManager::getStandardLanguageList(); if (isset($predefined[$this->id])) { if (!isset($values['name'])) { $this->name = $predefined[$this->id][0]; } if (!isset($values['direction']) && isset($predefined[$this->id][2])) { $this->direction = $predefined[$this->id][2]; } } } }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) { if (count($install_state['translations']) > 1) { $files = $install_state['translations']; } else { $files = array(); } $standard_languages = LanguageManager::getStandardLanguageList(); $select_options = array(); $browser_options = array(); $form['#title'] = $this->t('Choose language'); // Build a select list with language names in native language for the user // to choose from. And build a list of available languages for the browser // to select the language default from. if (count($files)) { // Select lists based on available language files. foreach ($files as $langcode => $uri) { $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode; $browser_options[] = $langcode; } } else { // Select lists based on all standard languages. foreach ($standard_languages as $langcode => $language_names) { $select_options[$langcode] = $language_names[1]; $browser_options[] = $langcode; } } $request = Request::createFromGlobals(); $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options); $form['langcode'] = array('#type' => 'select', '#title' => $this->t('Choose language'), '#title_display' => 'invisible', '#options' => $select_options, '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en'); if (empty($files)) { $form['help'] = array('#type' => 'item', '#markup' => String::format('<p>Translations will be downloaded from the <a href="http://localize.drupal.org">Drupal Translation website</a>. If you do not want this, select <a href="!english">English</a>.</p>', array('!english' => install_full_redirect_url(array('parameters' => array('langcode' => 'en'))))), '#states' => array('invisible' => array('select[name="langcode"]' => array('value' => 'en')))); } $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array('#type' => 'submit', '#value' => $this->t('Save and continue'), '#button_type' => 'primary'); return $form; }
/** * Provides a list of configs to test. */ public function providerGetConfig() { return [['un', count(LanguageManager::getUnitedNationsLanguageList())], ['all', count(LanguageManager::getStandardLanguageList())]]; }
/** * Creates a configurable language object from a langcode. * * @param string $langcode * The language code to use to create the object. * * @return $this * * @see \Drupal\Core\Language\LanguageManager::getStandardLanguageList() */ public static function createFromLangcode($langcode) { $standard_languages = LanguageManager::getStandardLanguageList(); if (!isset($standard_languages[$langcode])) { // Drupal does not know about this language, so we set its values with the // best guess. The user will be able to edit afterwards. return static::create(array('id' => $langcode, 'label' => $langcode)); } else { // A known predefined language, details will be filled in properly. return static::create(array('id' => $langcode, 'label' => $standard_languages[$langcode][0], 'direction' => isset($standard_languages[$langcode][2]) ? $standard_languages[$langcode][2] : static::DIRECTION_LTR)); } }
public function buildForm(array $form, FormStateInterface $form_state, $install_state = NULL) { if (count($install_state['translations']) > 1) { $files = $install_state['translations']; } else { $files = array(); } $standard_languages = LanguageManager::getStandardLanguageList(); $select_options = array(); $browser_options = array(); $form['#title'] = 'Choose languages'; // Build a select list with language names in native language for the user // to choose from. And build a list of available languages for the browser // to select the language default from. // Select lists based on all standard languages. foreach ($standard_languages as $langcode => $language_names) { $select_options[$langcode] = $language_names[1]; $browser_options[$langcode] = $langcode; } // Add languages based on language files in the translations directory. if (count($files)) { foreach ($files as $langcode => $uri) { $select_options[$langcode] = isset($standard_languages[$langcode]) ? $standard_languages[$langcode][1] : $langcode; $browser_options[$langcode] = $langcode; } } asort($select_options); $request = Request::createFromGlobals(); $browser_langcode = UserAgent::getBestMatchingLangcode($request->server->get('HTTP_ACCEPT_LANGUAGE'), $browser_options); $form['langcode'] = array( '#type' => 'select', '#title' => 'Choose default language', '#title_display' => 'before', '#options' => $select_options, // Use the browser detected language as default or English if nothing found. '#default_value' => !empty($browser_langcode) ? $browser_langcode : 'en', ); $link_to_english = install_full_redirect_url(array('parameters' => array('langcode' => 'en'))); $form['help'] = array( '#type' => 'item', // #markup is XSS admin filtered which ensures unsafe protocols will be // removed from the url. '#markup' => '<p>Translations will be downloaded from the <a href="http://localize.drupal.org">Drupal Translation website</a>. If you do not want this, select <a href="' . $link_to_english . '">English</a>.</p>', '#states' => array( 'invisible' => array( 'select[name="langcode"]' => array('value' => 'en'), ), ), ); $form['langcodes'] = array( '#type' => 'select', '#title' => 'Choose another languages', '#title_display' => 'before', '#options' => $select_options, '#multiple' => TRUE, '#description' => 'Select another languages if your site is multilingual', ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => 'Save and continue', '#button_type' => 'primary', ); return $form; }
/** * {@inheritdoc} */ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { $langcode = $form_state->getValue('predefined_langcode'); if ($langcode == 'custom') { $langcode = $form_state->getValue('langcode'); $label = $form_state->getValue('label'); $direction = $form_state->getValue('direction'); } else { $standard_languages = LanguageManager::getStandardLanguageList(); $label = $standard_languages[$langcode][0]; $direction = isset($standard_languages[$langcode][2]) ? $standard_languages[$langcode][2] : ConfigurableLanguage::DIRECTION_LTR; } $entity->set('id', $langcode); $entity->set('label', $label); $entity->set('direction', $direction); // There is no weight on the edit form. Fetch all configurable languages // ordered by weight and set the new language to be placed after them. $languages = \Drupal::languageManager()->getLanguages(ConfigurableLanguage::STATE_CONFIGURABLE); $last_language = end($languages); $entity->setWeight($last_language->getWeight() + 1); }
/** * {@inheritdoc} */ public function label() { $languages = LanguageManager::getStandardLanguageList(); $countries = $this->getCountryManager()->getList(); return $this->t('@language (@country)', ['@language' => isset($languages[$this->getLanguageCode()]) ? $languages[$this->getLanguageCode()][0] : $this->getLanguageCode(), '@country' => isset($countries[$this->getCountryCode()]) ? $countries[$this->getCountryCode()] : $this->getCountryCode()]); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { /** @var \Drupal\currency\Entity\CurrencyLocaleInterface $currency_locale */ $currency_locale = $this->getEntity(); $options = array(); foreach (LanguageManager::getStandardLanguageList() as $language_code => $language_names) { $options[$language_code] = $language_names[0]; } natcasesort($options); $form['language_code'] = array('#default_value' => $currency_locale->getLanguageCode(), '#empty_value' => '', '#options' => $options, '#required' => TRUE, '#title' => $this->t('Language'), '#type' => 'select'); $form['country_code'] = array('#default_value' => $currency_locale->getCountryCode(), '#empty_value' => '', '#options' => $this->countryManager->getList(), '#required' => TRUE, '#title' => $this->t('Country'), '#type' => 'select'); $form['formatting'] = array('#open' => TRUE, '#title' => $this->t('Formatting'), '#type' => 'details'); $form['formatting']['decimal_separator'] = array('#default_value' => $currency_locale->getDecimalSeparator(), '#maxlength' => 255, '#required' => TRUE, '#size' => 3, '#title' => $this->t('Decimal separator'), '#type' => 'textfield'); $form['formatting']['grouping_separator'] = array('#default_value' => $currency_locale->getGroupingSeparator(), '#maxlength' => 255, '#size' => 3, '#title' => $this->t('Group separator'), '#type' => 'textfield'); $form['formatting']['pattern'] = array('#default_value' => $currency_locale->getPattern(), '#description' => $this->t('A Unicode <abbr title="Common Locale Data Repository">CLDR</abbr> <a href="http://cldr.unicode.org/translation/number-patterns">currency number pattern</a>.'), '#maxlength' => 255, '#title' => $this->t('Pattern'), '#type' => 'textfield'); return parent::form($form, $form_state, $currency_locale); }
/** * Helper function to prepare a list of available languages. * * @return array * An array of languages supported by Mollom with keys as the language code * and values as the translated display values. */ public static function getSupportedLanguages() { $languages = LanguageManager::getStandardLanguageList(); $supported = array_flip(self::$LANGUAGES_SUPPORTED); $supported = array_combine(array_keys($supported), array_keys($supported)); // Define those mappings that differ between Drupal codes and Mollom codes. $mapped = array('nb' => 'no', 'zh-hans' => 'zh-cn', 'zh-hant' => 'zh-tw'); foreach ($mapped as $drupal_key => $mollom_key) { if (isset($supported[$mollom_key])) { $supported[$drupal_key] = $mollom_key; unset($supported[$mollom_key]); } } $options = array(); $installed_languages = array(); // This does assume that all Mollom supported languages are in the predefined // Drupal list. foreach ($languages as $langcode => $language) { $found = FALSE; $simplified_code = strtok($langcode, '-'); if (isset($supported[$simplified_code]) && !isset($options[$simplified_code])) { $options[$supported[$simplified_code]] = t($language[0]); $found = TRUE; } else { if (isset($supported[$langcode]) && !isset($options[$langcode])) { $options[$supported[$langcode]] = t($language[0]); $found = TRUE; } } } // Sort by translated option labels. asort($options); // UX: Sort installed languages first. // @todo array_intersect_key($options, $installed_languages) + $options; return $options; }
/** * @covers ::label * @covers ::getCountryManager * * @depends testGetLocale */ function testLabel() { $languages = LanguageManager::getStandardLanguageList(); $language_code = array_rand($languages); $country_code_a = strtoupper($this->randomMachineName()); $country_code_b = strtoupper($this->randomMachineName()); $country_code_c = strtoupper($this->randomMachineName()); $country_list = [$country_code_a => $this->randomMachineName(), $country_code_b => $this->randomMachineName(), $country_code_c => $this->randomMachineName()]; $this->countryManager->expects($this->atLeastOnce())->method('getList')->willReturn($country_list); $this->sut->setLocale($language_code, $country_code_b); $this->assertInstanceOf(TranslatableMarkup::class, $this->sut->label()); }
/** * {@inheritdoc} */ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) { $langcode = $form_state->getValue('predefined_langcode'); if ($langcode == 'custom') { $langcode = $form_state->getValue('langcode'); $label = $form_state->getValue('label'); $direction = $form_state->getValue('direction'); } else { $standard_languages = LanguageManager::getStandardLanguageList(); $label = $standard_languages[$langcode][0]; $direction = isset($standard_languages[$langcode][2]) ? $standard_languages[$langcode][2] : ConfigurableLanguage::DIRECTION_LTR; } $entity->set('id', $langcode); $entity->set('label', $label); $entity->set('direction', $direction); }
/** * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) { // Defaults. $config = ['language_list' => 'un']; $settings = $editor->getSettings(); if (isset($settings['plugins']['language'])) { $config = $settings['plugins']['language']; } $predefined_languages = LanguageManager::getStandardLanguageList(); $form['language_list'] = array('#title' => $this->t('Language list'), '#title_display' => 'invisible', '#type' => 'select', '#options' => ['un' => $this->t("United Nations' official languages"), 'all' => $this->t('All @count languages', ['@count' => count($predefined_languages)])], '#default_value' => $config['language_list'], '#description' => $this->t('The list of languages to show in the language dropdown. The basic list will only show the <a href=":url">six official languages of the UN</a>. The extended list will show all @count languages that are available in Drupal.', [':url' => Url::fromUri('http://www.un.org/en/aboutun/languages.shtml/')->toString(), '@count' => count($predefined_languages)]), '#attached' => ['library' => ['ckeditor/drupal.ckeditor.language.admin']]); return $form; }