Provide an ability to import translations

This commit is contained in:
Dmitriy Simushev 2014-06-30 14:34:14 +00:00
parent 53fbb8e6e7
commit fe40fd57ae
6 changed files with 237 additions and 18 deletions

View File

@ -41,10 +41,31 @@ abstract class AbstractController extends BaseAbstractController
? $this->generateUrl('translations')
: '';
$import = $route == 'translations_import'
|| $route == 'translations_import_process';
$tabs[getlocal('Translations import')] = !$import
? $this->generateUrl('translations_import')
: '';
$tabs[getlocal('Locales')] = ($route != 'locales')
? $this->generateUrl('locales')
: '';
return $tabs;
}
/**
* Builds human readable locale name in "<Native name> (<code>)" format.
*
* @param string $locale Locale code according to RFC 5646.
* @return string Human readable locale name.
*/
protected function getLocaleName($locale)
{
$locale_info = get_locale_info($locale);
return $locale_info
? sprintf('%s (%s)', $locale_info['name'], $locale)
: $locale;
}
}

View File

@ -47,10 +47,9 @@ class LocaleController extends AbstractController
$locales_list = array();
foreach ($fs_locales as $locale) {
$locale_info = get_locale_info($locale);
$locales_list[] = array(
'code' => $locale,
'name' => ($locale_info ? $locale_info['name'] : $locale),
'name' => $this->getLocaleName($locale),
'isDisabled' => !in_array($locale, $available_locales),
);
}

View File

@ -50,7 +50,10 @@ class TranslationController extends AbstractController
$locales_list = array();
$all_locales = get_available_locales();
foreach ($all_locales as $loc) {
$locales_list[] = array('id' => $loc, 'name' => $this->getLocaleName($loc));
$locales_list[] = array(
'id' => $loc,
'name' => $this->getLocaleName($loc)
);
}
// Prepare localization constants to display.
@ -195,21 +198,6 @@ class TranslationController extends AbstractController
return $this->render('translation_edit', $page);
}
/**
* Builds human readable locale name in "<Native name> (<code>)" format.
*
* @param string $locale Locale code according to RFC 5646.
* @return string Human readable locale name.
*/
protected function getLocaleName($locale)
{
$locale_info = get_locale_info($locale);
return $locale_info
? sprintf('%s (%s)', $locale_info['name'], $locale)
: $locale;
}
/**
* Loads all editable translation strings.
*

View File

@ -0,0 +1,133 @@
<?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 Mibew\Settings;
use Symfony\Component\HttpFoundation\Request;
/**
* Contains actions for all translations import functionality.
*/
class TranslationsImportController extends AbstractController
{
/**
* Builds a page with form for upload 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 = CURRENT_LOCALE;
}
$page = array(
// Use errors list stored in the request. We need to do so to have
// an ability to pass the request from other actions.
'errors' => $request->attributes->get('errors', array()),
);
// 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['stored'] = $request->query->has('stored');
$page['localesList'] = $locales_list;
$page['formtarget'] = $target;
$page['title'] = getlocal('Translations import');
$page['menuid'] = 'translation';
$page = array_merge($page, prepare_menu($operator));
$page['tabs'] = $this->buildTabs($request);
return $this->render('translations_import', $page);
}
/**
* Processes submitting of the form which is generated in
* {@link \Mibew\Controller\TranslationsImportController::showFormAction()}
* method.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
*/
public function submitFormAction(Request $request)
{
csrf_check_token($request);
$errors = array();
$target = $request->request->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE;
}
// Validate uploaded file
$file = $request->files->get('translation_file');
if ($file) {
// Process uploaded file.
$orig_filename = $file->getClientOriginalName();
$file_size = $file->getSize();
if ($file_size == 0 || $file_size > Settings::get('max_uploaded_file_size')) {
$errors[] = failed_uploading_file($orig_filename, "Uploaded file size exceeded");
} elseif ($file->getClientOriginalExtension() != 'po') {
$errors[] = failed_uploading_file($orig_filename, "Invalid file type");
}
} else {
$errors[] = "No file selected";
}
// Try to process uploaded file
if (count($errors) == 0) {
try {
// Try to import new messages.
import_messages($target, $file->getRealPath());
// The file is not needed any more. Remove it.
unlink($file->getRealPath());
} catch (\Exception $e) {
$errors[] = $e->getMessage();
}
}
if (count($errors) != 0) {
$request->attributes->set('errors', $errors);
// The form should be rebuild. Invoke appropriate action.
return $this->showFormAction($request);
}
// Redirect the operator to the same page using GET method.
$redirect_to = $this->generateUrl(
'translations_import',
array('stored' => true)
);
return $this->redirect($redirect_to);
}
}

View File

@ -638,6 +638,22 @@ translations:
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
translations_import:
path: /operator/translation/import
defaults:
_controller: Mibew\Controller\Localization\TranslationsImportController::showFormAction
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
methods: [GET]
translations_import_process:
path: /operator/translation/import
defaults:
_controller: Mibew\Controller\Localization\TranslationsImportController::submitFormAction
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
methods: [POST]
## Updates
updates:
path: /operator/updates

View File

@ -0,0 +1,62 @@
{{#extends "_layout"}}
{{#override "menu"}}{{> _menu}}{{/override}}
{{#override "content"}}
{{l10n "On this page you can upload a custom translation file."}}
<br />
<br />
{{> _errors}}
{{#if stored}}
<div id="formmessage">{{l10n "Translation imported"}}</div>
{{/if}}
<form method="post" action="{{mibewRoot}}/operator/translation/import" enctype="multipart/form-data">
{{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="field">
<label for="translation_file" class="flabel">
{{l10n "Upload translation"}}<span class="required">*</span>
</label>
<div class="fvalue">
<input id="translation_file" type="file" name="translation_file" size="40" value="{{formtranslationFile}}" class="formauth"/>
</div>
<label for="translation_file" class="fdescr"> &mdash; {{l10n "Choose the translation file to upload."}}</label>
<br clear="all"/>
</div>
<div class="fbutton">
<input type="submit" name="save" class="submit-bg-button save-button" value="{{l10n "Upload"}}"/>
</div>
</div>
</div>
<div class="formbottom">
<div class="formbottomi"></div>
</div>
</div>
</div>
</form>
{{/override}}
{{/extends}}