Skip to content

[Translation] Added support for JSON format (both loader and dumper). #8534

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

Merged
merged 1 commit into from
Sep 27, 2013
Merged

[Translation] Added support for JSON format (both loader and dumper). #8534

merged 1 commit into from
Sep 27, 2013

Conversation

radekbenkel
Copy link

Based on IniFileLoader\Dumper.

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc this component don't have docs

@radekbenkel
Copy link
Author

Heh, PHP5.3 don't have JSON_PRETTY_PRINT constant. So, in this case is there possibility to use somebody's code (with annotation of course about an author) - like this https://github.com/GerHobbelt/nicejson-php/blob/master/nicejson.php ?

Another solutions is to:

  1. Remove JsonDumper
  2. Throw exception in PHP < 5.4

throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
}

$messages = json_decode(file_get_contents($resource), true);
Copy link
Member

Choose a reason for hiding this comment

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

You should use json_last_error to handle errors.

@GromNaN
Copy link
Member

GromNaN commented Jul 20, 2013

Instead of relying on a generic JSON prettifier, you can generate it line by line:

$lines = array();
foreach ($messages->all($domain) as $key => $value) {
    $lines[] = json_encode($key) . ': ' . json_encode($value)
}
return '{' . PHP_EOL . '    ' . implode(',' . PHP_EOL . '    ', $lines) . PHP_EOL . '}';

$lines[] = json_encode($key) . ': ' . json_encode($value);
}

return '{' . PHP_EOL . ' ' . implode(',' . PHP_EOL . ' ', $lines) . PHP_EOL . '}';
Copy link
Contributor

Choose a reason for hiding this comment

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

Strings should be concatenated without the spaces between dots. For example:

json_encode($key).': '.json_encode($value);

That's how it's done in other parts of code.

@hacfi
Copy link
Contributor

hacfi commented Jul 27, 2013

@Singles Great addition..thanks for the PR!

@radekbenkel
Copy link
Author

Should I squash these commits into one and/or open new PR?

@wouterj
Copy link
Member

wouterj commented Jul 28, 2013

Just squash them

$errorMsg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$errorMsg = 'Unknown error';
Copy link
Contributor

Choose a reason for hiding this comment

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

This should contain the code which you were not able to map, so a user of the api has information to google something without the need to adjust your code to get this information out of it.

Copy link
Author

Choose a reason for hiding this comment

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

This part is taken directly from PHP's manual example (not user comments). What about passing errorCode as exception code?

@stof
Copy link
Member

stof commented Aug 5, 2013

you also need to create the service in FrameworkBundle to register it

* @param array $messages
* @return string Human readable JSON representation
*/
protected function formatPretty(array $messages)
Copy link
Member

Choose a reason for hiding this comment

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

this should be a private method

@fabpot
Copy link
Member

fabpot commented Aug 10, 2013

I don't see the need for a JSON loader/dumper. First, we already have plenty options, then, and AFAIK, there is no standard for translations in a JSON file, and finally, writing JSON is a PITA.

@hacfi
Copy link
Contributor

hacfi commented Aug 11, 2013

I think JSON is useful because web translation services usually support it and it gives you more flexibility for writing a custom tool for editing yourself.

@fabpot
Copy link
Member

fabpot commented Aug 11, 2013

@hacfi Which online tool are you talking about? Any examples?

@radekbenkel
Copy link
Author

@fabpot Crowdin for example http://crowdin.net/page/tour/supported-formats

About this JSON PITA thing - I would say that is a subjective opinion.

@hacfi
Copy link
Contributor

hacfi commented Aug 12, 2013

We're using https://phraseapp.com/ and the provider we used before (can't
remember the name right now) supported it as well.

On Sunday, August 11, 2013, Fabien Potencier wrote:

@hacfi https://github.com/hacfi Which online tool are you talking
about? Any examples?


Reply to this email directly or view it on GitHubhttps://github.com//pull/8534#issuecomment-22453447
.

public function format(MessageCatalogue $messages, $domain = 'messages')
{
$data = $messages->all($domain);
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
Copy link
Member

Choose a reason for hiding this comment

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

I would remove this special case and just don't pretty format for PHP < 5.4 (because 5.3 is not supported anymore anyway).

Copy link
Author

Choose a reason for hiding this comment

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

@fabpot It is not? Didn't know that :)

I added this after first release, when Travis built (5.3 and 5.3.3) failed.

Copy link
Author

Choose a reason for hiding this comment

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

So, should I remove this line and make PHP 5.4 only version? Didn't find any information about Symfony 2.4 will require PHP 5.4.

Copy link
Member

Choose a reason for hiding this comment

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

At the top of the file, if the JSON_PRETTY_PRINT constant is not defined, define it, and remove everything else. That way, on 5.3, it won't be pretty formatted, but as of 5.4, it will.

Copy link
Member

Choose a reason for hiding this comment

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

@Singles Symfony does not require 5.4+. But as 5.3 reached its end of maintenance, it is OK to have a non-pretty file for users still on it.

Copy link
Author

Choose a reason for hiding this comment

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

@stof @fabpot So how you want tests for dumper to be handled? Currently it checks format of file, so it take things like indentation into account - which in my opinion is good, because we test dumper, and it should be human readable.

Proposed solutions from my side:

  1. If php < 5.4 then mark test as skipped
  2. Make two tests - for <= 5.3 and second for newer versions, and mark them skipped depending on PHP version. Then when running on 5.4 , test for 5.3 (which compares non human readable format) will be skipped, and for running on 5.3 test for 5.4 will be skipped.
  3. Instead of checking file format, parse file and check parsed data structure, but for me it not good test case for dumper (look above)
  4. ?

Copy link
Member

Choose a reason for hiding this comment

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

Option 1 seems the best.

Copy link
Author

Choose a reason for hiding this comment

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

@fabpot Updated PR.

@fabpot fabpot merged commit fcef021 into symfony:master Sep 27, 2013
@radekbenkel radekbenkel deleted the translation-json branch September 27, 2013 13:33
@hacfi
Copy link
Contributor

hacfi commented Sep 28, 2013

Thanks @fabpot !

weaverryan added a commit to symfony/symfony-docs that referenced this pull request Jan 9, 2014
…2.4 (singles)

This PR was merged into the 2.4 branch.

Discussion
----------

Translation - Added info about JsonFileLoader added in 2.4

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes (symfony/symfony#8534)
| Applies to    | 2.4
| Fixed tickets | n/a

Commits
-------

adf678b Added info about JsonFileLoader added in 2.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants