diff options
author | Aleksander Machniak <alec@alec.pl> | 2012-02-16 10:34:14 (GMT) |
---|---|---|
committer | Aleksander Machniak <alec@alec.pl> | 2012-02-16 10:34:14 (GMT) |
commit | 224f0fa412408c2d1e1e59c20fbbf575d1ef6b31 (patch) | |
tree | 9c81a96c18cf8b61e9793122a822de617cf42541 | |
parent | 6664b058c428e86511c5330f735df3d7752b6fe8 (diff) | |
download | kolab-wap-224f0fa412408c2d1e1e59c20fbbf575d1ef6b31.tar.gz |
API localization
-rw-r--r-- | lib/api/kolab_group_actions.php | 2 | ||||
-rw-r--r-- | lib/api/kolab_user_actions.php | 4 | ||||
-rw-r--r-- | lib/kolab_api_controller.php | 64 | ||||
-rw-r--r-- | lib/kolab_client_task.php | 2 | ||||
-rw-r--r-- | lib/locale/en_US.api.php | 3 |
5 files changed, 68 insertions, 7 deletions
diff --git a/lib/api/kolab_group_actions.php b/lib/api/kolab_group_actions.php index be1f778..7c99e91 100644 --- a/lib/api/kolab_group_actions.php +++ b/lib/api/kolab_group_actions.php @@ -31,7 +31,7 @@ class kolab_group_actions extends kolab_api_service if (isset($gta['form_fields'])) { foreach ($gta['form_fields'] as $key => $value) { error_log("form field $key"); - if (!isset($postdata[$key]) || empty($postdata[$key])) { + if (!isset($postdata[$key]) || $postdata[$key] === '') { throw new Exception("Missing input value for $key", 345); } else { diff --git a/lib/api/kolab_user_actions.php b/lib/api/kolab_user_actions.php index c39d89e..ed4de66 100644 --- a/lib/api/kolab_user_actions.php +++ b/lib/api/kolab_user_actions.php @@ -22,10 +22,10 @@ class kolab_user_actions extends kolab_api_service public function user_add($getdata, $postdata) { if (!isset($postdata['user_type_id'])) { - throw new Exception("No user type ID specified", 346781); + throw new Exception($this->controller::translate('user.notypeid'), 346781); } - $sql_result = query("SELECT attributes FROM user_types WHERE id = ?", $postdata['user_type_id']); + $sql_result = $this->db->query("SELECT attributes FROM user_types WHERE id = ?", $postdata['user_type_id']); $user_type = mysql_fetch_assoc($sql_result); $uta = json_decode(unserialize($user_type['attributes']), true); diff --git a/lib/kolab_api_controller.php b/lib/kolab_api_controller.php index 5401609..57922bd 100644 --- a/lib/kolab_api_controller.php +++ b/lib/kolab_api_controller.php @@ -11,6 +11,7 @@ class kolab_api_controller private $request = array(); private $services = array(); private $domains = array('localhost.localdomain'); + private static $translation = array(); public function __construct() { @@ -24,7 +25,7 @@ class kolab_api_controller ); } else { - throw new Exception("Unknown methods", 400); + throw new Exception("Unknown method", 400); } } else { @@ -117,6 +118,9 @@ class kolab_api_controller } } + // init localization + $this->locale_init(); + // call service method $service_handler = $this->get_service($service); @@ -150,7 +154,7 @@ class kolab_api_controller $method = $this->request['method']; $url .= '/' . $service . '.' . $method; - console("Proxying to " . $url); + console("Proxying " . $url); $request = new HTTP_Request2(); $url = new Net_URL2($url); @@ -295,7 +299,7 @@ class kolab_api_controller } /** - * End the current user ession + * End the current user session */ private function quit() { @@ -316,4 +320,58 @@ class kolab_api_controller /* ======== Utility functions ======== */ + /** + * Localization initialization. + */ + private function locale_init() + { + // @TODO: read language from logged user data + $lang = 'en_US'; + + if ($lang != 'en_US' && file_exists(INSTALL_PATH . "/locale/$lang.api.php")) { + $language = $lang; + } + + $LANG = array(); + @include INSTALL_PATH . '/locale/en_US.api.php'; + + if (isset($language)) { + @include INSTALL_PATH . "/locale/$language.api.php"; + setlocale(LC_ALL, $language . '.utf8', 'en_US.utf8'); + } + else { + setlocale(LC_ALL, 'en_US.utf8'); + } + + self::$translation = $LANG; + } + + /** + * Returns translation of defined label/message. + * + * @return string Translated string. + */ + public static function translate() + { + $args = func_get_args(); + + if (is_array($args[0])) { + $args = $args[0]; + } + + $label = $args[0]; + + if (isset(self::$translation[$label])) { + $content = trim(self::$translation[$label]); + } + else { + $content = $label; + } + + for ($i = 1, $len = count($args); $i < $len; $i++) { + $content = str_replace('$'.$i, $args[$i], $content); + } + + return $content; + } } diff --git a/lib/kolab_client_task.php b/lib/kolab_client_task.php index 9ab856d..3dc6770 100644 --- a/lib/kolab_client_task.php +++ b/lib/kolab_client_task.php @@ -74,7 +74,7 @@ class kolab_client_task $LANG = array(); @include INSTALL_PATH . '/locale/en_US.php'; - if (isset($language)) { + if (!empty($language) && $language != 'en_US') { @include INSTALL_PATH . "/locale/$language.php"; setlocale(LC_ALL, $language . '.utf8', 'en_US.utf8'); } diff --git a/lib/locale/en_US.api.php b/lib/locale/en_US.api.php new file mode 100644 index 0000000..4e43a72 --- /dev/null +++ b/lib/locale/en_US.api.php @@ -0,0 +1,3 @@ +<?php + +$LANG['user.notypeid'] = 'No user type ID specified!'; |