Weglot
WebsiteGithubSlack
  • Developer Documentation
  • WordPress
    • Getting Started
    • Helpers Functions
    • Hooks
      • Translations Hooks
      • Other Hooks
    • Use cases
      • Lang attribute
      • Translate link
      • Implementing Custom Link Translation
      • Wp-rocket Lazyload
      • Deactivate Weglot on Elementor
      • Weglot translate on Elementor
      • Language Selector Styling
      • Change flag size
      • Exclude a Custom Post Type
      • Exclude the search page
      • Exclude draft or private status post
      • Plugin Woocommerce: Translate all email sent to customers
      • Plugin WooCommerce: Translate Product URLs
      • Use custom language code (URL, lang and hreflang attribute)
      • Theme OceanWP: Use language selector in the main menu
      • Theme Avada: Use language selector in the main menu
      • LinkedIn Share Post: Translate oEmbed WP URL
      • Hide button on excluded page
      • Auto switch only for HomePage
      • Proxify Url
      • Translate pdf for Woocommerce pdf invoice
      • Translate pdf with Gravity pdf
      • Translate Dublin core meta (or other meta)
      • How to not translate specific ajax action
      • How to get translated url programmatically
      • How to set WP locale based on Weglot current language
      • How to translate mail from Photo Reviews for WooCommerce
      • How to translate schema from schema.org generate by Yoast plugin
      • How to translate email sending by plugin WP Mail SMTP
      • How to hide the Weglot admin button for non administrator roles
      • Exemple of snippet to translate search from JetSearch plugin
      • Example of snippet to translate your checkout element (override/added by the Funnel Builder plugin)
      • LScache
      • How to fix live builder issue with Weglot
      • Add translated url to Rankmath sitemap index
      • Advanced Woo Search X Weglot
      • Conditionally Disabling Weglot Translation for Specific URLs
    • Weglot filters
      • weglot_translate_email
      • weglot_cancel_init
      • weglot_translate_email_languages_forced
      • weglot_menu_parent_item_title
      • weglot_active_current_menu_item
      • weglot_ajax_no_translate
      • weglot_active_translation_before_process
      • weglot_active_translation_before_treat_page
      • weglot_debug_file
      • weglot_autoredirect_only_home
      • weglot_autoredirect_skip
      • weglot_remove_google_translate
      • weglot_add_hreflang
      • weglot_get_replace_modify_link
      • get_replace_modify_link_in_xml
      • weglot_get_flag_class
      • weglot_get_name_with_language_entry
      • weglot_get_class_dropdown
      • weglot_button_html
      • weglot_get_dom_checkers
      • weglot_replace_div_id
      • weglot_replace_weglot_menu
      • weglot_render_default_button
      • weglot_render_switcher_editor_button
      • weglot_href_lang
      • weglot_get_options_from_cdn_cache
      • weglot_get_options_from_cdn_cache_duration
      • weglot_get_slugs_from_cache
      • weglot_get_slugs_cache_duration
      • weglot_exclude_blocks
      • weglot_exclude_urls
      • weglot_get_parser_ignored_nodes
      • weglot_navigator_language
      • weglot_url_auto_redirect
      • weglot_replace_url
      • weglot_replace_link
      • weglot_ajax_replace_urls
      • weglot_proxify_urls
      • weglot_add_json_keys
      • weglot_json_treat_page
      • weglot_html_treat_page
      • Copy of weglot_xml_treat_page
      • weglot_render_dom
      • weglot_default_current_language_empty
    • Algolia integration
  • Proxy
    • Headers
  • Javascript
    • Getting Started
    • Options
    • Javascript functions
    • Language link Hooks
    • Translate iframe
    • Advanced concepts
      • Translation engines
  • CMS Specific
    • Shopify
  • API
    • Reference
  • Cookies
