Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromradekbenkel:translation-json
Sep 27, 2013
Merged

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

fabpot merged 1 commit intosymfony:masterfromradekbenkel:translation-json
Sep 27, 2013

Conversation

radekbenkel
Copy link

Based onIniFileLoader\Dumper.

QA
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets-
LicenseMIT
Docthis 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 thishttps://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 usejson_last_error to handle errors.

@GromNaN
Copy link
Member

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

@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

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 commentedAug 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

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

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

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

@radekbenkel
Copy link
Author

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

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

@hacfi
Copy link
Contributor

We're usinghttps://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:

@hacfihttps://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.

@fabpotfabpot merged commitfcef021 intosymfony:masterSep 27, 2013
@radekbenkelradekbenkel deleted the translation-json branchSeptember 27, 2013 13:33
@hacfi
Copy link
Contributor

Thanks@fabpot !

weaverryan added a commit to symfony/symfony-docs that referenced this pull requestJan 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/aCommits-------adf678b Added info about JsonFileLoader added in 2.4
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

8 participants
@radekbenkel@GromNaN@hacfi@wouterj@stof@fabpot@staabm@jakzal

[8]ページ先頭

©2009-2025 Movatter.jp