<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  /**
   * Locale
   * 
   * Usage [retreiving default locale]
   * <code>
   *   $locale= &Locale::getDefault();
   *   var_dump($locale);
   * </code>
   *
   * Usage [setting default locale]
   * <code>
   *   Locale::setDefault(new Locale('de_DE'));
   * </code>
   *
   * @see      http://ftp.ics.uci.edu/pub/ietf/http/related/iso639.txt
   * @see      http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
   * @see      http://groups.google.com/groups?threadm=DREPPER.96Aug8030605%40i44d2.ipd.info.uni-karlsruhe.de#link1
   * @purpose  Represent a locale
   */
  class Locale extends Object {
    var
      $lang     = '',
      $country  = '',
      $variant  = '';
    
    var
      $_str     = '';

    /**
     * Constructor
     *
     * @access  public
     * @param   string lang 2-letter abbreviation of language
     * @param   string country 2-letter abbreviation of country
     * @param   string variant default ''
     * @throws  lang.IllegalArgumentException in case the locale is not available
     */
    function __construct() {
      switch (func_num_args()) {
        case 1: 
          sscanf(func_get_arg(0), '%2s_%2s%s', $this->lang, $this->country, $this->variant);
          break;
          
        case 2:
          list($this->lang, $this->country)= func_get_args();
          break;
          
        case 3:
          list($this->lang, $this->country, $this->variant)= func_get_args();
          break;
      }
      
      // Try to set the locale using different variations. If all fail, 
      // throw an IllegalArgumentException.
      $current= setlocale(LC_ALL, NULL);
      $this->_str= setlocale(LC_ALL, array(
        $this->lang.'_'.$this->country.$this->variant,
        $this->lang.'_'.$this->country,
        $this->lang,
        $this->lang.'_'.$this->country.'.'.$this->variant,
        $this->lang.'_'.$this->country.'@'.$this->variant
      ));
      setlocale(LC_ALL, $current);
      if (FALSE === $this->_str) {
        return throw(new IllegalArgumentException(sprintf(
          'Locale [lang=%s,country=%s,variant=%s] not available',
          $this->lang, 
          $this->country, 
          ltrim($this->variant, '.@')
        )));
      }
    }
    
    /**
     * Get default locale
     *
     * @model   static
     * @access  public
     * @return  &util.Locale
     */
    function &getDefault() {
      return new Locale(('C' == ($locale= setlocale(LC_ALL, NULL)) 
        ? 'en_US'
        : $locale
      ));
    }
    
    /**
     * Set default locale for this script
     *
     * @model   static
     * @access  public
     * @param   &util.Locale locale
     */
    function setDefault(&$locale) {
      setlocale(LC_ALL, $locale->toString());
    }

    /**
     * Get Language
     *
     * @access  public
     * @return  string
     */
    function getLanguage() {
      return $this->lang;
    }

    /**<commit_msg>- Only throw exceptions when actually setting the locale<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  /**
   * Locale
   * 
   * Usage [retreiving default locale]
   * <code>
   *   $locale= &Locale::getDefault();
   *   var_dump($locale);
   * </code>
   *
   * Usage [setting default locale]
   * <code>
   *   Locale::setDefault(new Locale('de_DE'));
   * </code>
   *
   * @see      http://ftp.ics.uci.edu/pub/ietf/http/related/iso639.txt
   * @see      http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.html
   * @see      http://groups.google.com/groups?threadm=DREPPER.96Aug8030605%40i44d2.ipd.info.uni-karlsruhe.de#link1
   * @purpose  Represent a locale
   */
  class Locale extends Object {
    var
      $lang     = '',
      $country  = '',
      $variant  = '';
    
    var
      $_str     = '';

    /**
     * Constructor
     *
     * @access  public
     * @param   string lang 2-letter abbreviation of language
     * @param   string country 2-letter abbreviation of country
     * @param   string variant default ''
     */
    function __construct() {
      switch (func_num_args()) {
        case 1: 
          $this->_str= func_get_arg(0);
          sscanf(func_get_arg(0), '%2s_%2s%s', $this->lang, $this->country, $this->variant);
          break;
          
        case 2:
          list($this->lang, $this->country)= func_get_args();
          $this->_str= $this->lang.'_'.$this->country;
          break;
          
        case 3:
          list($this->lang, $this->country, $this->variant)= func_get_args();
          $this->_str= $this->lang.'_'.$this->country.'@'.$this->variant;
          break;
      }
    }
    
    /**
     * Get default locale
     *
     * @model   static
     * @access  public
     * @return  &util.Locale
     */
    function &getDefault() {
      return new Locale(('C' == ($locale= setlocale(LC_ALL, NULL)) 
        ? 'en_US'
        : $locale
      ));
    }
    
    /**
     * Set default locale for this script
     *
     * @model   static
     * @access  public
     * @param   &util.Locale locale
     * @throws  lang.IllegalArgumentException in case the locale is not available
     */
    function setDefault(&$locale) {
      if (FALSE === setlocale(LC_ALL, $locale->toString())) {
        return throw(new IllegalArgumentException(sprintf(
          'Locale [lang=%s,country=%s,variant=%s] not available',
          $this->lang, 
          $this->country, 
          ltrim($this->variant, '.@')
        )));
      }
    }

    /**
     * Get Language
     *
     * @access  public
     * @return  string
     */
    function getLanguage() {
      return $this->lang;
    }

    /**
{'fraction_non_alphanumeric': 0.1528315160042209, 'fraction_numerical': 0.010728104115371086, 'mean_word_length': 1.8970962812022414, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 59, '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': '2465467', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1818, 'n_words': 364}