Make translation interface play nice with new localization system

This commit is contained in:
Dmitriy Simushev 2014-06-27 13:17:19 +00:00
parent 1f899a3d80
commit 791abc1c2b
4 changed files with 85 additions and 85 deletions

View File

@ -17,6 +17,8 @@
namespace Mibew\Controller\Localization;
use Mibew\Database;
use Mibew\Http\Exception\NotFoundException;
use Symfony\Component\HttpFoundation\Request;
/**
@ -34,24 +36,13 @@ class TranslationController extends AbstractController
{
$operator = $this->getOperator();
$source = $request->query->get('source');
if (!preg_match("/^[\w-]{2,5}$/", $source)) {
$source = DEFAULT_LOCALE;
}
$target = $request->query->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE;
}
$lang1 = load_messages($source);
$lang2 = load_messages($target);
$page = array(
'lang1' => $source,
'lang2' => $target,
'title1' => $this->getLocaleName($source),
'title2' => $this->getLocaleName($target),
'localeName' => $this->getLocaleName($target),
'errors' => array(),
);
@ -63,53 +54,47 @@ class TranslationController extends AbstractController
}
// Prepare localization constants to display.
$result = array();
$all_keys = array_keys($lang1);
foreach ($all_keys as $key) {
$t_source = htmlspecialchars($lang1[$key]);
if (isset($lang2[$key])) {
$value = htmlspecialchars($lang2[$key]);
} else {
$value = "<font color=\"#c13030\"><b>absent</b></font>";
}
$result[] = array(
'id' => $key,
'l1' => $t_source,
'l2' => $value,
$strings = $this->loadStrings($target);
foreach ($strings as $key => $item) {
$strings[$key] = array(
'id' => $item['stringid'],
'source' => htmlentities($item['source']),
'translation' => (empty($item['translation'])
? "<font color=\"#c13030\"><b>absent</b></font>"
: htmlspecialchars($item['translation'])),
);
}
// Sort localization constants in the specified order.
$order = $request->query->get('sort');
if (!in_array($order, array('id', 'l1'))) {
$order = 'id';
if (!in_array($order, array('source', 'translation'))) {
$order = 'source';
}
if ($order == 'id') {
if ($order == 'source') {
usort(
$result,
$strings,
function ($a, $b) {
return strcmp($a['id'], $b['id']);
return strcmp($a['source'], $b['source']);
}
);
} elseif ($order == 'l1') {
} elseif ($order == 'translation') {
usort(
$result,
$strings,
function ($a, $b) {
return strcmp($a['l1'], $b['l1']);
return strcmp($a['translation'], $b['translation']);
}
);
}
$pagination = setup_pagination($result, 100);
$pagination = setup_pagination($strings, 100);
$page['pagination'] = $pagination['info'];
$page['pagination.items'] = $pagination['items'];
$page['formtarget'] = $target;
$page['formsource'] = $source;
$page['availableLocales'] = $locales_list;
$page['availableOrders'] = array(
array('id' => 'id', 'name' => getlocal('Key identifier')),
array('id' => 'l1', 'name' => getlocal('Source language string')),
array('id' => 'source', 'name' => getlocal('Source string')),
array('id' => 'translation', 'name' => getlocal('Translation')),
);
$page['formsort'] = $order;
$page['title'] = getlocal('Translations');
@ -132,41 +117,27 @@ class TranslationController extends AbstractController
$operator = $this->getOperator();
$string_id = $request->attributes->get('string_id');
$source = $request->query->get('source');
if (!preg_match("/^[\w-]{2,5}$/", $source)) {
$source = DEFAULT_LOCALE;
$string = $this->loadString($string_id);
if (!$string) {
throw new NotFoundException('The string is not found.');
}
$target = $request->query->has('target')
? $request->query->get('target')
: $request->request->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE;
}
$lang1 = load_messages($source);
$lang2 = load_messages($target);
$target = $string['locale'];
$page = array(
'title1' => $this->getLocaleName($source),
'title2' => $this->getLocaleName($target),
'localeName' => $this->getLocaleName($target),
// Use errors list stored in the request. We need to do so to have
// an ability to pass the request from the "submitEditForm" action.
'errors' => $request->attributes->get('errors', array()),
);
$translation = isset($lang2[$string_id]) ? $lang2[$string_id] : '';
// Override translation value from the request if needed.
if ($request->request->has('translation')) {
$translation = $request->request->get('translation');
}
$translation = $request->request->get('translation', $string['translation']);
$page['saved'] = false;
$page['key'] = $string_id;
$page['target'] = $target;
$page['formoriginal'] = isset($lang1[$string_id]) ? $lang1[$string_id] : '<b><unknown></b>';
$page['formoriginal'] = $string['source'];
$page['formtranslation'] = $translation;
$page['title'] = getlocal('Translations');
$page = array_merge(
@ -190,14 +161,16 @@ class TranslationController extends AbstractController
csrf_check_token($request);
$operator = $this->getOperator();
$string_id = $request->attributes->get('string_id');
$errors = array();
$target = $request->request->get('target');
if (!preg_match("/^[\w-]{2,5}$/", $target)) {
$target = CURRENT_LOCALE;
$string_id = $request->attributes->get('string_id');
$string = $this->loadString($string_id);
if (!$string) {
throw new NotFoundException('The string is not found.');
}
$target = $string['locale'];
$translation = $request->request->get('translation');
if (!$translation) {
$errors[] = no_field("Translation");
@ -210,7 +183,7 @@ class TranslationController extends AbstractController
return $this->showEditFormAction($request);
}
save_message($target, $string_id, $translation);
save_message($target, $string['source'], $translation);
$page['saved'] = true;
$page['title'] = getlocal("Translations");
@ -238,4 +211,43 @@ class TranslationController extends AbstractController
return $locale;
}
/**
* Loads all editable translation strings.
*
* @param string $locale Locale code.
* @return array List of translated strings. Each element of this array is
* an associative with the following keys:
* - id: int, ID of the translated string in the database.
* - source: string, english string.
* - translation: string, translated string.
*/
protected function loadStrings($locale)
{
$rows = Database::getInstance()->query(
"SELECT * FROM {translation} WHERE locale = :locale",
array(':locale' => $locale),
array('return_rows' => Database::RETURN_ALL_ROWS)
);
return $rows ? $rows : array();
}
/**
* Loads translated string from the database by its ID.
*
* @param int $id ID of the translated string in the database.
* @return array|boolean Associative array with string info or boolean false
* it the string is not found.
*/
protected function loadString($id)
{
$string = Database::getInstance()->query(
"SELECT * FROM {translation} WHERE stringid = :id",
array(':id' => $id),
array('return_rows' => Database::RETURN_ONE_ROW)
);
return $string ? $string : false;
}
}

