Replace const CURRENT_LOCALE with get_current_locale() func

This commit is contained in:
Dmitriy Simushev 2014-07-11 14:26:14 +00:00
parent e3e25f78d7
commit 803863571b
17 changed files with 82 additions and 64 deletions

View File

@ -765,7 +765,7 @@ function chat_start_for_user(
$thread->userName = $visitor_name; $thread->userName = $visitor_name;
$thread->remote = $remote_host; $thread->remote = $remote_host;
$thread->referer = $referrer; $thread->referer = $referrer;
$thread->locale = CURRENT_LOCALE; $thread->locale = get_current_locale();
$thread->userId = $visitor_id; $thread->userId = $visitor_id;
$thread->userAgent = $user_browser; $thread->userAgent = $user_browser;
$thread->save(); $thread->save();
@ -786,7 +786,7 @@ function chat_start_for_user(
getlocal( getlocal(
'Visitor accepted invitation from operator {0}', 'Visitor accepted invitation from operator {0}',
array($operator_name), array($operator_name),
CURRENT_LOCALE, get_current_locale(),
true true
) )
); );
@ -794,7 +794,7 @@ function chat_start_for_user(
if ($referrer) { if ($referrer) {
$thread->postMessage( $thread->postMessage(
Thread::KIND_FOR_AGENT, Thread::KIND_FOR_AGENT,
getlocal('Vistor came from page {0}', array($referrer), CURRENT_LOCALE, true) getlocal('Vistor came from page {0}', array($referrer), get_current_locale(), true)
); );
} }
if ($requested_operator && !$requested_operator_online) { if ($requested_operator && !$requested_operator_online) {
@ -803,14 +803,14 @@ function chat_start_for_user(
getlocal( getlocal(
'Thank you for contacting us. We are sorry, but requested operator <strong>{0}</strong> is offline. Another operator will be with you shortly.', 'Thank you for contacting us. We are sorry, but requested operator <strong>{0}</strong> is offline. Another operator will be with you shortly.',
array(get_operator_name($requested_operator)), array(get_operator_name($requested_operator)),
CURRENT_LOCALE, get_current_locale(),
true true
) )
); );
} else { } else {
$thread->postMessage( $thread->postMessage(
Thread::KIND_INFO, Thread::KIND_INFO,
getlocal('Thank you for contacting us. An operator will be with you shortly.', null, CURRENT_LOCALE, true) getlocal('Thank you for contacting us. An operator will be with you shortly.', null, get_current_locale(), true)
); );
} }
} }
@ -819,7 +819,7 @@ function chat_start_for_user(
if ($info) { if ($info) {
$thread->postMessage( $thread->postMessage(
Thread::KIND_FOR_AGENT, Thread::KIND_FOR_AGENT,
getlocal('Info: {0}', array($info), CURRENT_LOCALE, true) getlocal('Info: {0}', array($info), get_current_locale(), true)
); );
} }

View File

@ -96,8 +96,8 @@ class ButtonCodeController extends AbstractController
$disable_invitation = false; $disable_invitation = false;
if (!$lang || !in_array($lang, $image_locales)) { if (!$lang || !in_array($lang, $image_locales)) {
$lang = in_array(CURRENT_LOCALE, $image_locales) $lang = in_array(get_current_locale(), $image_locales)
? CURRENT_LOCALE ? get_current_locale()
: $image_locales[0]; : $image_locales[0];
} }
@ -120,7 +120,9 @@ class ButtonCodeController extends AbstractController
$disable_invitation = true; $disable_invitation = true;
if (!$lang || !in_array($lang, $locales_list)) { if (!$lang || !in_array($lang, $locales_list)) {
$lang = in_array(CURRENT_LOCALE, $locales_list) ? CURRENT_LOCALE : $locales_list[0]; $lang = in_array(get_current_locale(), $locales_list)
? get_current_locale()
: $locales_list[0];
} }
$message = getlocal('Click to chat'); $message = getlocal('Click to chat');

View File

@ -60,7 +60,7 @@ class ButtonController extends AbstractController
$lang = ''; $lang = '';
} }
if (!$lang || !locale_exists($lang)) { if (!$lang || !locale_exists($lang)) {
$lang = CURRENT_LOCALE; $lang = get_current_locale();
} }
$group_id = $request->query->get('group', ''); $group_id = $request->query->get('group', '');

