Content based on current language (Polylang + Switch/If)

Goal:
I would like to use different content (translations) based on current language settings. I use Polylang.

I checked this thread and built something analogical for Polylang, but it doesn’t work. What might be a problem?

What I have:
Simple check:

<Loop type="taxonomy_term" taxonomy="language">
  <p>Available languge: <Field title/></p>
</Loop>

It returns:

Available languge: English
Available languge: Deutsch

It returns correctly name of available languages, but I’m not sure what I could do with this.

What comes next, seems to be a more reasonable approach based on the aforementioned thread above.

<Set current-lang>[get_current_language]</Set>
<p>Current language check: <Get current-lang></Get></p>

and PHP for the shortcode:

function xm_get_language_shortcode() {
	if ( function_exists( 'pll_the_languages' ) ) {
    	return pll_current_language();
	}
}
add_shortcode( 'get_current_language', 'xm_get_language_shortcode' );

And L&L returns:

Current language check: en

All good.

So I wanted to use Switch or If to get things done, but it doesn’t work. I suspect that using a shortcode inside is the culprit?

<!-- 1. SWITCH with variable -->
<Switch variable="{Get current-lang}" is>
  <When value="en" />
    <p>1. This is <Get current-lang></Get> language</p>
  <When value="de" />
    <p>1. This is <Get current-lang></Get> language</p>
  <When />
    <p>1. Something is not right OR language not specified</p>
</Switch>

<!-- 2. SWITCH with check -->
<Switch check="{Get current-lang}" is>
  <When value="en" />
    <p>2. This is <Get current-lang></Get> language</p>
  <When value="de" />
    <p>2. This is <Get current-lang></Get> language</p>
  <When />
    <p>2. Something is not right OR language not specified</p>
</Switch>

<!-- 3. IF with check -->
<If check="{Get current-lang}" is value="en">
   <p>3. This is <Get current-lang></Get> language</p>
  <Else if check="{Get current-lang}" value="de" />
     <p>3. This is <Get current-lang></Get> language</p>
  <Else />
    <p>3. Something is not right OR language not specified</p>
</If>

In each case it results in “Something is not right OR language not specified”.

Ok, let’s change the variable to “en”:

<Set current-lang>en</Set>
<p>Current language check: <Get current-lang></Get></p>

And now 2 and 3 cases work, except 1. Anyway, that confirms the variable issue, so I either can’t use a shortcode inside (previous thread doesn’t indicate that), or need to format it, but I couldn’t figure it out.

Does L&L have a strict comparison operator? Maybe the way the shortcode is processed inside and then the value inside {Get} messes something up? Just guessing.

Maybe there is an alternative, a better approach?

Any thoughts and help appreciated.

To run a shortcode, you can use the Shortcode tag.

<Set current-lang><Shortcode get_current_language /></Set>

By default, shortcodes inside templates are not run unless they’re wrapped by the Shortcode tag. But from your other code examples, it seems the shortcode was running there already.

The first example with Switch, I think the issue is that the variable attribute expects the name of the variable not its value.

<Switch variable="current-lang">

The comparison operator is is by default, so it’s not necessary to specify.

1 Like

Yes, it worked! Thank you @eliot

My mistake was not wrapping the shortcode in the correct syntax ().

Now I see it in the docs:

1 Like