View File

@ -618,7 +618,7 @@ translation_edit:
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
requirements:
string_id: "[_\.\w]+"
string_id: "\d{1,10}"
methods: [GET]
translation_edit_save:
@ -628,7 +628,7 @@ translation_edit_save:
_access_check: Mibew\AccessControl\Check\PermissionsCheck
_access_permissions: [CAN_ADMINISTRATE]
requirements:
string_id: "[_\.\w]+"
string_id: "\d{1,10}"
methods: [POST]
translations:

View File

@ -19,7 +19,6 @@
<form name="translateForm" method="post" action="{{mibewRoot}}/operator/translation/{{key}}/edit">
{{csrfTokenInput}}
<input type="hidden" name="target" value="{{target}}"/>
<div class="mform">
<div class="formtop">
@ -29,14 +28,14 @@
<div class="forminner">
<div class="fieldForm">
<div class="field">
<label for="original" class="flabel">{{title1}}</label>
<label for="original" class="flabel">{{l10n "Source string"}}</label>
<div class="fvaluenodesc">
<textarea id="original" name="original" disabled="disabled" cols="20" rows="5" class="wide">{{formoriginal}}</textarea>
</div>
</div>
<div class="field">
<label for="translation" class="flabel">{{title2}}</label>
<label for="translation" class="flabel">{{localeName}}</label>
<div class="fvaluenodesc">
<textarea id="translation" name="translation" cols="20" rows="5" class="wide">{{formtranslation}}</textarea>
</div>

View File

@ -17,13 +17,7 @@
<div class="forminner">
<div class="packedFormField">
{{l10n "Direction:"}}<br/>
<select name="source" onchange="this.form.submit();">
{{#each availableLocales}}
<option value="{{id}}" {{#ifEqual id ../formsource}} selected="selected"{{/ifEqual}}>{{name}}</option>
{{/each}}
</select>
=&gt;
{{l10n "For language:"}}<br/>
<select name="target" onchange="this.form.submit();">
{{#each availableLocales}}
<option value="{{id}}"{{#ifEqual id ../formtarget}} selected="selected"{{/ifEqual}}>{{name}}</option>
@ -59,11 +53,9 @@
<table class="translate">
<thead>
<tr class="header"><th>
Key
{{l10n "Source string"}}
</th><th>
{{title1}}
</th><th>
{{title2}}
{{localeName}}
</th></tr>
</thead>
<tbody>
@ -71,18 +63,15 @@
{{#each [pagination.items]}}
<tr>
<td>
<a href="{{../mibewRoot}}/operator/translation/{{id}}/edit?source={{../lang1}}&amp;target={{../lang2}}" target="_blank" onclick="this.newWindow = window.open('{{../mibewRoot}}/operator/translation/{{id}}/edit?source={{../lang1}}&amp;target={{../lang2}}', '', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,width=640,height=480,resizable=1');this.newWindow.focus();this.newWindow.opener=window;return false;">{{id}}</a>
{{{source}}}
</td>
<td>
{{{l1}}}
</td>
<td>
{{{l2}}}
<a href="{{../mibewRoot}}/operator/translation/{{id}}/edit" target="_blank" onclick="this.newWindow = window.open('{{../mibewRoot}}/operator/translation/{{id}}/edit', '', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,width=640,height=480,resizable=1');this.newWindow.focus();this.newWindow.opener=window;return false;">{{{translation}}}</a>
</td>
</tr>
{{else}}
<tr>
<td colspan="3">{{l10n "Found 0 elements"}}</td>
<td colspan="2">{{l10n "Found 0 elements"}}</td>
</tr>
{{/each}}