I18n::loadLocaleDefinition PHP Method

loadLocaleDefinition() public static method

Parses a locale definition file following the POSIX standard
public static loadLocaleDefinition ( string $filename ) : mixed
$filename string Locale definition filename
return mixed Array of definitions on success or false on failure
    public static function loadLocaleDefinition($filename)
    {
        if (!($file = fopen($filename, 'r'))) {
            return false;
        }
        $definitions = array();
        $comment = '#';
        $escape = '\\';
        $currentToken = false;
        $value = '';
        $_this = I18n::getInstance();
        while ($line = fgets($file)) {
            $line = trim($line);
            if (empty($line) || $line[0] === $comment) {
                continue;
            }
            $parts = preg_split("/[[:space:]]+/", $line);
            if ($parts[0] === 'comment_char') {
                $comment = $parts[1];
                continue;
            }
            if ($parts[0] === 'escape_char') {
                $escape = $parts[1];
                continue;
            }
            $count = count($parts);
            if ($count === 2) {
                $currentToken = $parts[0];
                $value = $parts[1];
            } elseif ($count === 1) {
                $value = is_array($value) ? $parts[0] : $value . $parts[0];
            } else {
                continue;
            }
            $len = strlen($value) - 1;
            if ($value[$len] === $escape) {
                $value = substr($value, 0, $len);
                continue;
            }
            $mustEscape = array($escape . ',', $escape . ';', $escape . '<', $escape . '>', $escape . $escape);
            $replacements = array_map('crc32', $mustEscape);
            $value = str_replace($mustEscape, $replacements, $value);
            $value = explode(';', $value);
            $_this->_escape = $escape;
            foreach ($value as $i => $val) {
                $val = trim($val, '"');
                $val = preg_replace_callback('/(?:<)?(.[^>]*)(?:>)?/', array(&$_this, '_parseLiteralValue'), $val);
                $val = str_replace($replacements, $mustEscape, $val);
                $value[$i] = $val;
            }
            if (count($value) === 1) {
                $definitions[$currentToken] = array_pop($value);
            } else {
                $definitions[$currentToken] = $value;
            }
        }
        return $definitions;
    }

Usage Example

Example #1
0
 /**
  * testLoadLocaleDefinition method
  *
  * @return void
  */
 public function testLoadLocaleDefinition()
 {
     $path = current(App::path('locales'));
     $result = I18n::loadLocaleDefinition($path . 'nld' . DS . 'LC_TIME');
     $expected = array('zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag');
     $this->assertSame($expected, $result['day']);
 }