summaryrefslogtreecommitdiff
path: root/core/translator.php
diff options
context:
space:
mode:
authorFrédéric Guillot <fguillot@users.noreply.github.com>2014-03-09 23:21:23 -0400
committerFrédéric Guillot <fguillot@users.noreply.github.com>2014-03-09 23:21:23 -0400
commit7749b8ed569f6d27b0bb2ed4c2040e8b61ed4422 (patch)
treeee101992e87d740bdf0362e35ea040c866986f5a /core/translator.php
parent7bd4697dfca41a21f5857f83d6b29108fafb9a1e (diff)
Automatic actions
Diffstat (limited to 'core/translator.php')
-rw-r--r--core/translator.php122
1 files changed, 122 insertions, 0 deletions
diff --git a/core/translator.php b/core/translator.php
new file mode 100644
index 00000000..75d40a23
--- /dev/null
+++ b/core/translator.php
@@ -0,0 +1,122 @@
+<?php
+
+namespace Translator {
+
+ const PATH = 'locales/';
+
+ function translate($identifier)
+ {
+ $args = \func_get_args();
+
+ \array_shift($args);
+ \array_unshift($args, get($identifier, $identifier));
+
+ foreach ($args as &$arg) {
+ $arg = htmlspecialchars($arg, ENT_QUOTES, 'UTF-8', false);
+ }
+
+ return \call_user_func_array(
+ 'sprintf',
+ $args
+ );
+ }
+
+ function number($number)
+ {
+ return number_format(
+ $number,
+ get('number.decimals', 2),
+ get('number.decimals_separator', '.'),
+ get('number.thousands_separator', ',')
+ );
+ }
+
+ function currency($amount)
+ {
+ $position = get('currency.position', 'before');
+ $symbol = get('currency.symbol', '$');
+ $str = '';
+
+ if ($position === 'before') {
+ $str .= $symbol;
+ }
+
+ $str .= number($amount);
+
+ if ($position === 'after') {
+ $str .= ' '.$symbol;
+ }
+
+ return $str;
+ }
+
+ function datetime($format, $timestamp)
+ {
+ return strftime(get($format, $format), (int) $timestamp);
+ }
+
+ function get($identifier, $default = '')
+ {
+ $locales = container();
+
+ if (isset($locales[$identifier])) {
+ return $locales[$identifier];
+ }
+ else {
+ return $default;
+ }
+ }
+
+ function load($language)
+ {
+ setlocale(LC_TIME, $language.'.UTF-8');
+
+ $path = PATH.$language;
+ $locales = array();
+
+ if (is_dir($path)) {
+
+ $dir = new \DirectoryIterator($path);
+
+ foreach ($dir as $fileinfo) {
+
+ if (strpos($fileinfo->getFilename(), '.php') !== false) {
+ $locales = array_merge($locales, include $fileinfo->getPathname());
+ }
+ }
+ }
+
+ container($locales);
+ }
+
+ function container($locales = null)
+ {
+ static $values = array();
+
+ if ($locales !== null) {
+ $values = $locales;
+ }
+
+ return $values;
+ }
+}
+
+
+namespace {
+
+ function t() {
+ return call_user_func_array('\Translator\translate', func_get_args());
+ }
+
+ function c() {
+ return call_user_func_array('\Translator\currency', func_get_args());
+ }
+
+ function n() {
+ return call_user_func_array('\Translator\number', func_get_args());
+ }
+
+ function dt() {
+ return call_user_func_array('\Translator\datetime', func_get_args());
+ }
+}