Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show translations in profile languages #2791

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ $sentenceUrl = $this->Url->build([
{{vm.sentence.expandLabel}}
</span>
<span ng-if="vm.isExpanded">
<?php echo __('Fewer translations') ?>
<?php echo __('Show fewer translations') ?>
</span>
</md-button>
</div>
Expand Down
45 changes: 26 additions & 19 deletions src/View/Helper/LanguagesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function preferredLanguageFilter() {
}
}

private function separatePreferredLanguages($languages)
private function prioritizePreferredLanguages($languages, $oldDesign)
{
$filter = $this->preferredLanguageFilter();
if (!$filter) {
Expand All @@ -85,18 +85,28 @@ private function separatePreferredLanguages($languages)
}
$this->localizedAsort($preferred);

if (CurrentUser::isMember()) {
$filterName = __('Profile languages');
if ($oldDesign) {
if (CurrentUser::isMember()) {
$filterName = __('Profile languages');
} else {
$filterName = __('Last used languages');
}
return array(
$filterName => $preferred,
__('Other languages') => $languages,
);
} else {
$filterName = __('Last used languages');
$preferred = array_map(
function($langName) {
return ['name' => $langName, 'prio' => true];
},
$preferred
);
return $preferred + $languages;
}
return array(
$filterName => $preferred,
__('Other languages') => $languages,
);
}

public function onlyLanguagesArray($split = true)
public function onlyLanguagesArray($split = true, $oldDesign = false)
{
if (!$this->__languages_alone) {
$this->__languages_alone = array_map(
Expand All @@ -106,11 +116,8 @@ public function onlyLanguagesArray($split = true)
$this->localizedAsort($this->__languages_alone);
}

$languages = $this->__languages_alone;
if ($split) {
$languages = $this->separatePreferredLanguages($languages);
}
return $languages;
return $split ? $this->prioritizePreferredLanguages($this->__languages_alone, $oldDesign)
: $this->__languages_alone;
}

/**
Expand Down Expand Up @@ -150,7 +157,7 @@ public function profileLanguagesArray($withAutoDetection = false, $with = [])
public function languagesArrayAlone()
{
$languages = $this->onlyLanguagesArray();
$options = ['und' => $this->langAsAlone(__('All languages'))];
$options = ['und' => ['name' => $this->langAsAlone(__('All languages')), 'prio' => true]];

return $options + $languages;
}
Expand Down Expand Up @@ -180,7 +187,7 @@ public function unknownLanguagesArray($split = true)
*/
public function otherLanguagesArray()
{
$languages = $this->onlyLanguagesArray();
$languages = $this->onlyLanguagesArray(true, true);
$options = ['' => __('other language')];

return $options + $languages;
Expand All @@ -198,11 +205,11 @@ public function languagesArrayShowTranslationsIn($withAllLanguages = true)
$languages = $this->onlyLanguagesArray();
$options = [
/* @translators: option used in language selection dropdown for "Show translations in" in advanced search form */
'none' => __('None'),
'none' => ['name' => __('None'), 'prio' => true],
];
if ($withAllLanguages) {
/* @translators: option used in language selection dropdown for "Show translations in" in advanced search form */
$options['und'] = __x('show-translations-in', 'All languages');
$options['und'] = ['name' => __x('show-translations-in', 'All languages'), 'prio' => true];
}

return $options + $languages;
Expand All @@ -219,7 +226,7 @@ public function getSearchableLanguagesArray($anyOption = null)
{
$languages = $this->onlyLanguagesArray();
if (!is_null($anyOption)) {
$languages = array('und' => $anyOption) + $languages;
$languages = ['und' => ['name' => $anyOption, 'prio' => true]] + $languages;
}
return $languages;
}
Expand Down
190 changes: 104 additions & 86 deletions src/View/Helper/SentencesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,97 +148,88 @@ public function displaySentencesGroup(
<?php
}

public function displayTranslations($id, $translations, $withAudio = true, $langFilter = 'und') {
list($translations, $indirectTranslations) = $translations;
public function displayTranslations($id, $allTranslations, $withAudio = true, $langFilter = 'und') {
?>
<div id="_<?php echo $id; ?>_translations" class="translations">

<?php
$totalDirectTranslations = count(array_keys($translations));
$totalIndirectTranslations = count(array_keys($indirectTranslations));

//merge direct and indirect translations into single array
$allTranslations = array_merge($translations, $indirectTranslations);

$totalTranslations = count($allTranslations);
$showButton = true;

//if only 1 hidden sentence then show all
$collapsibleTranslationsEnabled = !CurrentUser::isMember() || CurrentUser::get('settings.collapsible_translations');
if ($totalTranslations <= 6 || !$collapsibleTranslationsEnabled) {
$initiallyDisplayedTranslations = $totalTranslations;
$showButton = false;
} else {
$initiallyDisplayedTranslations = 5;
$displayed = $totalTranslations - $initiallyDisplayedTranslations;
$showButton = false;
$totalTranslations = count($allTranslations[0]) + count($allTranslations[1]);
$userLangs = CurrentUser::getProfileLanguages();
$langBased = is_array($userLangs) && count($userLangs) > 1;
$typeMap = [0 => 'directTranslation', 1 => 'indirectTranslation'];
$hidden = 0;

if (!$langBased) {
if ($totalTranslations <= 6 || !$collapsibleTranslationsEnabled) {
$toGo = $totalTranslations;
} else {
$toGo = 5;
}
}

//Split 'allTranslations' array into two, visible & hidden sets of sentences
$visibleTranslations = array_slice($allTranslations, 0, $initiallyDisplayedTranslations);
$hiddenTranslations = array_slice($allTranslations, $initiallyDisplayedTranslations);

$sentenceCount = 0;

//visible list of translations
foreach ($visibleTranslations as $translation) {

if ($sentenceCount < $totalDirectTranslations)
$type = 'directTranslation';
else
$type = 'indirectTranslation';

$this->displayGenericSentence(
$translation,
$type,
$withAudio,
$id,
false,
$langFilter
);

$sentenceCount++;
foreach($allTranslations as $type => $translations) {
foreach($translations as $translation) {
if ($langBased) {
$initiallyHidden = !in_array($translation->lang, $userLangs);
} else {
$initiallyHidden = !!$toGo;
if ($toGo > 0)
$toGo--;
}

if ($initiallyHidden) {
echo $this->Html->tag('div', null, ['class' => 'more']);
$showButton = true;
$hidden++;
}
$this->displayGenericSentence(
$translation,
$typeMap[$type],
$withAudio,
$id,
false,
$langFilter
);
if ($initiallyHidden) {
echo $this->Html->tag('/div');
}
}
}

if($showButton){
if ($showButton) {
$downArrowIcon = ' ▼ ';
if ($hidden == $totalTranslations) {
$buttonLabel = __n(
'{downArrowIcon} Show all translations (1)',
'{downArrowIcon} Show all translations ({number})',
$hidden
);
} else {
$buttonLabel = __n(
'{downArrowIcon} Show all translations (+1)',
'{downArrowIcon} Show all translations (+{number})',
$hidden
);
}
echo $this->Html->tag('div',
' ▼ ' . format(__n(
'Show 1 more translation',
'Show {number} more translations',
$displayed,
true
), array('number' => $displayed)),
format(
$buttonLabel,
['number' => $hidden, 'downArrowIcon' => $downArrowIcon]
),
array('class' => 'showLink')
);
}

//expanded list of translations
echo $this->Html->tag('div', null, array('class' => 'more'));

foreach ($hiddenTranslations as $translation) {

if ($sentenceCount < $totalDirectTranslations)
$type = 'directTranslation';
else
$type = 'indirectTranslation';

$this->displayGenericSentence(
$translation,
$type,
$withAudio,
$id,
false,
$langFilter
);

$sentenceCount++;
}

echo $this->Html->tag('div',
' ▲ ' . __('Fewer translations'),
$upArrowIcon = ' ▲ ';
echo $this->Html->tag('div',
format(
__('{upArrowIcon} Show fewer translations'),
compact('upArrowIcon')
),
array('class' => 'hideLink')
);
}
?>
</div>
</div>
<?php
}
Expand Down Expand Up @@ -849,23 +840,50 @@ public function translationsForAngular($translations) {

public function getExpandLabel($sentence)
{
$extraTranslationsCount = $this->getNumberOfExtraTranslations($sentence);
$extraTranslationsCount = $this->getNumberOfExtraTranslations($sentence, $allHidden);
if ($extraTranslationsCount > 0) {
return format(__n(
'Show 1 more translation',
'Show {number} more translations',
$extraTranslationsCount
), ['number' => $this->Number->format($extraTranslationsCount)]);
if ($allHidden) {
$buttonLabel = __n(
'Show all translations (1)',
'Show all translations ({number})',
$extraTranslationsCount
);
} else {
$buttonLabel = __n(
'Show all translations (+1)',
'Show all translations (+{number})',
$extraTranslationsCount
);
}

return format(
$buttonLabel,
['number' => $this->Number->format($extraTranslationsCount)]
);
} else {
return null;
}
}

private function getNumberOfExtraTranslations($sentence)
private function getNumberOfExtraTranslations($sentence, &$allHidden)
{
$translations = $sentence->translations;
$total = count($translations[0]) + count($translations[1]);
return $total - SentencesTable::MAX_TRANSLATIONS_DISPLAYED;
$allHidden = false;
$userLangs = CurrentUser::getProfileLanguages();
if (is_array($userLangs) && count($userLangs) > 1) {
$total = $count = 0;
foreach ($sentence->translations as $translations) {
foreach ($translations as $translation) {
$count += !in_array($translation->lang, $userLangs);
$total++;
}
}
$allHidden = $count == $total;
return $count;
} else {
$translations = $sentence->translations;
$total = count($translations[0]) + count($translations[1]);
return $total - SentencesTable::MAX_TRANSLATIONS_DISPLAYED;
}
}
}
?>
30 changes: 11 additions & 19 deletions webroot/js/directives/language-dropdown.dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,17 @@
/////////////////////////////////////////////////////////////////////////

function $onInit() {
var data = $scope.languagesJson;
var isPriority;

Object.keys(data).forEach(function (key1) {
if (typeof data[key1] === 'object'){
var items = data[key1];
isPriority = !isPriority && key1 !== '0';
Object.keys(items).forEach(function (key2) {
languages.push({code: key2, name: items[key2], isPriority: isPriority});
});
vm.hasSuggestions = true;
} else {
var code = key1;
var lang = {code: code, name: data[code]};
if (code == 'und' || code == 'none') {
lang.isPriority = true;
}
languages.push(lang);
}
var langs = $scope.languagesJson;

Object.keys(langs).forEach(function (code) {
var isPriority = langs[code].prio || false;
var name = langs[code].name || langs[code];
languages.push({
code: code,
name: name,
isPriority: isPriority
});
vm.hasSuggestions = vm.hasSuggestions || isPriority;
});

if ($scope.initialSelection) {
Expand Down
20 changes: 14 additions & 6 deletions webroot/js/directives/sentence-and-translations.dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,20 @@
}

function showFewerTranslations() {
vm.directTranslations = allDirectTranslations.filter(function(item, index) {
return index <= MAX_TRANSLATIONS - 1;
});
vm.indirectTranslations = allIndirectTranslations.filter(function(item, index) {
return index + allDirectTranslations.length <= MAX_TRANSLATIONS - 1;
});
if (Object.keys(vm.userLanguages).length > 1) {
function onlyUserLanguages(sentence) {
return sentence.lang in vm.userLanguages;
}
vm.directTranslations = allDirectTranslations.filter(onlyUserLanguages);
vm.indirectTranslations = allIndirectTranslations.filter(onlyUserLanguages);
} else {
vm.directTranslations = allDirectTranslations.filter(function(item, index) {
return index <= MAX_TRANSLATIONS - 1;
});
vm.indirectTranslations = allIndirectTranslations.filter(function(item, index) {
return index + allDirectTranslations.length <= MAX_TRANSLATIONS - 1;
});
}
}

function toggleMenu() {
Expand Down
Loading