<commit_before><?php

/**
 * Translate a string.
 *
 * @link http://github.com/marcoraddatz/candyCMS
 * @author <NAME> <http://marcoraddatz.com>
 * @license MIT
 * @since 2.0
 *
 */

namespace CandyCMS\Core\Helpers;

use CandyCMS\Core\Helpers\AdvancedException;

class I18n {

  /**
   *
   * Holds all translations
   *
   * @var static
   * @access private
   *
   */
  private static $_aLang = null;

  /**
   *
   * Holds the object
   *
   * @var static
   * @access private
   *
   */
  private static $_oObject = null;

  /**
  *
  * Holds the wanted Language
  *
  * @var static
  * @access private
  *
  */
  private static $_sLanguage = null;

  /**
   * Read the language yaml and save information into session due to fast access.
   *
   * @access public
   * @param string $sLanguage language to load
   * @param array $aSession the session object, if given save the translations in S_SESSION['lang']
   * @param array $aPlugins plugins to load
   *
   */
  public function __construct($sLanguage = 'en', &$aSession = null, $aPlugins = array()) {
    if ($aSession)
      $this->_aSession = $aSession;

    I18n::$_oObject = $this;

    # first call
    if (!isset(I18n::$_aLang) || WEBSITE_MODE == 'development' || WEBSITE_MODE == 'test') {
      $sLanguageFile = $sLanguage . '.yml';
      $sLanguagePath = PATH_STANDARD . '/app/languages/' . $sLanguageFile;

      # Remove mistakenly set cookie to avoid exceptions.
      if (!file_exists($sLanguagePath))
        $_COOKIE['default_language'] = 'en';

      # reload the files, if necessary
      if (WEBSITE_MODE == 'development' || WEBSITE_MODE == 'test' || !isset($aSession['lang'])) {
        self::$_aLang = array();

        if ($aSession != null)
          $aSession['lang'] = & I18n::$_aLang;
      }
      # use the already loaded session stuff
      else
        self::$_aLang = & $aSession['lang'];

      # load the default language
      self::load($sLanguage, $aPlugins);
    }
  }

  /**
   *
   * Load a language to internal language cache
   *
   * @access public
   * @param string $sLanguage the language to load
   * @param array $aPlugins plugins to load language files from
   * @return boolean whether language could be loaded
   *
   */
  public static function load($sLanguage, $aPlugins = array()) {
    # Already loaded?
    if (isset(I18n::$_aLang[$sLanguage])) {
      self::$_sLanguage = $sLanguage;
      SmartySingleton::getInstance()->setDefaultLanguage(self::$_aLang[$sLanguage], $sLanguage);
      return true;
    }

    # Have to load from YML files
    $sLanguageFile        = $sLanguage . '.yml';
    $sCustomLanguageFile  = PATH_STANDARD . '/app/languages/' . $sLanguageFile;
    $sCoreLanguageFile    = PATH_STANDARD . '/vendor/candyCMS/core/languages/' . $sLanguageFile;

    # Language does not exist
    if (!file_exists($sCustomLanguageFile))
      return false;

    # Load the core language file
    self::$_aLang[$sLanguage] = \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sCoreLanguageFile));

    # Load the plugin language files and merge them
    $sPluginPath = PATH_STANDARD . '/vendor/candyCMS/plugins/';

    foreach ($aPlugins as $sPlugin) {
      if (file_exists($sPluginPath . $sPlugin . '/languages')) {
        $aPluginLang = file_exists($sPluginPath . $sPlugin . '/languages/' . $sLanguageFile) ?
                \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sPluginPath . $sPlugin . '/languages/' . $sLanguageFile)) :
                \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sPluginPath . $sPlugin . '/languages/en.yml'));

        Helper::recursiveOnewayArrayReplace(self::$_aLang[$sLanguage], $aPluginLang);
      }
    }

    # Load the extension language files and merge them
    if (EXTENSION_CHECK) {
      $sExtensionLanguagePath = PATH_STANDARD . '/app/extensions/languages/';
      $oDir = opendir($sExtensionLanguagePath);

      while ($sFile = readdir($oDir)) {
        if ($sFile == '.' || $sFile == '..')
          continue;

        $aExtensionLang = file_exists($sExtensionLanguagePath . $sFile . '/' . $sLanguageFile) ?
                \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sExtensionLanguagePath . $sFile . '/' . $sLanguageFile)) :
                \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sExtensionLanguagePath . $sFile . '/en.yml'));

        Helper::recursiveOnewayArrayReplace(self::$_aLang[$sLanguage], $aExtensionLang);
      }
    }

    # Merge all that with the users custom language file
    Helper::recursiveOnewayArrayReplace(I18n::$_aLang,
            \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sCustomLanguageFile)));

    self::$_sLanguage = $sLanguage;
    SmartySingleton::getInstance()->setDefaultLanguage(self::$_aLang[$sLanguage], $sLanguage);

    return true;
  }

  /**
   * Return the language array.
   *
   * @static
   * @access public
   * @param string $sPart main part of the array to return string from
   * @return array $_SESSION['lang'] session array with language data<commit_msg>removed extension languages. they are now under standard<commit_after><?php

/**
 * Translate a string.
 *
 * @link http://github.com/marcoraddatz/candyCMS
 * @author <NAME> <http://marcoraddatz.com>
 * @license MIT
 * @since 2.0
 *
 */

