0

My website is in English Language and using WordPress.

Html Code contains lang="en-US" only.

But I want some of the page in other language like Spanish, Arabic and German.

When I create new page it is by default lang="en-US".

How can I set this lang="es" or specific language attribute?

I have tried many plugins but all are converting every pages to selected languages that I don't want.

1

2 Answers 2

0

You mentioned that you want:

some of the page in other language like Spanish, Arabic and German.

By "some" do you mean a few lines in one page are written in a foreign language, or that some pages of your entire website are in a foreign language?

Because if your entire content is English and you just need to write a few lines or paragraphs in a foreign language you should be able to do that and then write styles to render them correctly (like adding direction: rtl for Arabic text).

In the case of the latter (some pages being in a foreign language), perhaps you could create a custom template for those pages with the appropriate tags and apply them? So you would have the Default Template, Arabic, German, and Spanish Template all show up as options in your Page settings.

0

Use locale or theme_locale hooks to alter the locale value of WordPress depending on a condition like:

  • the slug of a given page (/spanish-version/)
  • the presence of a specific slug (es-) in $_SERVER['REQUEST_URI'])

For example:

function my_theme_locale( string $locale, string $domain ) {
    $current_uri = $_SERVER['REQUEST_URI'];
    if ( is_page( 'spanish-version' ) ) {
        $locale = 'es_ES';
        switch_to_locale( $locale );
    }
    return $locale;
}
add_filter( 'theme_locale', 'my_theme_locale', 10, 2 );

You can even call load_theme_textdomain() to use WordPress native theme’s string translation logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.