Skip Navigation

[Resolved] How to Check Locale in a Conditional

This thread is resolved. Here is a description of the problem and solution.

Problem:
Need to check in which language users viewing my website so I can show them different content in views and content templates. Just need to use a conditional in a content template so if wordpress changes to english then I should show different content to users. Is that possible?

Solution:
In a View or Content Template or Layout template you can detect language something like this:

[wpv-conditional if="( '[wpv-bloginfo show="language"]' eq 'en-US' )"]
Language is english
[/wpv-conditional]
 
[wpv-conditional if="( '[wpv-bloginfo show="language"]' eq 'ar' )"]
Language is Arabic
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 7 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by ioannisM-2 7 years ago.

Assisted by: Noman.

Author
Posts
#582990

Hello,

I need to check in which language users viewing my website so I can show them different content in views and content templates. I have manually translated with poedit. Before header exists the tag <html lang=en_US> with the language code or <html lang=el>. Is there a way to check in a conditional which version of the website a user is viewing and show him different content in views and templates? How could I check the locale?

Thank you!

#583252

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ioannis,

Thank you for contacting Toolset support. To translate Views and Content Template created with Toolset we recommend to use WPML plugin as it provides you with language switcher and natively support multilingual sites. Below are the docs explain this process:

- Translating sites built with Toolset: https://toolset.com/documentation/translating-sites-built-with-toolset/
- Translating Content Templates, WordPress Archives, Views, and CRED forms: https://toolset.com/documentation/translating-sites-built-with-toolset/translating-content-templates-wordpress-archives-views-cred-forms/

If you are not using WPML that I you will require lots of custom coding stuff and we do not recommend that. Some threads that might help you in this case:
- https://stackoverflow.com/questions/3770513/detect-browser-language-in-php#answer-3770616
- https://stackoverflow.com/questions/297542/simplest-way-to-detect-client-locale-in-php#answer-297554

Other than that falls into Custom coding & custom development and it is out of support policy (https://toolset.com/toolset-support-policy/). So we recommend to contact Toolset recommended service providers to further discuss the custom approach. We have some recommended list of service providers here if you would like to take a look: https://toolset.com/consultant/

Thank you

#586196

Hello Noman,

I am sorry for the delayed response... What I am trying to achieve is to detect in which language wordpress is running and not to detect the user's browser language. If I use wp in my language the code in the beginning of html is <html lang=el>. If I change the language from the dashboard to english, the code changes to <html lang=en_US>. I just need to use a conditional in a content template so if wordpress changes to english then I should show different content to users. Is that possible?

Thank you!

#586665

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Ok I believe you are using wpv if conditional in a Content Template or in a View, can you please confirm this?

Or you are using conditional in a php code?

Thanks

#586669

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Ioannis,

In a View or Content Template or Layout template you can detect language something like this:

[wpv-conditional if="( '[wpv-bloginfo show="language"]' eq 'en-US' )"]
Language is english
[/wpv-conditional]

[wpv-conditional if="( '[wpv-bloginfo show="language"]' eq 'ar' )"]
Language is Arabic
[/wpv-conditional]

Please try it, I hope this will resolve the issue. Thank you

#586916

Yes, it works perfectly, thank you!!!!! Is there a way to check in a php template also?

#586997

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Ioannis,

Yes, you can check using following code:

$site_lng = get_bloginfo( 'language' );

if($site_lng == 'en-US') {
	
}
elseif($site_lng == 'ar') {
	
}

Thank you

#587305

Yes, it works! Thank you Noman for your excellent support!!!!!