ICanBoogie/I18n 2.0.x
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • Binding
      • I18n
    • I18n
      • Translator

Classes

  • FormattedString
  • Helpers
  • Hooks
  • Translator

Functions

  • date_period
  • format_currency
  • format_date
  • format_datetime
  • format_number
  • format_size
  • format_time
  • get_cldr
  • get_language
  • get_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 
<?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;

use ICanBoogie\CLDR\Currency;

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

/**
 * Sets the current locale.
 *
 * @param string $id Locale identifier.
 *
 * @return \ICanBoogie\CLDR\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 $options=[])
{
    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, [ ':size' => round($size) ]);
}

/**
 * Formats a number.
 *
 * @param $number
 * @param string|null $pattern
 *
 * @return string
 */
function format_number($number, $pattern=null)
{
    return get_locale()->format_number($number, $pattern);
}

/**
 * Formats a currency using the current locale.
 *
 * @param number $number
 * @param string|Currency $currency The currency code or a {@link Currency} instance.
 *
 * @return string
 */
function format_currency($number, $currency)
{
    if (!$currency instanceof Currency)
    {
        $currency = get_cldr()->currencies[$currency];
    }

    $localized_currency = get_locale()->localize($currency);

    return $localized_currency->format($number);
}

/**
 * Formats a date.
 *
 * @param mixed $datetime
 * @param string $pattern_or_width
 *
 * @return string
 *
 * @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
 *
 * @return string
 *
 * @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
 *
 * @return string
 *
 * @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);
}
ICanBoogie/I18n 2.0.x API documentation generated by ApiGen