# Expose the Page Language to JavaScript

> Adds a small window.wpideLang object with the current locale, language code and text direction, so your scripts can format dates, numbers or messages correctly without guessing.

- Type: PHP
- Runs: Run everywhere
- Category: [Multilingual](https://wpide.com/library/category/multilingual/)
- Tags: javascript, locale, developer
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/i18n-current-language-js/

## Code

```php
add_action( 'wp_head', function () {
    $locale = determine_locale();
    $code   = strtok( str_replace( '_', '-', $locale ), '-' );

    if ( defined( 'ICL_LANGUAGE_CODE' ) && ICL_LANGUAGE_CODE ) {
        $code = ICL_LANGUAGE_CODE;
    } elseif ( function_exists( 'pll_current_language' ) && pll_current_language() ) {
        $code = pll_current_language();
    }

    $data = array(
        'locale' => $locale,
        'code'   => $code,
        'dir'    => is_rtl() ? 'rtl' : 'ltr',
    );

    echo '<script>window.wpideLang=' . wp_json_encode( $data ) . ';</script>' . "\n";
}, 1 );
```

## Notes

Picks up the language set by WPML or Polylang when either is active, otherwise the WordPress locale.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Expose the Page Language to JavaScript" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
