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

Sort output option added to Po generator #227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/Generators/Po.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Gettext\Generators;

use Gettext\Translation;
use Gettext\Translations;

class Po extends Generator implements GeneratorInterface
{
public static $options = [
'noLocation' => false,
'sortOutput' => false,
];

/**
Expand All @@ -27,6 +29,12 @@ public static function toString(Translations $translations, array $options = [])

$lines[] = '';

if ($options['sortOutput'] == true) {
$translations->uasort(function (Translation $translation1, Translation $translation2) {
return strcmp($translation1->getOriginal(), $translation2->getTranslation());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand - sorting by comparing original with the translation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fault. The comparison must be this:

return strcmp($translation1->getOriginal(), $translation2->getOriginal());

Sorry. Must I send a fix or can you change it?

});
}

//Translations
foreach ($translations as $translation) {
if ($translation->hasComments()) {
Expand Down Expand Up @@ -136,6 +144,7 @@ public static function convertString($value)
"\x00" => '',
'\\' => '\\\\',
"\t" => '\t',
"\r" => '\r',
"\n" => '\n',
'"' => '\\"',
]
Expand Down