AlienWolf
AlienWolf

Reputation: 25

Detect language and display proper content

I'm working on a multi language website and I'm almost done, except for an annoying detail: the search form displays results for all language.

<form action="<?php echo esc_url( home_url( '/' ) ); ?>">

I managed to understand that if I put the language prefix (es. en/ for english) into the form action it will display the current language results.

<form action="<?php echo esc_url( home_url( '/' ) ); ?>en/">

What I need to do now is to replace that en/ with a PHP code able to detect the current language to put the right prefix in the form action. I could tell the code to read html lang attribute and to echo the proper content, but I don't know how. Currently this is what I've done:

<?php if (get_lang() == 'it-it') {echo "it/";} elseif (get_lang() == 'en-us') {echo "en/";} else {echo "zh/";}?>

How do I detect page language in order to modify the form action and display the right content?

Upvotes: 1

Views: 296

Answers (1)

Aniket Sahrawat
Aniket Sahrawat

Reputation: 12937

Hacking around with substr instead of chained if else:

echo substr(get_lang(), 0, 2) . "/"; // echo first two characters of get_lang()

You can also use Locale::getPrimaryLanguage():

echo Locale::getPrimaryLanguage(get_lang()) . "/";

Upvotes: 3

Related Questions