Add ability for client-side plugins to use own locale strings

This commit is contained in:
Dmitriy Simushev 2013-04-11 10:04:19 +00:00
parent 438824fa36
commit 5798c2399d
4 changed files with 42 additions and 4 deletions

View File

@ -130,6 +130,37 @@ function get_additional_js($page_name) {
return implode("\n", $result); return implode("\n", $result);
} }
/**
* Add additional localized strings for JavaScript application
*
* Triggers 'pageAddLocalizedStrings' and pass listeners associative array with
* following keys:
* - 'page': string, name of page to which localized strings will be added.
* - 'localized_strings': associative array with localized strings.
*
* @param string $page_name Localized strings add to this page
* @return string JSON encoded localized strings
*/
function get_additional_localized_strings($page_name) {
// Prepare event arguments array
$args = array(
'page' => $page_name,
'localized_strings' => array()
);
// Trigger event
$dispatcher = EventDispatcher::getInstance();
$dispatcher->triggerEvent('pageAddLocalizedStrings', $args);
// Build result
$result = array();
if (! empty($args['localized_strings'])
&& is_array($args['localized_strings'])) {
$result = $args['localized_strings'];
}
return json_encode($result);
}
/** /**
* Build Javascript code that contains initializing options for JavaScript * Build Javascript code that contains initializing options for JavaScript
@ -166,6 +197,8 @@ function get_js_plugin_options($page_name) {
* @return array Associative array of plugins data. It contains following keys: * @return array Associative array of plugins data. It contains following keys:
* - 'additional_css': contains results of the 'get_additional_css function * - 'additional_css': contains results of the 'get_additional_css function
* - 'additional_js': contains results of the 'get_additional_js' function * - 'additional_js': contains results of the 'get_additional_js' function
* - 'additional_localized_strings': contains results of the
* 'get_additional_localized_strings' function
* - 'js_plugin_options': contains results of the 'get_js_plugin_options' * - 'js_plugin_options': contains results of the 'get_js_plugin_options'
* function * function
*/ */
@ -173,6 +206,7 @@ function get_plugins_data($page_name) {
return array( return array(
'additional_css' => get_additional_css($page_name), 'additional_css' => get_additional_css($page_name),
'additional_js' => get_additional_js($page_name), 'additional_js' => get_additional_js($page_name),
'additional_localized_strings' => get_additional_localized_strings($page_name),
'js_plugin_options' => get_js_plugin_options($page_name) 'js_plugin_options' => get_js_plugin_options($page_name)
); );
} }

View File

@ -52,10 +52,8 @@ $page['coreStyles.trackedVisitorWindowParams'] = $style_config['tracked']['visit
$page['coreStyles.inviteWindowParams'] = $style_config['invitation']['window_params']; $page['coreStyles.inviteWindowParams'] = $style_config['invitation']['window_params'];
$page['coreStyles.banWindowParams'] = $style_config['ban']['window_params']; $page['coreStyles.banWindowParams'] = $style_config['ban']['window_params'];
// Get additional files // Get additional plugins data
$page['additional_css'] = get_additional_css('users'); $page = array_merge($page, get_plugins_data('users'));
$page['additional_js'] = get_additional_js('users');
$page['js_plugin_options'] = get_js_plugin_options('users');
prepare_menu($operator); prepare_menu($operator);
start_html_output(); start_html_output();

View File

@ -31,6 +31,7 @@
${page:additional_js} ${page:additional_js}
<script type="text/javascript"><!-- <script type="text/javascript"><!--
// Localized strings for the core
Mibew.Localization.set({ Mibew.Localization.set({
'chat.close.confirmation': ${msgjs:chat.close.confirmation}, 'chat.close.confirmation': ${msgjs:chat.close.confirmation},
'typing.remote': ${msgjs:typing.remote}, 'typing.remote': ${msgjs:typing.remote},
@ -70,6 +71,8 @@
'presurvey.title': ${msgjs:presurvey.title}, 'presurvey.title': ${msgjs:presurvey.title},
'presurvey.intro': ${msgjs:presurvey.intro} 'presurvey.intro': ${msgjs:presurvey.intro}
}); });
// Plugins localization
Mibew.Localization.set(${page:additional_localized_strings});
//--></script> //--></script>
<!-- Run application --> <!-- Run application -->

View File

@ -46,6 +46,7 @@ function tpl_header() { global $page, $webimroot;
<!-- Localization constants --> <!-- Localization constants -->
<script type="text/javascript"><!-- <script type="text/javascript"><!--
// Localized strings for the core
Mibew.Localization.set({ Mibew.Localization.set({
'pending.table.speak': "<?php echo getlocal('pending.table.speak') ?>", 'pending.table.speak': "<?php echo getlocal('pending.table.speak') ?>",
'pending.table.view': "<?php echo getlocal('pending.table.view') ?>", 'pending.table.view': "<?php echo getlocal('pending.table.view') ?>",
@ -86,6 +87,8 @@ function tpl_header() { global $page, $webimroot;
'chat.client.spam.prefix': "<?php echo getstring('chat.client.spam.prefix'); ?>", 'chat.client.spam.prefix': "<?php echo getstring('chat.client.spam.prefix'); ?>",
'pending.errors.network': "<?php echo getlocal('pending.errors.network'); ?>" 'pending.errors.network': "<?php echo getlocal('pending.errors.network'); ?>"
}); });
// Plugins localization
Mibew.Localization.set(<?php echo $page['additional_localized_strings']; ?>);
//--></script> //--></script>
<script type="text/javascript"><!-- <script type="text/javascript"><!--