Pass all localized string to a client side application

This commit is contained in:
Dmitriy Simushev 2014-09-25 10:30:19 +00:00
parent a2e2f1b194
commit ba103d5636
6 changed files with 83 additions and 73 deletions

View File

@ -90,6 +90,13 @@ cron:
path: /cron
defaults: { _controller: Mibew\Controller\CronController::runAction }
# A set of localization constants for the client side applications is generated
# dynamically
js_translation:
path: /locales/{locale}/translation.js
defaults:
_controller: Mibew\Controller\Localization\JsTranslationController::indexAction
license:
path: /license
defaults: { _controller: Mibew\Controller\LicenseController::indexAction }

View File

@ -0,0 +1,64 @@
<?php
/*
* This file is a part of Mibew Messenger.
*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Controller\Localization;
use Mibew\Controller\AbstractController;
use Stash\Invalidation;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
/**
* Generates translation file for a client side application.
*/
class JsTranslationController extends AbstractController
{
/**
* Generates a JavaScript file with client-side localization constants.
*
* @param Request $request Incoming request.
* @return Response Prepared JavaScript file with client side localization
* constants.
*/
public function indexAction(Request $request)
{
$locale = $request->attributes->get('locale');
$item = $this->getCache()->getItem('translation/js/' . $locale);
$content = $item->get(Invalidation::OLD);
if ($item->isMiss()) {
$item->lock();
$messages = load_messages($locale);
$content = sprintf(
'%s(%s);',
'Mibew.Localization.set',
json_encode($messages)
);
$item->set($content);
}
$response = new Response();
$response->headers->set('Content-Type', 'text/javascript');
$response->setContent($content);
return $response;
}
}

View File

