0

I want to get a full list of all languages included in the WPML plugin, regardless of whether or not they were active. The function icl_get_languages() only returns languages which are active on the site, so does not work here. The language list should be returned in the language of the current locale and should be sorted however that particular language sorts words (e.g. English: A -> Z, Japanese: ア -> オ).

I did "answer your own question", the solution is below. Full credit to the following (my solution is basically just these three stuck together):

Community
  • 1
  • 1
friendofdog
  • 79
  • 12

1 Answers1

0

If anyone's found a cleaner way of doing this, please do share.

<?php

function get_all_langs_names( $lang ) {

    global $wpdb;

    $lang_data = array();

    $languages = $wpdb -> get_results( 

        $wpdb -> prepare( "
            SELECT code, english_name, active, tag, name 
            FROM {$wpdb -> prefix}icl_languages lang 
            INNER JOIN {$wpdb -> prefix}icl_languages_translations trans 
            ON lang.code = trans.language_code 
            AND trans.display_language_code=%s", 
        $lang )

    );

    foreach( $languages as $l ) {

        $lang_data[$l -> code] = array(
            'english_name' => $l -> english_name,
            'active' => $l -> active,
            'tag' => $l -> tag,
            'name' => $l -> name,
        );

    }

    return $lang_data;

}

// get language tag of current site locale...
$code = ICL_LANGUAGE_CODE;

$lang_tag = $wpdb -> get_var("
    SELECT tag 
    FROM {$wpdb -> prefix}icl_languages 
    WHERE code='{$code}'"
);

// ... and then get language names associated with that tag
$languages = get_all_langs_names( $lang_tag );

// sort language array alphabetically, or however current language orders words
function sort_languages( $a, $b ) {

    return strcmp( $a['name'], $b['name'] );

}

usort( $languages, 'sort_languages' );

print_r( $languages );
friendofdog
  • 79
  • 12