Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Nicolas V. 1 year, 7 months ago.

Assisted by: Nicolas V..

Author Posts
October 31, 2023 at 4:05 pm #14702155

eikoT

Hi, I'm currently developing a new custom theme.

The use of constants like ICL_LANGUAGE_CODE is flagged as deprecated in your docs.
You offer a filter as a replacement for that (called 'wpml_current_language', which returns 'en' for English or 'de' for German).

However I want to use the old ICL_LANGUAGE_NAME constant to simply get the current language name written out in the current language. As in 'English' or 'Deutsch'.
I was not able to find a replacement for the constant.

Any suggestions?

Kind Regards

October 31, 2023 at 8:47 pm #14703883

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hello,

Welcome to the WPML support forum.
1. You can use 'wpml_current_language' and write something like this:

$my_current_lang = apply_filters( 'wpml_current_language', NULL );
if ( $my_current_lang == 'fr' ){ 
[FRENCH TEXT HERE]
} elseif ( $my_current_lang == 'en' ){
[FRENCH TEXT HERE]

Ref: https://wpml.org/wpml-hook/wpml_current_language/

2. Or use 'wpml_post_language_details' to get all the details about the language of a post (this will include the translated and the native names: French / Français).
Ref: https://wpml.org/wpml-hook/wpml_post_language_details/

November 1, 2023 at 10:14 am #14706507

eikoT

Thanks for your reply.

1. Sorry, this is too crude for my case. If I add another language this solution falls apart.

2. I've looked into that, but this filter needs a post object. That means, on a 404 Page this filter throws a wp error object.

I've found another filter called 'wpml_active_languages' that enabled me to build my own template function. Maybe add something like that to your FAQ / Docs. Feel free to use my function.

Here's a gist: hidden link

```php
if ( ! function_exists( 'wtb_get_current_language_name' ) ) :
/**
* Returns current active site language array.
* If the optional $arg is set, only the corresponding value is returned.
*
* $arg can be one of:
* code, id, native_name, major, active, default_locale,
* encode_url, tag, translated_name, url, country_flag_url, language_code
*/
function wtb_get_current_language( $arg = NULL ){
$wpml_active_languages = apply_filters( 'wpml_active_languages', NULL, NULL );
if ( !is_array( $wpml_active_languages ) ) {
return null;
}

foreach ($wpml_active_languages as $key => $language) {
if ( isset($language['active']) && $language['active'] == 1 ) {
//return language array
if(!$arg){
return $language;
}
//return only selected language value
//one of ["code"], ["id"], ["native_name"], ["major"], ["active"], ["default_locale"], ["encode_url"], ["tag"], ["translated_name"], ["url"], ["country_flag_url"], ["language_code"]
if( $arg && isset($language[$arg]) ){
return $language[$arg];
}
}
}
return null; //No active language
}
endif;
```

The issue is resolved now.

November 1, 2023 at 12:27 pm #14707361

Nicolas V.
Supporter

Languages: English (English ) French (Français )

Timezone: America/Lima (GMT-05:00)

Hello,

I'm sorry if I didn't fully understand your request. I thought you were building a custom theme for yourself.

You can find all our hooks here: https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/
For each hook, you have a short example but you can also click on the hook title to see the dedicated page for that hook, so example here: https://wpml.org/wpml-hook/wpml_active_languages/

I hope it will be helpful for you or any client finding this thread. Thanks for sharing your solution!

I'll close this ticket now, but feel free to open a new ticket anytime you have questions, we will be happy to help.

Have a good day ahead, Bye 🙂