ICanBoogie/I18n master
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • I18n
      • Translator

Classes

  • FormattedString
  • Helpers
  • Locale
  • NumberFormatter
  • Translator

Functions

  • date_period
  • format_currency
  • format_date
  • format_datetime
  • format_number
  • format_size
  • format_time
  • get_cldr
  • get_language
  • get_locale
  • set_locale
  • t
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie\I18n;

/**
 * Returns a locale.
 *
 * @param string $id Idenfitier of the locale or `null` to retrieve the current locale.
 *
 * @return \ICanBoogie\I18n\Locale
 */
function get_locale($id=null)
{
    return Helpers::get_locale($id);
}

/**
 * Sets the current locale.
 *
 * @param string $id Locale identifier.
 *
 * @return \ICanBoogie\I18n\Locale
 */
function set_locale($id)
{
    return Helpers::set_locale($id);
}

/**
 * Returns the language of the current locale.
 *
 * @return string The language of the current locale or `null` if there is none.
 */
function get_language()
{
    return Helpers::get_language();
}

/**
 * Returns the CLDR representation.
 *
 * @return \ICanBoogie\CLDR\Repository
 */
function get_cldr()
{
    return Helpers::get_cldr();
}

/**
 * Translates a string using the current locale.
 *
 * @param string $str The native string to translate.
 * @param array $args Arguments used to format the translated string.
 * @param array $options Options for the translation.
 *
 * @return string The translated string.
 */
function t($str, array $args=array(), array $options=array())
{
    return Helpers::t($str, $args, $options);
}

/**
 * Formats a size in "b", "Kb", "Mb", "Gb" or "Tb".
 *
 * @param int $size
 *
 * @return string
 */
function format_size($size)
{
    if ($size < 1024)
    {
        $str = ":size\xC2\xA0b";
    }
    else if ($size < 1024 * 1024)
    {
        $str = ":size\xC2\xA0Kb";
        $size = $size / 1024;
    }
    else if ($size < 1024 * 1024 * 1024)
    {
        $str = ":size\xC2\xA0Mb";
        $size = $size / (1024 * 1024);
    }
    else if ($size < 1024 * 1024 * 1024 * 1024)
    {
        $str = ":size\xC2\xA0Gb";
        $size = $size / (1024 * 1024 * 1024);
    }
    else
    {
        $str = ":size\xC2\xA0Tb";
        $size = $size / (1024 * 1024 * 1024 * 1024);
    }

    return t($str, array(':size' => round($size)));
}

function format_number($number)
{
    $locale = get_locale();
    $decimal_point = $locale['numbers']['symbols-numberSystem-' . $locale['numbers']['defaultNumberingSystem']]['decimal'];
    $thousands_sep = ' ';

    return number_format($number, ($number - floor($number) < .009) ? 0 : 2, $decimal_point, $thousands_sep);
}

function format_currency($value, $currency)
{
    return get_locale()->number_formatter->format_currency($value, $currency);
}

/**
 * Formats a date.
 *
 * @param mixed $datetime
 * @param string $pattern_or_width
 *
 * @see \ICanBoogie\CLDR\DateFormatter
 */
function format_date($datetime, $pattern_or_width='medium')
{
    return get_locale()->calendar->date_formatter->format($datetime, $pattern_or_width);
}

/**
 * Formats a time.
 *
 * @param mixed $datetime
 * @param string $pattern_or_width
 *
 * @see \ICanBoogie\CLDR\TimeFormatter
 */
function format_time($datetime, $pattern_or_width='medium')
{
    return get_locale()->calendar->time_formatter->format($datetime, $pattern_or_width);
}

/**
 * Formats a date and time.
 *
 * @param mixed $datetime
 * @param string $pattern_or_width_or_skeleton
 *
 * @see \ICanBoogie\CLDR\DateTimeFormatter
 */
function format_datetime($datetime, $pattern_or_width_or_skeleton='medium')
{
    return get_locale()->calendar->datetime_formatter->format($datetime, $pattern_or_width_or_skeleton);
}

function date_period($date)
{
    static $relative;

    if (is_numeric($date))
    {
        $date_secs = $date;
        $date = date('Y-m-d', $date);
    }
    else
    {
        $date_secs = strtotime($date);
    }

    $today_days = strtotime(date('Y-m-d')) / (60 * 60 * 24);
    $date_days = strtotime(date('Y-m-d', $date_secs)) / (60 * 60 * 24);

    $diff = round($date_days - $today_days);
    $locale_id = get_language();

    if (empty($relative[$locale_id]))
    {
        $locale = get_locale();
        $relative[$locale_id] = $locale['dateFields']['day'];
    }

    if (isset($relative[$locale_id]["relative-type-{$diff}"]))
    {
        return $relative[$locale_id]["relative-type-{$diff}"];
    }
    else if ($diff > -6)
    {
        return ucfirst(format_date($date_secs, 'EEEE'));
    }

    return format_date($date);
}

/**
 * Patchable helpers of the ICanBoogie/I18n package.
 */
class Helpers
{
    static private $jumptable = array
    (
        'get_locale' => array(__CLASS__, 'get_locale'),
        'set_locale' => array(__CLASS__, 'set_locale'),
        'get_language' => array(__CLASS__, 'get_language'),
        'get_cldr' => array(__CLASS__, 'get_cldr'),
        't' => array(__CLASS__, 't')
    );

    /**
     * Calls the callback of a patchable function.
     *
     * @param string $name Name of the function.
     * @param array $arguments Arguments.
     *
     * @return mixed
     */
    static public function __callstatic($name, array $arguments)
    {
        return call_user_func_array(self::$jumptable[$name], $arguments);
    }

    /**
     * Patches a patchable function.
     *
     * @param string $name Name of the function.
     * @param collable $callback Callback.
     *
     * @throws \RuntimeException is attempt to patch an undefined function.
     */
    static public function patch($name, $callback)
    {
        if (empty(self::$jumptable[$name]))
        {
            throw new \RuntimeException("Undefined patchable: $name.");
        }

        self::$jumptable[$name] = $callback;
    }

    /*
     * Default implementations
     */

    /**
     * Current locale.
     *
     * @var \ICanBoogie\I18n\Locale
     */
    static private $locale;

    static private function get_locale($id=null)
    {
        return $id ? Locale::from($id) : (self::$locale ? self::$locale : self::$locale = Locale::from('en'));
    }

    static private function set_locale($id)
    {
        return self::$locale = Locale::from($id);
    }

    static private function get_language()
    {
        return self::$locale ? self::$locale->language : null;
    }

    static private function get_cldr()
    {
        static $cldr;

        if (!$cldr)
        {
            $provider = new \ICanBoogie\CLDR\Provider
            (
                new \ICanBoogie\CLDR\RunTimeCache(new \ICanBoogie\CLDR\FileCache(REPOSITORY)),
                new \ICanBoogie\CLDR\Retriever
            );

            $cldr = new \ICanBoogie\CLDR\Repository($provider);
        }

        return $cldr;
    }

    static private function t($str, array $args=array(), array $options=array())
    {
        $locale = get_locale(empty($options['language']) ? null : $options['language']);

        return $locale->translator($str, $args, $options);
    }
}
ICanBoogie/I18n master API documentation generated by ApiGen