namespace CandyCMS\Core\Helpers;

use CandyCMS\Core\Helpers\AdvancedException;

class I18n {

  /**
   *
   * Holds all translations
   *
   * @var static
   * @access private
   *
   */
  private static $_aLang = null;

  /**
   *
   * Holds the object
   *
   * @var static
   * @access private
   *
   */
  private static $_oObject = null;

  /**
  *
  * Holds the wanted Language
  *
  * @var static
  * @access private
  *
  */
  private static $_sLanguage = null;

  /**
   * Read the language yaml and save information into session due to fast access.
   *
   * @access public
   * @param string $sLanguage language to load
   * @param array $aSession the session object, if given save the translations in S_SESSION['lang']
   * @param array $aPlugins plugins to load
   *
   */
  public function __construct($sLanguage = 'en', &$aSession = null, $aPlugins = array()) {
    if ($aSession)
      $this->_aSession = $aSession;

    I18n::$_oObject = $this;

    # first call
    if (!isset(I18n::$_aLang) || WEBSITE_MODE == 'development' || WEBSITE_MODE == 'test') {
      $sLanguageFile = $sLanguage . '.yml';
      $sLanguagePath = PATH_STANDARD . '/app/languages/' . $sLanguageFile;

      # Remove mistakenly set cookie to avoid exceptions.
      if (!file_exists($sLanguagePath))
        $_COOKIE['default_language'] = 'en';

      # reload the files, if necessary
      if (WEBSITE_MODE == 'development' || WEBSITE_MODE == 'test' || !isset($aSession['lang'])) {
        self::$_aLang = array();

        if ($aSession != null)
          $aSession['lang'] = & I18n::$_aLang;
      }
      # use the already loaded session stuff
      else
        self::$_aLang = & $aSession['lang'];

      # load the default language
      self::load($sLanguage, $aPlugins);
    }
  }

  /**
   *
   * Load a language to internal language cache
   *
   * @access public
   * @param string $sLanguage the language to load
   * @param array $aPlugins plugins to load language files from
   * @return boolean whether language could be loaded
   *
   */
  public static function load($sLanguage, $aPlugins = array()) {
    # Already loaded?
    if (isset(I18n::$_aLang[$sLanguage])) {
      self::$_sLanguage = $sLanguage;
      SmartySingleton::getInstance()->setDefaultLanguage(self::$_aLang[$sLanguage], $sLanguage);
      return true;
    }

    # Have to load from YML files
    $sLanguageFile        = $sLanguage . '.yml';
    $sCustomLanguageFile  = PATH_STANDARD . '/app/languages/' . $sLanguageFile;
    $sCoreLanguageFile    = PATH_STANDARD . '/vendor/candyCMS/core/languages/' . $sLanguageFile;

    # Language does not exist
    if (!file_exists($sCustomLanguageFile))
      return false;

    # Load the core language file
    self::$_aLang[$sLanguage] = \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sCoreLanguageFile));

    # Load the plugin language files and merge them
    $sPluginPath = PATH_STANDARD . '/vendor/candyCMS/plugins/';

    foreach ($aPlugins as $sPlugin) {
      if (file_exists($sPluginPath . $sPlugin . '/languages')) {
        $aPluginLang = file_exists($sPluginPath . $sPlugin . '/languages/' . $sLanguageFile) ?
                \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sPluginPath . $sPlugin . '/languages/' . $sLanguageFile)) :
                \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sPluginPath . $sPlugin . '/languages/en.yml'));

        Helper::recursiveOnewayArrayReplace(self::$_aLang[$sLanguage], $aPluginLang);
      }
    }

    # Merge all that with the users custom language file
    Helper::recursiveOnewayArrayReplace(I18n::$_aLang,
            \Symfony\Component\Yaml\Yaml::parse(file_get_contents($sCustomLanguageFile)));

    self::$_sLanguage = $sLanguage;
    SmartySingleton::getInstance()->setDefaultLanguage(self::$_aLang[$sLanguage], $sLanguage);

    return true;
  }

  /**
   * Return the language array.
   *
   * @static
   * @access public
   * @param string $sPart main part of the array to return string from
   * @return array $_SESSION['lang'] session array with language data
{'fraction_non_alphanumeric': 0.134409751924722, 'fraction_numerical': 0.0029940119760479044, 'mean_word_length': 3.0210662080825452, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '5684235', 'n_tokens_mistral': 3008, 'n_tokens_neox': 2881, 'n_words': 686}