Powered by GitBook
On this page
  • Example: Changing "tw" to "zh-HK"
  • Hook for customize language code
  • Update for autoredirection option
  • Method 1: Use plugin Code Snippets
  • Method 2: Create a MU-PLUGIN
  1. WordPress
  2. Use cases

Use custom language code (URL, lang and hreflang attribute)

Here, we explain how to change the default language code. This is useful if you don't like the default 2 letters code of Weglot in the URL and want to change it into another language code.

Example: Changing "tw" to "zh-HK"

In this post, we show for example how to change language code tw (Traditional Chinese) to zh-HK (Hong Kong Chinese).

<!-- HTML output by default (Traditional Chinese) -->
<html lang="tw">
    <head>
        <link rel="alternate" hreflang="en" href="https://mysite.com/"/>
        <link rel="alternate" hreflang="tw" href="https://mysite.com/tw/"/>
    </head>
    <body>
        <!-- ... -->
    </body>
</html>

<!-- HTML output with custom language code (Hong Kong Chinese) -->
<html lang="zh-HK">
    <head>
        <link rel="alternate" hreflang="en" href="https://mysite.com/"/>
        <link rel="alternate" hreflang="zh-HK" href="https://mysite.com/zh-HK/"/>
    </head>
    <body>
        <!-- ... -->
    </body>
</html>

<!-- HTML output with custom language code (Hong Kong Chinese) but keep it to tw on hreflangs-->
<html lang="zh-HK">
    <head>
        <link rel="alternate" hreflang="en" href="https://mysite.com/"/>
        <link rel="alternate" hreflang="tw" href="https://mysite.com/zh-HK/"/>
    </head>
    <body>
        <!-- ... -->
    </body>
</html>

Hook for customize language code

This hook must be load before your theme. This is why you can not use it in your functions.php. To use it, you can follow one of the 2 method below.

// Use zh-HK (Hong Kong Chinese) instead tw (Traditional Chinese)
add_filter( 'weglot_language_code_replace', 'custom_weglot_language_code_replace' );
function custom_weglot_language_code_replace( $replacements ) { 
    $replacements['tw'] = 'zh-HK';
    return $replacements;
}

// Use tw instead of zh-HK on hreflangs (optional)
add_filter( 'weglot_href_lang', 'custom_weglot_href_lang' );
function custom_weglot_href_lang( $render ) {
	$render = str_replace("hreflang=\"zh-HK\"", "hreflang=\"tw\"", $render);
	return $render;
}

Update for autoredirection option

If you've activate the autoredirection option, you should be use this other filter to add your custom code into the navigator language list

add_filter( 'weglot_navigator_language', 'custom_weglot_navigator_language' );
function custom_weglot_navigator_language( $navigator_languages ) {
	$navigator_languages = array();
	if ( isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) { //phpcs:ignore
		$navigator_languages = explode( ',', trim( sanitize_text_field( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) );
		foreach ( $navigator_languages as &$navigator_language ) {
			if ( strpos( $navigator_language, ';' ) !== false ) {
				$navigator_language = substr( $navigator_language, 0, strpos( $navigator_language, ';' ) );
			}
			$navigator_language = strtolower( $navigator_language );
			$navigator_language = str_replace('tw', 'zh-HK', $navigator_language);
		}
	}

	return $navigator_languages;
}

Method 1: Use plugin Code Snippets

  1. In your WordPress back office, go to Snippets -> Add new

  2. Add a title (example: Weglot - Custom language code)

  3. Add the following code into the code input. Don't copy <?php cause it's already added by the plugin.

  4. Save changes

Method 2: Create a MU-PLUGIN

  1. Create a new file : /wp-content/mu-plugins/weglot-language-code-replace.php

  2. Add the following code into the new file and save it.

PreviousPlugin WooCommerce: Translate Product URLsNextTheme OceanWP: Use language selector in the main menu

Last updated 2 years ago

Add and activate Code Snippets plugin on your WordPress:

https://wordpress.org/plugins/code-snippets/