View File

@ -55,8 +55,8 @@ class CannedMessageController extends AbstractController
// Get selected locale, if any. // Get selected locale, if any.
$lang = $this->extractLocale($request); $lang = $this->extractLocale($request);
if (!$lang) { if (!$lang) {
$lang = in_array(CURRENT_LOCALE, $all_locales) $lang = in_array(get_current_locale(), $all_locales)
? CURRENT_LOCALE ? get_current_locale()
: $all_locales[0]; : $all_locales[0];
} }

View File

@ -111,7 +111,7 @@ class MailController extends AbstractController
} }
// Load mail templates and substitute placeholders there. // Load mail templates and substitute placeholders there.
$mail_template = mail_template_load('user_history', CURRENT_LOCALE); $mail_template = mail_template_load('user_history', get_current_locale());
if (!$mail_template) { if (!$mail_template) {
throw new \RuntimeException('Cannot load "user_history" mail template'); throw new \RuntimeException('Cannot load "user_history" mail template');
} }

View File

@ -38,7 +38,7 @@ class TranslationController extends AbstractController
$target = $request->query->get('target'); $target = $request->query->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) { if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE; $target = get_current_locale();
} }
$page = array( $page = array(

View File

@ -38,7 +38,7 @@ class TranslationImportController extends AbstractController
$target = $request->request->get('target'); $target = $request->request->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) { if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE; $target = get_current_locale();
} }
$page = array( $page = array(
@ -84,7 +84,7 @@ class TranslationImportController extends AbstractController
$target = $request->request->get('target'); $target = $request->request->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) { if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE; $target = get_current_locale();
} }
// Validate uploaded file // Validate uploaded file

View File

@ -208,8 +208,8 @@ class MailTemplateController extends AbstractController
&& preg_match("/^[\w-]{2,5}$/", $lang) && preg_match("/^[\w-]{2,5}$/", $lang)
&& in_array($lang, $all_locales); && in_array($lang, $all_locales);
if (!$correct_locale) { if (!$correct_locale) {
$lang = in_array(CURRENT_LOCALE, $all_locales) $lang = in_array(get_current_locale(), $all_locales)
? CURRENT_LOCALE ? get_current_locale()
: $all_locales[0]; : $all_locales[0];
} }

View File

@ -92,7 +92,7 @@ class PasswordRecoveryController extends AbstractController
); );
// Load mail templates and substitute placeholders there. // Load mail templates and substitute placeholders there.
$mail_template = mail_template_load('password_recovery', CURRENT_LOCALE); $mail_template = mail_template_load('password_recovery', get_current_locale());
if (!$mail_template) { if (!$mail_template) {
throw new \RuntimeException('Cannot load "password_recovery" mail template'); throw new \RuntimeException('Cannot load "password_recovery" mail template');
} }

View File

