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.

Elementor users - please update WPML to the latest version to maintain compatibility. More details here - https://wpml.org/changelog/2024/12/wpml-4-6-15-critical-update-for-elementor-sites/
Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: America/Los_Angeles (GMT-08:00)

This topic contains 0 replies, has 0 voices.

Last updated by Bobby 1 week, 3 days ago.

Assisted by: Bobby.

Author Posts
December 8, 2024 at 12:02 pm #16489663

olegA-5

Background of the issue:
I am trying to configure the WPML language settings for Montenegrin. In the WPML settings, I have set the language code as 'me', the default locale as 'sr_ME', and the hreflang as 'sr-me'. On the frontend, the language of the corresponding pages is correctly specified as 'sr-ME', and hreflang is specified as 'sr-me'.

Symptoms:
In the WPML language menu, the lang attribute is specified as 'me', which Google considers an error because this language is not included in the specs used by Google.

Questions:
How can I change the lang attribute in the WPML language menu to match the default locale or hreflang?

December 8, 2024 at 4:13 pm #16490090

olegA-5

I think this is exactly what I need.

add_filter( 'icl_ls_languages', 'my_montenegrin' );
function my_montenegrin( $languages ) {
    foreach( $languages as &$language ) {
        if( $language['default_locale'] === 'sr_ME' ) {
            $language['language_code'] = 'sr';
            break;
        }
    } 
    return $languages;
}

But it doesn't work. Why?

The options that do work:

$language['url'] =
$language['native_name'] =

But not the language_code one.

December 9, 2024 at 8:56 pm #16495119

Bobby
Supporter

Languages: English (English )

Timezone: America/Los_Angeles (GMT-08:00)

Hi there,

When I review your site I can see that the 'me' version is showing the following language attribute in HTML. "me-ME"

view-source:hidden link

Are you still experiencing this issue?

Also as a side note "Google primarily relies on "hreflang" tags to identify different language versions of a webpage, not the standard HTML "lang" attribute

Source: hidden link

December 12, 2024 at 5:23 pm #16509080

olegA-5

Hi Bobby,

The problem is not with the lang attribute of the page or hreflang, both of which are correct, but with the separate lang attribute of the WPML language menu. This attribute, as far as I understand, corresponds to the language code, i.e. the two letters that indicate the language in the URL (these two letters are selected once and for all in the WPML settings, they cannot be changed, they can only be changed by creating a new language and deleting the old one). I need to change the lang attribute of the WPML language menu because Google, which didn't seem to pay attention to it before, now looks at it (for screen reader purposes) and reports an error (downgrading the page) if the code doesn't meet its specification. At the same time, the peculiarity of Montenegrin is that it is not considered a language according to the specifications used by Google (there is a separate issue behind this, indirectly related to the break-up of Yugoslavia). So I have to satisfy Google without changing the language name in the URLs. I see that the only way to satisfy Google is to assign this language to Serbian as its variant. This is what I did earlier with the lang attribute of pages in this language and the hreflang attribute. But I can't change the lang attribute of the WPML language menu because, as far as I know, it's not an independent attribute, but reproduces the language code (this situation, by the way, I consider a fundamental flaw, as Google now sees this attribute as equivalent to the lang attribute of the page and hreflang).

December 13, 2024 at 8:07 am #16510256

Bobby
Supporter

Languages: English (English )

Timezone: America/Los_Angeles (GMT-08:00)

Hi there,

Thank you for this explanation!

You are correct, the language switcher is using the language code, which yes once it is set within WPML->Languages->Edit languages cannot be adjusted and we also don't have a recommended way of changing it in the database as that could cause major issues with existing translations.

The current workaround to change it is as follows:

1. Add a new custom language with a language code that meets your requirements

2. Go to WPML->Translation Management->Batch duplicate from the original language into the new one.

3. Replace the original language with the 'new' one that has the desired language code.

Regarding this "Google now sees this attribute as equivalent to the lang attribute of the page and hreflang" I am not familiar with this and have not heard anything, if you have any documentation that points to this information It would be great for me to pass along to our team to review.