@ -190,6 +190,9 @@ class TranslationController extends AbstractController
save_message($target, $string['source'], $translation);
// Remove cached client side translations.
$this->getCache()->getItem('translation/js/' . $target)->clear();
$page['saved'] = true;
$page['title'] = getlocal("Translations");
$page = array_merge(

View File

@ -116,6 +116,9 @@ class TranslationImportController extends AbstractController
// Try to import new messages.
import_messages($target, $file->getRealPath(), $override);
// Remove cached client side translations.
$this->getCache()->getItem('translation/js/' . $target)->clear();
// The file is not needed any more. Remove it.
unlink($file->getRealPath());
} catch (\Exception $e) {

View File

@ -22,6 +22,9 @@
<script type="text/javascript" src="{{asset "js/compiled/default_app.js"}}"></script>
<script type="text/javascript" src="{{asset "js/compiled/chat_app.js"}}"></script>
<!-- Localized string -->
<script type="text/javascript" src="{{route "js_translation" locale=currentLocale}}"></script>
<!-- Add style scripts -->
<script type="text/javascript" src="{{asset "@CurrentStyle/js/compiled/scripts.js"}}"></script>
@ -30,43 +33,11 @@
<script type="text/javascript"><!--
// Localized strings for the core
Mibew.Localization.set({
'Are you sure want to leave chat?': '{{#jsString}}{{l10n "Are you sure want to leave chat?"}}{{/jsString}}',
'Remote user is typing...': '{{#jsString}}{{l10n "Remote user is typing..."}}{{/jsString}}',
'Select answer...': '{{#jsString}}{{l10n "Select answer..."}}{{/jsString}}',
'Send message': '{{#jsString}}{{l10n "Send message"}}{{/jsString}}',
'chat.window.send_message_short_and_shortcut': '{{#jsString}}{{l10n "Send ({0})" send_shortcut}}{{/jsString}}',
'Close chat': '{{#jsString}}{{l10n "Close chat"}}{{/jsString}}',
'Refresh': '{{#jsString}}{{l10n "Refresh"}}{{/jsString}}',
'Send chat history by e-mail': '{{#jsString}}{{l10n "Send chat history by e-mail"}}{{/jsString}}',
'Redirect visitor to another operator': '{{#jsString}}{{l10n "Redirect visitor to another operator"}}{{/jsString}}',
'Visit history': '{{#jsString}}{{l10n "Visit history"}}{{/jsString}}',
'You are': '{{#jsString}}{{l10n "You are"}}{{/jsString}}',
'Change name': '{{#jsString}}{{l10n "Change name"}}{{/jsString}}',
'Turn off sound': '{{#jsString}}{{l10n "Turn off sound"}}{{/jsString}}',
'Turn on sound': '{{#jsString}}{{l10n "Turn on sound"}}{{/jsString}}',
'Powered by:': '{{#jsString}}{{l10n "Powered by:"}}{{/jsString}}',
'Close...': '{{#jsString}}{{l10n "Close..."}}{{/jsString}}',
'Choose Department:': '{{#jsString}}{{l10n "Choose Department:"}}{{/jsString}}',
'Department description:': '{{#jsString}}{{l10n "Department description:"}}{{/jsString}}',
'Your email': '{{#jsString}}{{l10n "Your email"}}{{/jsString}}',
'Your name': '{{#jsString}}{{l10n "Your name"}}{{/jsString}}',
'Message': '{{#jsString}}{{l10n "Message"}}{{/jsString}}',
'Close': '{{#jsString}}{{l10n "Close"}}{{/jsString}}',
'Sorry. None of the support team is available at the moment. <br/>Please leave a message and someone will get back to you shortly.': '{{#jsString}}{{l10n "Sorry. None of the support team is available at the moment. <br/>Please leave a message and someone will get back to you shortly."}}{{/jsString}}',
'Thank you for your message. We\'ll answer your query by email as soon as possible.': '{{#jsString}}{{l10n "Thank you for your message. We'll answer your query by email as soon as possible."}}{{/jsString}}',
'leavemessage.error.email.required': '{{#jsString}}{{localized.[email.required]}}{{/jsString}}',
'leavemessage.error.name.required': '{{#jsString}}{{localized.[name.required]}}{{/jsString}}',
'leavemessage.error.message.required': '{{#jsString}}{{localized.[message.required]}}{{/jsString}}',
'leavemessage.error.wrong.email': '{{#jsString}}{{localized.[wrong.email]}}{{/jsString}}',
'The letters you typed don\'t match the letters that were shown in the picture.': '{{#jsString}}{{l10n "The letters you typed don't match the letters that were shown in the picture."}}{{/jsString}}',
'Send': '{{#jsString}}{{l10n "Send"}}{{/jsString}}',
'Name:': '{{#jsString}}{{l10n "Name:"}}{{/jsString}}',
'Email:': '{{#jsString}}{{l10n "Email:"}}{{/jsString}}',
'Initial Question:': '{{#jsString}}{{l10n "Initial Question:"}}{{/jsString}}',
'Start Chat': '{{#jsString}}{{l10n "Start Chat"}}{{/jsString}}',
'Wrong email address.': '{{#jsString}}{{l10n "Wrong email address."}}{{/jsString}}',
'Live support': '{{#jsString}}{{l10n "Live support"}}{{/jsString}}',
'Thank you for contacting us. Please fill out the form below and click the Start Chat button.': '{{#jsString}}{{l10n "Thank you for contacting us. Please fill out the form below and click the Start Chat button."}}{{/jsString}}'
});
// Plugins localization
Mibew.Localization.set({{{additional_localized_strings}}});

View File

@ -25,53 +25,15 @@
<script type="text/javascript" src="{{asset "js/compiled/default_app.js"}}"></script>
<script type="text/javascript" src="{{asset "js/compiled/users_app.js"}}"></script>
<!-- Localized string -->
<script type="text/javascript" src="{{route "js_translation" locale=currentLocale}}"></script>
<!-- Plugins JavaScript files -->
{{{additional_js}}}
<!-- Localization constants -->
<script type="text/javascript"><!--
// Localized strings for the core
Mibew.Localization.set({
'Click to chat with the visitor': "{{#jsString}}{{l10n 'Click to chat with the visitor'}}{{/jsString}}",
'Watch the chat': "{{#jsString}}{{l10n 'Watch the chat'}}{{/jsString}}",
'Ban this visitor': "{{#jsString}}{{l10n 'Ban this visitor'}}{{/jsString}}",
'Show menu >>': "{{#jsString}}{{l10n 'Show menu >>'}}{{/jsString}}",
'Hide menu >>': "{{#jsString}}{{l10n 'Hide menu >>'}}{{/jsString}}",
'A new visitor is waiting for an answer.': "{{#jsString}}{{l10n 'A new visitor is waiting for an answer.'}}{{/jsString}}",
'Tracked visitor\'s path': "{{#jsString}}{{l10n 'Tracked visitor\'s path'}}{{/jsString}}",
'Invite to chat': "{{#jsString}}{{l10n 'Invite to chat'}}{{/jsString}}",
'Away': "{{#jsString}}{{l10n 'Away'}}{{/jsString}}",
'Up to date': "{{#jsString}}{{l10n 'Up to date'}}{{/jsString}}",
'Set status as "Available"': "{{#jsString}}{{l10n 'Set status as "Available"'}}{{/jsString}}",
'Set status as "Away"': "{{#jsString}}{{l10n 'Set status as "Away"'}}{{/jsString}}",
'Name': "{{#jsString}}{{l10n 'Name'}}{{/jsString}}",
'Actions': "{{#jsString}}{{l10n 'Actions'}}{{/jsString}}",
'Visitor\'s address': "{{#jsString}}{{l10n 'Visitor\'s address'}}{{/jsString}}",
'State': "{{#jsString}}{{l10n 'State'}}{{/jsString}}",
'Operator': "{{#jsString}}{{l10n 'Operator'}}{{/jsString}}",
'Total time': "{{#jsString}}{{l10n 'Total time'}}{{/jsString}}",
'Waiting time': "{{#jsString}}{{l10n 'Waiting time'}}{{/jsString}}",
'Misc': "{{#jsString}}{{l10n 'Misc'}}{{/jsString}}",
'Actions': "{{#jsString}}{{l10n 'Actions'}}{{/jsString}}",
'Name': "{{#jsString}}{{l10n 'Name'}}{{/jsString}}",
'Visitor\'s address': "{{#jsString}}{{l10n 'Visitor\'s address'}}{{/jsString}}",
'First seen': "{{#jsString}}{{l10n 'First seen'}}{{/jsString}}",
'Last seen': "{{#jsString}}{{l10n 'Last seen'}}{{/jsString}}",
'Invited by': "{{#jsString}}{{l10n 'Invited by'}}{{/jsString}}",
'Invitation time': "{{#jsString}}{{l10n 'Invitation time'}}{{/jsString}}",
'Invitations / Chats': "{{#jsString}}{{l10n 'Invitations / Chats'}}{{/jsString}}",
'Misc': "{{#jsString}}{{l10n 'Misc'}}{{/jsString}}",
'There are no visitors ready to chat on your site at present time': "{{#jsString}}{{l10n 'There are no visitors ready to chat on your site at present time'}}{{/jsString}}",
'The list of visitors waiting is empty': "{{#jsString}}{{l10n 'The list of visitors waiting is empty'}}{{/jsString}}",
'In queue': "{{#jsString}}{{l10n 'In queue'}}{{/jsString}}",
'Waiting for operator': "{{#jsString}}{{l10n 'Waiting for operator'}}{{/jsString}}",
'In chat': "{{#jsString}}{{l10n 'In chat'}}{{/jsString}}",
'Closed': "{{#jsString}}{{l10n 'Closed'}}{{/jsString}}",
'Loading': "{{#jsString}}{{l10n 'Loading'}}{{/jsString}}",
'[spam]': "{{#jsString}}{{l10n '[spam]'}}{{/jsString}}",
'Network problems detected. Please refresh the page.': "{{#jsString}}{{l10n 'Network problems detected. Please refresh the page.'}}{{/jsString}}"
});
// Plugins localization
Mibew.Localization.set({{{additional_localized_strings}}});
//--></script>