Skip to content

Centralized Translation Strings (Xe_Plugin\Strings)

The Xe_Plugin\Strings class provides a single centralized location for all localized frontend messages, status text, and interface strings.


Why Centralize Strings?

  • Prevents duplicate esc_html__() calls scattered throughout various JavaScript and PHP files.
  • Simplifies string maintenance and translation auditing.
  • Allows seamless localization data sharing between PHP and JavaScript via wp_localize_script().

Defining Strings (src/Strings.php)

namespace Xe_Plugin;

final class Strings {

  /**
   * Get all strings.
   *
   * @return array<string, string> Associative array of translation strings.
   */
  public static function all(): array {

    return [
      'pleaseWait'    => esc_html__( 'Please wait...', 'xe-plugin' ),
      'errorOccurred' => esc_html__( 'An error occurred: ', 'xe-plugin' ),
      'saving'        => esc_html__( 'Saving changes...', 'xe-plugin' ),
      'dashboardUrl'  => _xe_plugin()->endpoints()->get_url_by_key( 'xep-dashboard' ),
    ];

  }

}

Accessing Strings in PHP & JavaScript

In PHP:

$strings = \Xe_Plugin\Strings::all();
echo $strings['pleaseWait'];

In JavaScript:

Strings are automatically passed to the client script when enqueuing assets in src/Admin/Assets.php and src/Frontend/Assets.php:

// Access localized strings in your JS files
console.log(xePlugin.strings.pleaseWait);
console.log(xePlugin.strings.errorOccurred);