function locale_form_language_admin_overview_form_alter

Same name in other branches
  1. 8.9.x core/modules/locale/locale.module \locale_form_language_admin_overview_form_alter()
  2. 10 core/modules/locale/locale.module \locale_form_language_admin_overview_form_alter()
  3. 11.x core/modules/locale/locale.module \locale_form_language_admin_overview_form_alter()

Implements hook_form_FORM_ID_alter() for language_admin_overview_form().

File

core/modules/locale/locale.module, line 584

Code

function locale_form_language_admin_overview_form_alter(&$form, FormStateInterface $form_state) {
    $languages = $form['languages']['#languages'];
    $total_strings = \Drupal::service('locale.storage')->countStrings();
    $stats = array_fill_keys(array_keys($languages), []);
    // If we have source strings, count translations and calculate progress.
    if (!empty($total_strings)) {
        $translations = \Drupal::service('locale.storage')->countTranslations();
        foreach ($translations as $langcode => $translated) {
            $stats[$langcode]['translated'] = $translated;
            if ($translated > 0) {
                $stats[$langcode]['ratio'] = round($translated / $total_strings * 100, 2);
            }
        }
    }
    array_splice($form['languages']['#header'], -1, 0, [
        'translation-interface' => t('Interface translation'),
    ]);
    foreach ($languages as $langcode => $language) {
        $stats[$langcode] += [
            'translated' => 0,
            'ratio' => 0,
        ];
        if (!$language->isLocked() && locale_is_translatable($langcode)) {
            $form['languages'][$langcode]['locale_statistics'] = Link::fromTextAndUrl(t('@translated/@total (@ratio%)', [
                '@translated' => $stats[$langcode]['translated'],
                '@total' => $total_strings,
                '@ratio' => $stats[$langcode]['ratio'],
            ]), Url::fromRoute('locale.translate_page', [], [
                'query' => [
                    'langcode' => $langcode,
                ],
            ]))->toRenderable();
        }
        else {
            $form['languages'][$langcode]['locale_statistics'] = [
                '#markup' => t('not applicable'),
            ];
        }
        // #type = link doesn't work with #weight on table.
        // reset and set it back after locale_statistics to get it at the right end.
        $operations = $form['languages'][$langcode]['operations'];
        unset($form['languages'][$langcode]['operations']);
        $form['languages'][$langcode]['operations'] = $operations;
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.