Provide an ability to export translations

Fixes #45
This commit is contained in:
Dmitriy Simushev 2014-08-06 08:39:15 +00:00
parent 668e724a01
commit 4594619a4a
4 changed files with 170 additions and 2 deletions

View File

@ -631,6 +631,22 @@ translation_edit_save:
string_id: "\d{1,10}" string_id: "\d{1,10}"
methods: [POST] methods: [POST]
translation_export:
path: /operator/translation/export
defaults:
_controller: Mibew\Controller\Localization\TranslationExportController::showFormAction
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
methods: [GET]
translation_export_process:
path: /operator/translation/export
defaults:
_controller: Mibew\Controller\Localization\TranslationExportController::submitFormAction
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
methods: [POST]
translation_import: translation_import:
path: /operator/translation/import path: /operator/translation/import
defaults: defaults:

View File

@ -41,12 +41,18 @@ abstract class AbstractController extends BaseAbstractController
? $this->generateUrl('translations') ? $this->generateUrl('translations')
: ''; : '';
$import = $route == 'translation_import' $import = ($route == 'translation_import'
|| $route == 'translation_import_process'; || $route == 'translation_import_process');
$tabs[getlocal('Translations import')] = !$import $tabs[getlocal('Translations import')] = !$import
? $this->generateUrl('translation_import') ? $this->generateUrl('translation_import')
: ''; : '';
$export = ($route == 'translation_export'
|| $route == 'translation_export_process');
$tabs[getlocal('Translations export')] = !$export
? $this->generateUrl('translation_export')
: '';
$tabs[getlocal('Locales')] = ($route != 'locales') $tabs[getlocal('Locales')] = ($route != 'locales')
? $this->generateUrl('locales') ? $this->generateUrl('locales')
: ''; : '';

View File

@ -0,0 +1,99 @@
<?php
/*
* 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 Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Dumper\PoFileDumper;
/**
* Contains actions for all translations export functionality.
*/
class TranslationExportController extends AbstractController
{
/**
* Builds a page with form for downloading translation file.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
*/
public function showFormAction(Request $request)
{
set_csrf_token();
$operator = $this->getOperator();
$target = $request->request->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = get_current_locale();
}
// Load list of all available locales.
$locales_list = array();
$all_locales = get_available_locales();
foreach ($all_locales as $loc) {
$locales_list[] = array(
'id' => $loc,
'name' => $this->getLocaleName($loc)
);
}
$page['localesList'] = $locales_list;
$page['formtarget'] = $target;
$page['title'] = getlocal('Translations export');
$page['menuid'] = 'translation';
$page = array_merge($page, prepare_menu($operator));
$page['tabs'] = $this->buildTabs($request);
return $this->render('translation_export', $page);
}
/**
* Processes submitting of the form which is generated in
* {@link \Mibew\Controller\TranslationExportController::showFormAction()}
* method.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
*/
public function submitFormAction(Request $request)
{
csrf_check_token($request);
$target = $request->request->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = get_current_locale();
}
$messages = load_messages($target);
ksort($messages);
$catalogue = new MessageCatalogue($target, array('messages' => $messages));
$dumper = new PoFileDumper();
$output = $dumper->format($catalogue);
$response = new Response();
$response->headers->set('Content-type', 'application/octet-stream');
$response->headers->set('Content-Disposition', sprintf('attachment; filename=translation-%s.po', $target));
$response->headers->set('Content-Length', strlen($output));
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->setContent($output);
return $response;
}
}

View File

@ -0,0 +1,47 @@
{{#extends "_layout"}}
{{#override "menu"}}{{> _menu}}{{/override}}
{{#override "content"}}
{{l10n "On this page you can download translations."}}
<br />
<br />
{{> _errors}}
<form method="post" action="{{mibewRoot}}/operator/translation/export">
{{csrfTokenInput}}
<div>
{{> _tabs}}
<div class="mform">
<div class="formtop">
<div class="formtopi"></div>
</div>
<div class="forminner">
<div class="fieldForm">
{{l10n "For language:"}}<br/>
<select name="target">
{{#each localesList}}
<option value="{{id}}"{{#ifEqual id ../formtarget}} selected="selected"{{/ifEqual}}>{{name}}</option>
{{/each}}
</select>
</div>
<div class="fieldForm">
<div class="fbutton">
<input type="submit" name="save" class="submit-bg-button save-button" value="{{l10n "Download"}}"/>
</div>
</div>
</div>
<div class="formbottom">
<div class="formbottomi"></div>
</div>
</div>
</div>
</form>
{{/override}}
{{/extends}}