December 13, 2024 at 10:38 am #16510883

olegA-5

Bobby,

I don't want to change the language code. It's correct. Not only because there are already many links to pages with this code in the URL, but also because there is no other sensible two-character code for the Montenegrin language apart from the Serbian language code. However, Serbian is another language that I already use. This means that my site already has pages in Serbian (in its Cyrillic version; my Montenegrin is in Latin script). So no, it's not about changing the language code in the URL, it's just about changing (I'd even say 'replacing') the lang attribute (used exclusively by screen readers) in the WPML language menu. I literally need to replace two letters of that very lang attribute of the language menu with another two letters (more would be better, of course, because it's ideal to call this language 'sr-me', not 'sr', because sr is already used for another language, and I'm not sure Google would like that).

As for the change in Google's perception, in the same ticket, I shared links in private messages to the results of analysing pages through pagespeed, where it is clear that Google considers this to be an error (actually it is enough to run any page of my website in Montenegrin through pagespeed to see this error). And yes, Google didn't react to it before. It also did not react to the page's lang attribute and hreflang, which I had originally set to 'me' for Montenegrin, not 'sr-me' as it is now.

December 13, 2024 at 10:48 am #16510917

olegA-5

Another, not ideal but still acceptable, option I see is to remove the WPML lang attribute from the language menu, but only for the Montenegrin language. I don't want to remove these attributes for other languages. As far as I understand, this option is definitely available, because if you switch to displaying the language name in the native language and its name in the page language at the same time, then the lang attribute is not used in the WPML language menu.

December 16, 2024 at 10:17 pm #16519051

Bobby
Supporter

Languages: English (English )

Timezone: America/Los_Angeles (GMT-08:00)

Hi there,

I completely understand the issue now.

To resolve this you will need to create a custom WPML language switcher and then unset the lang attribute.

This will require 2 pieces of code.

NOTE: For my tests I used 'es' as the language code, make sure you replace 'es' with your language code.

#1

Unset the lang attribute, to do this you will add the following code in your theme's functions.php

add_filter('wpml_language_switcher', 'custom_wpml_language_switcher', 10, 3);

function custom_wpml_language_switcher($languages, $style, $id) {
    foreach ($languages as &$language) {
        if ($language['code'] === 'es') {
            // Remove the 'lang' attribute for Spanish
            unset($language['lang']);
        }
    }
    return $languages;
}

#2 Create a custom language switcher

You can find more information about this here:
https://wpml.org/documentation/getting-started-guide/language-setup/language-switcher-options/custom-language-switcher/

For example here is an example one (very basic example)

function render_custom_wpml_language_switcher() {
    $languages = apply_filters('wpml_active_languages', NULL, ['skip_missing' => 0]);

    if (!empty($languages)) {
        echo '<ul class="custom-language-switcher">';
        foreach ($languages as $language) {
            // Check if the language is Spanish
            if ($language['code'] === 'es') {
                echo '<li><a href="' . esc_url($language['url']) . '">' . esc_html($language['native_name']) . '</a></li>';
            } else {
                echo '<li><a href="' . esc_url($language['url']) . '" lang="' . esc_attr($language['lang']) . '">' . esc_html($language['native_name']) . '</a></li>';
            }
        }
        echo '</ul>';
    }
}

#3

Display the language switcher in the front end.

Documentation: https://wpml.org/documentation/getting-started-guide/language-setup/language-switcher-options/adding-language-switchers-using-php-and-shortcodes/

For the one above, I used this in my theme's footer.php:

<?php render_custom_wpml_language_switcher(); ?>

Going this route will remove the lang attribute for the specified language without affecting the rest.

You can find an example here in the footer for the ES language only.

hidden link

NOTE: It is important to note that custom work is not covered under our support scope and this is provided by the supporter in order to assist further. Our team does not officially test all custom work and you are liable to make sure a backup is in place before testing the code and steps shared.