Making WordPress.org

Changeset 13051


Ignore:
Timestamp:
12/12/2023 11:29:38 AM (2 years ago)
Author:
amieiro
Message:

Translate: Reorder the preference for the "Other locales" summary element

See https://github.com/WordPress/wordpress.org/pull/170

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-translation-suggestions/inc/routes/class-other-languages.php

    r8761 r13051  
    99
    1010class Other_Languages extends GP_Route {
     11
     12    /**
     13     * Indicates the related locales for each locale, so we will put them in the top list
     14     * of "Other locales".
     15     *
     16     * This array should be sync with
     17     * https://github.com/GlotPress/gp-translation-helpers/blob/main/helpers/helper-other-locales.php
     18     *
     19     * @var array
     20     */
     21    public array $related_locales = array(
     22        'ca'  => array( 'ca-val', 'bal', 'es', 'oci', 'an', 'gl', 'fr', 'it', 'pt', 'ro', 'la' ),
     23        'es'  => array( 'gl', 'ca', 'pt', 'pt-ao', 'pt-br', 'it', 'fr', 'ro' ),
     24        'gl'  => array( 'es', 'pt', 'pt-ao', 'pt-br', 'ca', 'it', 'fr', 'ro' ),
     25        'it'  => array( 'ca', 'de', 'es', 'fr', 'pt', 'ro' ),
     26        'ne'  => array( 'hi', 'mr', 'as' ),
     27        'oci' => array( 'ca', 'fr', 'it', 'es', 'gl' ),
     28        'ug'  => array( 'tr', 'uz', 'az', 'zh-cn', 'zh-tw' ),
     29    );
    1130
    1231    public function get_suggestions( $project_path, $locale_slug, $translation_set_slug ) {
     
    3958        $suggestions = $this->query( $original, $translation_set );
    4059
    41         wp_send_json_success( gp_tmpl_get_output( 'other-languages-suggestions', [ 'suggestions' => $suggestions ], PLUGIN_DIR . '/templates/' ) );
     60        wp_send_json_success( gp_tmpl_get_output( 'other-languages-suggestions', array( 'suggestions' => $suggestions ), PLUGIN_DIR . '/templates/' ) );
    4261    }
    4362
     
    7897        );
    7998
    80         return $results;
     99        $results_with_preference = array();
     100
     101        // Put the variants in the top list.
     102        foreach ( $results as $key => $result ) {
     103            if ( explode( '-', $result['locale'] )[0] === explode( '-', $set_to_exclude->locale )[0] ) {
     104                $results_with_preference[] = $result;
     105                unset( $results[ $key ] );
     106            }
     107        }
     108
     109        if ( ! empty( $this->related_locales[ $set_to_exclude->locale ] ) ) {
     110            foreach ( $this->related_locales[ $set_to_exclude->locale ] as $locale ) {
     111                foreach ( $results as $key => $result ) {
     112                    if ( $result['locale'] == $locale ) {
     113                        $results_with_preference[] = $result;
     114                        unset( $results[ $key ] );
     115                        continue 2;
     116                    }
     117                }
     118            }
     119        }
     120
     121        return array_merge( $results_with_preference, $results );
    81122    }
    82123}
Note: See TracChangeset for help on using the changeset viewer.