Ajax – gettext without a plugin

I did manage to solve this later on by using WPML, but over time found that WPML is quite bloaty and slow, support not very good, and if you need only 1 language (but original strings for theme are in English and the language you need is not) or perhaps 2, you do not need it. The only thing you need is:

  1. Set LOCALE in your theme .env or wp-config.php. An example would be LOCALE=de_DE or define('LOCALE', 'de_DE') for wp-config.
  2. Have your text domain loaded based on where your .MO files will be (run this in after_setup_theme hook)

load_theme_textdomain( 'your_domain', get_template_directory() . '/languages' );

  1. Set up your locale via filters based on where you set it

/**
* Set locale based on our .env LOCALE
*/
add_filter( 'locale', function($locale) {
if ( !is_admin() && getenv('LOCALE') !== false)
$locale = getenv('LOCALE');
return $locale;
});

Also, I very much recommend https://roots.io/sage/ to start your
theme off, but it is not required.

It provides a nice way to generate your .pot file for translations via Yarn too. Based on that it is quite easy to create .PO files from it for the languages you need and send them for translation and just follow the steps above (dont forget to put .mo file in languages folder, named de_DE.mo based on your LOCALE, and everything works as expected.