<commit_before><?php
/**
 * @file
 * Contains \Drupal\AppConsole\Command\Helper\TranslatorHelper.
 */

namespace Drupal\AppConsole\Command\Helper;

use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Translation\Writer\TranslationWriter;
use Symfony\Component\Translation\MessageCatalogue;
use Drupal\AppConsole\YamlFileDumper;

class TranslatorHelper extends Helper
{

    private $language;

    /**
     * @var $translator Translator
     */
    private $translator;

    public function loadResource($language, $directoryRoot)
    {
        if (!file_exists($directoryRoot . 'config/translations/console.' . $language . '.yml')) {
            $language = 'en';
        }
        $this->language = $language;

        $this->translator = new Translator($language);
        $this->translator->addLoader('yaml', new YamlFileLoader());
        $this->translator->addResource(
          'yaml',
          $directoryRoot . 'config/translations/console.' . $language . '.yml',
          $language
        );
    }

    public function addResourceTranslationsByModule($module)
    {
        $resource = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) .
          '/config/translations/console.' . $this->language . '.yml';

        if (file_exists($resource)) {
            $this->translator->addResource(
              'yaml',
              $resource,
              $this->language
            );<commit_msg>#403 Refactor TranslatorHelper class<commit_after><?php
/**
 * @file
 * Contains \Drupal\AppConsole\Command\Helper\TranslatorHelper.
 */

namespace Drupal\AppConsole\Command\Helper;

use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Translation\Writer\TranslationWriter;
use Symfony\Component\Translation\MessageCatalogue;
use Drupal\AppConsole\YamlFileDumper;

class TranslatorHelper extends Helper
{

    private $language;

    /**
     * @var $translator Translator
     */
    private $translator;

    private function addResource($resource) {
        $this->translator->addResource(
          'yaml',
          $resource,
          $this->language
        );
    }

    public function loadResource($language, $directoryRoot)
    {
        $resource_fallback = $directoryRoot . 'config/translations/console.en.yml';
        $resource_language = $directoryRoot . 'config/translations/console.'.$language.'.yml';

        if (!file_exists($resource_language)) {
            $language = 'en';
            $resource_language = $resource_fallback;
        }
        $this->language = $language;
        $this->translator = new Translator($this->language);
        $this->translator->addLoader('yaml', new YamlFileLoader());

        //Fallback to English (en)
        $this->addResource($resource_fallback);

        //Load user language
        if ($resource_language != $resource_fallback) {
            $this->addResource($resource_language);
        }
    }

    public function addResourceTranslationsByModule($module)
    {
        $resource = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) .
          '/config/translations/console.' . $this->language . '.yml';

        if (file_exists($resource)) {
            $this->addResource($resource);
{'fraction_non_alphanumeric': 0.12903225806451613, 'fraction_numerical': 0.0008797653958944282, 'mean_word_length': 2.8325842696629215, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '6526588', 'n_tokens_mistral': 966, 'n_tokens_neox': 941, 'n_words': 159}