@ -503,7 +503,7 @@ class ThreadProcessor extends ClientSideProcessor
if ($email) { if ($email) {
$thread->postMessage( $thread->postMessage(
Thread::KIND_FOR_AGENT, Thread::KIND_FOR_AGENT,
getlocal('E-Mail: {0}', array($email), CURRENT_LOCALE, true) getlocal('E-Mail: {0}', array($email), get_current_locale(), true)
); );
} }
@ -606,7 +606,7 @@ class ThreadProcessor extends ClientSideProcessor
$thread->userName = $name; $thread->userName = $name;
$thread->remote = $remote_host; $thread->remote = $remote_host;
$thread->referer = $referrer; $thread->referer = $referrer;
$thread->locale = CURRENT_LOCALE; $thread->locale = get_current_locale();
$thread->userId = $visitor['id']; $thread->userId = $visitor['id'];
$thread->userAgent = $user_browser; $thread->userAgent = $user_browser;
$thread->state = Thread::STATE_LEFT; $thread->state = Thread::STATE_LEFT;
@ -617,19 +617,19 @@ class ThreadProcessor extends ClientSideProcessor
if ($referrer) { if ($referrer) {
$thread->postMessage( $thread->postMessage(
Thread::KIND_FOR_AGENT, Thread::KIND_FOR_AGENT,
getlocal('Vistor came from page {0}', array($referrer), CURRENT_LOCALE, true) getlocal('Vistor came from page {0}', array($referrer), get_current_locale(), true)
); );
} }
if ($email) { if ($email) {
$thread->postMessage( $thread->postMessage(
Thread::KIND_FOR_AGENT, Thread::KIND_FOR_AGENT,
getlocal('E-Mail: {0}', array($email), CURRENT_LOCALE, true) getlocal('E-Mail: {0}', array($email), get_current_locale(), true)
); );
} }
if ($info) { if ($info) {
$thread->postMessage( $thread->postMessage(
Thread::KIND_FOR_AGENT, Thread::KIND_FOR_AGENT,
getlocal('Info: {0}', array($info), CURRENT_LOCALE, true) getlocal('Info: {0}', array($info), get_current_locale(), true)
); );
} }
$thread->postMessage(Thread::KIND_USER, $message, array('name' => $name)); $thread->postMessage(Thread::KIND_USER, $message, array('name' => $name));

View File

@ -82,9 +82,9 @@ class ChatStyle extends AbstractStyle implements StyleInterface
// Pass additional variables to template // Pass additional variables to template
$data['mibewRoot'] = MIBEW_WEB_ROOT; $data['mibewRoot'] = MIBEW_WEB_ROOT;
$data['mibewVersion'] = MIBEW_VERSION; $data['mibewVersion'] = MIBEW_VERSION;
$data['currentLocale'] = CURRENT_LOCALE; $data['currentLocale'] = get_current_locale();
$locale_info = get_locale_info(CURRENT_LOCALE); $locale_info = get_locale_info(get_current_locale());
$data['rtl'] = $locale_info && $locale_info['rtl']; $data['rtl'] = $locale_info && $locale_info['rtl'];
$data['stylePath'] = MIBEW_WEB_ROOT . '/' . $this->getFilesPath(); $data['stylePath'] = MIBEW_WEB_ROOT . '/' . $this->getFilesPath();

View File

@ -82,9 +82,9 @@ class PageStyle extends AbstractStyle implements StyleInterface
// Pass additional variables to template // Pass additional variables to template
$data['mibewRoot'] = MIBEW_WEB_ROOT; $data['mibewRoot'] = MIBEW_WEB_ROOT;
$data['mibewVersion'] = MIBEW_VERSION; $data['mibewVersion'] = MIBEW_VERSION;
$data['currentLocale'] = CURRENT_LOCALE; $data['currentLocale'] = get_current_locale();
$locale_info = get_locale_info(CURRENT_LOCALE); $locale_info = get_locale_info(get_current_locale());
$data['rtl'] = $locale_info && $locale_info['rtl']; $data['rtl'] = $locale_info && $locale_info['rtl'];
$data['stylePath'] = MIBEW_WEB_ROOT . '/' . $this->getFilesPath(); $data['stylePath'] = MIBEW_WEB_ROOT . '/' . $this->getFilesPath();

View File

