summaryrefslogtreecommitdiff
path: root/include/MyLocale.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/MyLocale.class.php')
-rw-r--r--include/MyLocale.class.php89
1 files changed, 89 insertions, 0 deletions
diff --git a/include/MyLocale.class.php b/include/MyLocale.class.php
new file mode 100644
index 0000000..49d6856
--- /dev/null
+++ b/include/MyLocale.class.php
@@ -0,0 +1,89 @@
+<?php
+
+class MyLocale {
+
+ static private $allowedLangs = [];
+ static private $_currentLocale = '';
+
+ static function init() {
+ date_default_timezone_set(Env::get('locale','timezone'));
+ self::setLocale();
+ }
+
+ static function getAllowedLangs() {
+ if (!count(self::$allowedLangs)) {
+ self::$allowedLangs = Env::get('locale', 'languages');
+ }
+
+ return self::$allowedLangs;
+ }
+
+ static function getPreferredLang($allowed_languages = [], $deflang = '') {
+ if (count($allowed_languages)==0) {
+ $allowed_languages = self::getAllowedLangs();
+ }
+
+ if ($deflang=='') {
+ $deflang = Env::get('locale','default-language');
+ }
+
+ if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+ $accepted = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
+ foreach ($accepted as $usrlang) {
+ foreach ($allowed_languages as $pagelang) {
+ if ($pagelang === substr($usrlang, 0, strlen($pagelang))) {
+ return $pagelang;
+ }
+ }
+ }
+ }
+ elseif ($deflang) {
+ return $deflang;
+ }
+ return $allowed_languages[0];
+ }
+
+ /**
+ * sets current locale environment. It partially duplicates functionality of Env::lang()
+ * in regards of storing current settings.
+ *
+ * @param langid - new language code. If null or empty string, default language will be
+ * guessed by using Env::lang() method.
+ *
+ * @return void
+ */
+ static function setLocale($langid='') {
+ if ($langid=='') {
+ $langid = Env::lang();
+ }
+ if (self::$_currentLocale==$langid) {
+ return;
+ }
+ self::$_currentLocale = $langid;
+ bindtextdomain('main', BASEPATH.'/locale');
+ bind_textdomain_codeset('main','utf-8');
+ textdomain('main');
+ try {
+ $localeId = Env::get('locale', 'locale-id', $langid);
+ }
+ catch (EnvNoSuchEntryException $e) {
+ $localeId = Env::get('locale', 'locale-id', Env::get('locale', 'default-language'));
+ }
+
+ setlocale(LC_MESSAGES, $localeId);
+ setlocale(LC_CTYPE, $localeId);
+ setlocale(LC_NUMERIC, $localeId);
+ setlocale(LC_MONETARY, $localeId);
+ setlocale(LC_TIME, $localeId);
+ putenv('LANGUAGE=' . $localeId);
+ Env::setLang($langid);
+ }
+
+ static function formatCurrency($value, $currency) {
+ $locale = localeconv();
+ $f = number_format(round($value, 2), 2, null, ' ');
+ return sprintf(($locale['p_cs_precedes'] ? '%2$s %1$s' : '%s %s'),
+ $f, Currency::getCurrencySymbol($currency));
+ }
+}
+?>