<commit_before><?php

namespace Vierbeuter\WordPress\Service;

/**
 * The Translator service provides methods for translating texts.
 *
 * @package Vierbeuter\WordPress\Service
 */
class Translator extends Service
{

    /**
     * @var string
     */
    protected $domain;

    /**
     * @var string
     */
    protected $languagesDir;

    /**
     * Translator constructor.
     *
     * @param string $domain text domain (name of the plugin)
     * @param string $languagesDir absolute directory path to language files containing all translations
     */
    public function __construct(string $domain, string $languagesDir)
    {
        //  set domain
        $this->domain = $domain;
        //  set directory path, ensure to have an absolute path with leading and trailing slash
        $this->languagesDir = '/' . trim($languagesDir, '/') . '/';

        //  register action hook to load translation files
        add_action('plugins_loaded', [$this, 'loadTextDomain']);
    }

    /**
     * Returns the translated text for the given one.
     *
     * Optionally a context can be passed as information for the translators in case of the given text collides with
     * similar translatable text found at another location but with different translated context.
     *
     * @param string $text
     * @param string|null $context
     *
     * @return string
     *
     * @see __()
     * @see _x()
     */
    public function translate(string $text, string $context = null): string
    {
        if (empty($context)) {
            return __($text, $this->domain);
        }

        return _x($text, $context, $this->domain);
    }

    /**
     * Loads the translation file for current locale.
     */
    public function loadTextDomain()
    {
        /**
         * Filters a plugin's locale.
         *
         * @since 3.0.0
         *
         * @param string $locale The plugin's current locale.
         * @param string $domain Text domain. Unique identifier for retrieving translated strings.
         */
        $locale = apply_filters('plugin_locale', is_admin() ? get_user_locale() : get_locale(), $this->domain);

        //  build path to *.mo file as also done in several functions located in …/wp-includes/l10n.php
        /** @see load_plugin_textdomain() */
        $moFile = $this->domain . '-' . $locale . '.mo';

        //  load the file and "link" with domain
        load_textdomain($this->domain, $this->languagesDir . $moFile);
    }
}
<commit_msg>fix: replace action hook with direct method call due to problems in wp-admin (registering the action hook works for frontend but doesn't seem to work [yet?] in WP's admin area somehow)<commit_after><?php

namespace Vierbeuter\WordPress\Service;

/**
 * The Translator service provides methods for translating texts.
 *
 * @package Vierbeuter\WordPress\Service
 */
class Translator extends Service
{

    /**
     * @var string
     */
    protected $domain;

    /**
     * @var string
     */
    protected $languagesDir;

    /**
     * Translator constructor.
     *
     * @param string $domain text domain (name of the plugin)
     * @param string $languagesDir absolute directory path to language files containing all translations
     */
    public function __construct(string $domain, string $languagesDir)
    {
        //  set domain
        $this->domain = $domain;
        //  set directory path, ensure to have an absolute path with leading and trailing slash
        $this->languagesDir = '/' . trim($languagesDir, '/') . '/';

        //  load translation file
        $this->loadTextDomain();
    }

    /**
     * Returns the translated text for the given one.
     *
     * Optionally a context can be passed as information for the translators in case of the given text collides with
     * similar translatable text found at another location but with different translated context.
     *
     * @param string $text
     * @param string|null $context
     *
     * @return string
     *
     * @see __()
     * @see _x()
     */
    public function translate(string $text, string $context = null): string
    {
        if (empty($context)) {
            return __($text, $this->domain);
        }

        return _x($text, $context, $this->domain);
    }

    /**
     * Loads the translation file for current locale.
     */
    protected function loadTextDomain(): void
    {
        /**
         * Filters a plugin's locale.
         *
         * @since 3.0.0
         *
         * @param string $locale The plugin's current locale.
         * @param string $domain Text domain. Unique identifier for retrieving translated strings.
         */
        $locale = apply_filters('plugin_locale', is_admin() ? get_user_locale() : get_locale(), $this->domain);

        //  build path to *.mo file as also done in several functions located in …/wp-includes/l10n.php
        /** @see load_plugin_textdomain() */
        $moFile = $this->domain . '-' . $locale . '.mo';

        //  load the file and "link" with domain
        load_textdomain($this->domain, $this->languagesDir . $moFile);
    }
}

{'fraction_non_alphanumeric': 0.10934744268077601, 'fraction_numerical': 0.0019596315892612187, 'mean_word_length': 2.2551020408163267, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1208320', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1361, 'n_words': 467}