Skip to content

Commit ae718fe

Browse files
author
Oscar Otero
committed
first commit
0 parents  commit ae718fe

File tree

14 files changed

+378
-0
lines changed

14 files changed

+378
-0
lines changed

.gitattributes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/tests/ export-ignore
2+
/phpunit.xml export-ignore
3+
.* export-ignore
4+
phpcs.xml export-ignore
5+
6+
# Set the line ending configuration
7+
* text=lf

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
vendor
2+
composer.lock
3+
*.cache
4+
5+
# Eclipse-specific files
6+
/.settings/
7+
/.buildpath
8+
/.project
9+
10+
# PhpStorm-specific files
11+
/.idea/

.php_cs.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
return My\PhpCsFixerConfig::create()
4+
->setFinder(
5+
PhpCsFixer\Finder::create()
6+
->files()
7+
->name('*.php')
8+
->in(__DIR__.'/src')
9+
->in(__DIR__.'/tests')
10+
->exclude('assets')
11+
);

CONTRIBUTING.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Contributing to Gettext
2+
=======================
3+
4+
Looking to contribute something to this library? Here's how you can help.
5+
6+
## Bugs
7+
8+
A bug is a demonstrable problem that is caused by the code in the repository. Good bug reports are extremely helpful – thank you!
9+
10+
Please try to be as detailed as possible in your report. Include specific information about the environment – version of PHP, version of gettext, etc, and steps required to reproduce the issue.
11+
12+
## Pull Requests
13+
14+
Good pull requests – patches, improvements, new features – are a fantastic help. New extractors or generator are welcome. Before create a pull request, please follow these instructions:
15+
16+
* The code must be PSR-2 compliant
17+
* Write some tests

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 Oscar Otero Marzoa
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Gettext Json format
2+
3+
Created by Oscar Otero <http://oscarotero.com> <oom@oscarotero.com> (MIT License)
4+
5+
Json loader and generator to use with [gettext/gettext](https://github.com/php-gettext/Gettext)
6+
7+
## Installation
8+
9+
```
10+
composer require gettext/json
11+
```
12+
13+
## Usage example
14+
15+
```php
16+
use Gettext\Loader\PoLoader;
17+
use Gettext\Loader\JsonLoader;
18+
use Gettext\Generator\JsonGenerator;
19+
use Gettext\Translations;
20+
21+
//Load a .po file and export to .json
22+
$translations = (new PoLoader())->loadFile('locales/translations.po');
23+
(new JsonGenerator())->generateFile($translations, 'locales/translations.json');
24+
25+
//You can load the json file with JsonLoader
26+
$loadedTranslations = (new JsonLoader())->loadFile('locales/translations.json');
27+
```

composer.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "gettext/json",
3+
"type": "library",
4+
"description": "Json format for gettext",
5+
"keywords": ["json", "gettext", "i18n", "translation", "loader"],
6+
"homepage": "https://github.com/php-gettext/Json",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Oscar Otero",
11+
"email": "oom@oscarotero.com",
12+
"homepage": "http://oscarotero.com",
13+
"role": "Developer"
14+
}
15+
],
16+
"support": {
17+
"email": "oom@oscarotero.com",
18+
"issues": "https://github.com/php-gettext/Json/issues"
19+
},
20+
"require": {
21+
"php": "^7.2",
22+
"gettext/gettext": "dev-v5-dev"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^8.0",
26+
"squizlabs/php_codesniffer": "^3.0",
27+
"oscarotero/php-cs-fixer-config": "^1.0",
28+
"friendsofphp/php-cs-fixer": "^2.15"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"Gettext\\": "src"
33+
}
34+
},
35+
"autoload-dev": {
36+
"psr-4": {
37+
"Gettext\\Tests\\": "tests"
38+
}
39+
},
40+
"scripts": {
41+
"test": [
42+
"phpunit",
43+
"phpcs"
44+
],
45+
"cs-fix": "php-cs-fixer fix"
46+
}
47+
}

phpcs.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Gettext coding standard">
3+
<description>Gettext coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<arg name="colors"/>
8+
9+
<!-- coding standard -->
10+
<rule ref="PSR2"/>
11+
12+
<!-- Paths to check -->
13+
<file>src</file>
14+
</ruleset>

phpunit.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<phpunit bootstrap="./vendor/autoload.php">
2+
<testsuites>
3+
<testsuite name="All tests">
4+
<directory>./tests/</directory>
5+
</testsuite>
6+
</testsuites>
7+
<filter>
8+
<whitelist processUncoveredFilesFromWhitelist="true">
9+
<directory suffix=".php">src</directory>
10+
</whitelist>
11+
</filter>
12+
</phpunit>

src/Generator/JsonGenerator.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Gettext\Generator;
5+
6+
use Gettext\Headers;
7+
use Gettext\Translation;
8+
use Gettext\Translations;
9+
10+
final class JsonGenerator extends Generator
11+
{
12+
private $jsonOptions = 0;
13+
14+
public function jsonOptions(int $jsonOptions): self
15+
{
16+
$this->jsonOptions = $jsonOptions;
17+
18+
return $this;
19+
}
20+
21+
public function generateString(Translations $translations): string
22+
{
23+
$array = $this->generateArray($translations);
24+
25+
return json_encode($array, $this->jsonOptions);
26+
}
27+
28+
public function generateArray(Translations $translations): array
29+
{
30+
$pluralForm = $translations->getHeaders()->getPluralForm();
31+
$pluralSize = is_array($pluralForm) ? ($pluralForm[0] - 1) : null;
32+
$messages = [];
33+
34+
foreach ($translations as $translation) {
35+
if (!$translation->getTranslation() || $translation->isDisabled()) {
36+
continue;
37+
}
38+
39+
$context = $translation->getContext() ?: '';
40+
$original = $translation->getOriginal();
41+
42+
if (!isset($messages[$context])) {
43+
$messages[$context] = [];
44+
}
45+
46+
if (self::hasPluralTranslations($translation)) {
47+
$messages[$context][$original] = $translation->getPluralTranslations($pluralSize);
48+
array_unshift($messages[$context][$original], $translation->getTranslation());
49+
} else {
50+
$messages[$context][$original] = $translation->getTranslation();
51+
}
52+
}
53+
54+
return [
55+
'domain' => $translations->getDomain(),
56+
'plural-forms' => $translations->getHeaders()->get(Headers::HEADER_PLURAL),
57+
'messages' => $messages,
58+
];
59+
}
60+
61+
private static function hasPluralTranslations(Translation $translation): bool
62+
{
63+
return implode('', $translation->getPluralTranslations()) !== '';
64+
}
65+
}

0 commit comments

Comments
 (0)