@ -32,7 +32,7 @@ function date_diff_to_text($seconds)
function get_month_selection($from_time, $to_time) function get_month_selection($from_time, $to_time)
{ {
// Use correct months names and over translatable date/time strings. // Use correct months names and over translatable date/time strings.
$locale_info = get_locale_info(CURRENT_LOCALE); $locale_info = get_locale_info(get_current_locale());
setlocale(LC_TIME, $locale_info['time_locale']); setlocale(LC_TIME, $locale_info['time_locale']);
$start = getdate($from_time); $start = getdate($from_time);
@ -92,12 +92,17 @@ function date_to_text($unixtime)
* *
* @param int $timestamp Unix timestamp * @param int $timestamp Unix timestamp
* @param string $format Format name. Can be one of "full", "date", "time". * @param string $format Format name. Can be one of "full", "date", "time".
* @param string $locale Locale code. * @param string|null $locale Locale code. If null is passed in the current
* locale will be used.
* @return string Formatted date. * @return string Formatted date.
* @throws \InvalidArgumentException If $type argument has wrong value. * @throws \InvalidArgumentException If $type argument has wrong value.
*/ */
function format_date($timestamp, $format, $locale = CURRENT_LOCALE) function format_date($timestamp, $format, $locale = null)
{ {
if (is_null($locale)) {
$locale = get_current_locale();
}
// Get locale info // Get locale info
$locale_info = get_locale_info($locale); $locale_info = get_locale_info($locale);
$date_format = $locale_info['date_format']; $date_format = $locale_info['date_format'];

View File

@ -47,11 +47,6 @@ define(
: 'en') : 'en')
); );
/**
* Code of the current system locale
*/
define('CURRENT_LOCALE', get_locale());
function locale_exists($locale) function locale_exists($locale)
{ {
return file_exists(MIBEW_FS_ROOT . "/locales/$locale/translation.po"); return file_exists(MIBEW_FS_ROOT . "/locales/$locale/translation.po");
@ -153,31 +148,42 @@ function get_user_locale()
return 'en'; return 'en';
} }
function get_locale() /**
* Retrieves locale for the current request.
*
* @return string Locale code
*/
function get_current_locale()
{ {
$locale = verify_param("locale", "/./", ""); static $current_locale = null;
// Check if locale code passed in as a param is valid if (is_null($current_locale)) {
$locale_param_valid = $locale $locale = verify_param("locale", "/./", "");
&& locale_pattern_check($locale)
&& locale_exists($locale);
// Check if locale code stored in session data is valid // Check if locale code passed in as a param is valid
$session_locale_valid = isset($_SESSION['locale']) $locale_param_valid = $locale
&& locale_pattern_check($_SESSION['locale']) && locale_pattern_check($locale)
&& locale_exists($_SESSION['locale']); && locale_exists($locale);
if ($locale_param_valid) { // Check if locale code stored in session data is valid
$_SESSION['locale'] = $locale; $session_locale_valid = isset($_SESSION['locale'])
} elseif ($session_locale_valid) { && locale_pattern_check($_SESSION['locale'])
$locale = $_SESSION['locale']; && locale_exists($_SESSION['locale']);
} else {
$locale = get_user_locale(); if ($locale_param_valid) {
$_SESSION['locale'] = $locale;
} elseif ($session_locale_valid) {
$locale = $_SESSION['locale'];
} else {
$locale = get_user_locale();
}
setcookie(LOCALE_COOKIE_NAME, $locale, time() + 60 * 60 * 24 * 1000, MIBEW_WEB_ROOT . "/");
$current_locale = $locale;
} }
setcookie(LOCALE_COOKIE_NAME, $locale, time() + 60 * 60 * 24 * 1000, MIBEW_WEB_ROOT . "/"); return $current_locale;
return $locale;
} }
function get_locale_links() function get_locale_links()
@ -756,12 +762,17 @@ function read_locale_file($path)
* *
* @param string $text A text which should be localized * @param string $text A text which should be localized
* @param array $params Indexed array with placeholders. * @param array $params Indexed array with placeholders.
* @param string $locale Target locale code. * @param string|null $locale Target locale code. If null is passed in the
* current locale will be used.
* @param boolean $raw Indicates if the result should be sanitized or not. * @param boolean $raw Indicates if the result should be sanitized or not.
* @return string Localized text. * @return string Localized text.
*/ */
function getlocal($text, $params = null, $locale = CURRENT_LOCALE, $raw = false) function getlocal($text, $params = null, $locale = null, $raw = false)
{ {
if (is_null($locale)) {
$locale = get_current_locale();
}
$string = get_localized_string($text, $locale); $string = get_localized_string($text, $locale);
if ($params) { if ($params) {

View File

@ -72,7 +72,7 @@ function group_by_name($name)
*/ */
function get_group_name($group) function get_group_name($group)
{ {
if (HOME_LOCALE == CURRENT_LOCALE || !isset($group['vccommonname']) || !$group['vccommonname']) { if (HOME_LOCALE == get_current_locale() || !isset($group['vccommonname']) || !$group['vccommonname']) {
return $group['vclocalname']; return $group['vclocalname'];
} else { } else {
return $group['vccommonname']; return $group['vccommonname'];
@ -242,7 +242,7 @@ function group_is_away($group)
*/ */
function get_group_description($group) function get_group_description($group)
{ {
$use_local_description = HOME_LOCALE == CURRENT_LOCALE $use_local_description = HOME_LOCALE == get_current_locale()
|| !isset($group['vccommondescription']) || !isset($group['vccommondescription'])
|| !$group['vccommondescription']; || !$group['vccommondescription'];

View File

@ -97,7 +97,7 @@ function invitation_invite($visitor_id, $operator)
$thread->remote = $visitor_details['remote_host']; $thread->remote = $visitor_details['remote_host'];
$thread->referer = $last_visited_page; $thread->referer = $last_visited_page;
// User's locale is unknown, set operator locale to the thread // User's locale is unknown, set operator locale to the thread
$thread->locale = CURRENT_LOCALE; $thread->locale = get_current_locale();
$thread->userId = $visitor['userid']; $thread->userId = $visitor['userid'];
$thread->userAgent = $visitor_details['user_agent']; $thread->userAgent = $visitor_details['user_agent'];
$thread->state = Thread::STATE_INVITED; $thread->state = Thread::STATE_INVITED;
@ -122,13 +122,13 @@ function invitation_invite($visitor_id, $operator)
getlocal( getlocal(
'Operator {0} invites visitor at {1} page', 'Operator {0} invites visitor at {1} page',
array($operator_name, $last_visited_page), array($operator_name, $last_visited_page),
CURRENT_LOCALE, get_current_locale(),
true true
) )
); );
$thread->postMessage( $thread->postMessage(
Thread::KIND_AGENT, Thread::KIND_AGENT,
getlocal('Hello, how can I help you?', null, CURRENT_LOCALE, true), getlocal('Hello, how can I help you?', null, get_current_locale(), true),
array( array(
'name' => $operator_name, 'name' => $operator_name,
'operator_id' => $operator['operatorid'], 'operator_id' => $operator['operatorid'],
@ -202,7 +202,7 @@ function invitation_reject($visitor_id)
if ($thread) { if ($thread) {
$thread->postMessage( $thread->postMessage(
Thread::KIND_FOR_AGENT, Thread::KIND_FOR_AGENT,
getlocal('Visitor rejected invitation', null, CURRENT_LOCALE, true) getlocal('Visitor rejected invitation', null, get_current_locale(), true)
); );
} }

View File

@ -509,7 +509,7 @@ function is_operator_online($operator_id)
*/ */
function get_operator_name($operator) function get_operator_name($operator)
{ {
if (HOME_LOCALE == CURRENT_LOCALE) { if (HOME_LOCALE == get_current_locale()) {
return $operator['vclocalename']; return $operator['vclocalename'];
} else { } else {
return $operator['vccommonname']; return $operator['vccommonname'];