Skip to content

Commit

Permalink
Upgrade for Laravel 6 compatibility (#14)
Browse files Browse the repository at this point in the history
* Adapt `str_` and `array_` helpers.
* Rename public function `setUp()` to public function `setUp() void`.
* Adapt dependencies to make tests pass from Laravel 5.1 to 6.0.
* Debug app name for Laravel version < 5.3 to make tests pass.
* Improve README.
  • Loading branch information
MichaelHoste authored Sep 16, 2019
1 parent e0a793d commit a9d8a43
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 61 deletions.
13 changes: 4 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3

env:
- COMPOSER_FLAGS="--prefer-lowest"
- COMPOSER_FLAGS=""

matrix:
exclude:
- php: 7.1
env: COMPOSER_FLAGS="--prefer-lowest"
- COMPOSER_FLAGS=--prefer-lowest
- COMPOSER_FLAGS=

sudo: false

Expand All @@ -27,7 +22,7 @@ cache:

before_script:
- travis_retry composer self-update
- travis_wait composer update ${COMPOSER_FLAGS} --no-interaction --prefer-stable
- travis_wait composer update --no-interaction --prefer-stable $COMPOSER_FLAGS

script:
- vendor/bin/phpunit
Expand Down
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [Translation.io](https://translation.io/laravel) client for Laravel 5
# [Translation.io](https://translation.io/laravel) client for Laravel 5/6

[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/translation/laravel/master.svg?style=flat-square)](https://travis-ci.org/translation/laravel)
Expand Down Expand Up @@ -301,10 +301,18 @@ Translation::setLocale('fr');

## Testing

To run the specs:
To run the specs with oldest dependencies:

```bash
$ phpunit
$ composer update --no-interaction --prefer-stable --prefer-lowest
$ ./vendor/bin/phpunit
```

To run the specs with latest dependencies:

```bash
$ composer update --no-interaction --prefer-stable
$ ./vendor/bin/phpunit
```

## Contributing
Expand Down Expand Up @@ -346,9 +354,16 @@ Credits: [@deecewan](https://github.com/deecewan)

### Others

If you want to create a new client for your favorite language or framework, feel
free to reach us on [[email protected]](mailto:[email protected]) and
we'll assist you with the workflow logic and send you API docs.
If you want to create a new client for your favorite language or framework, please read our
[Create a Translation.io Library](https://translation.io/docs/create-library)
guide and use the special
[init](https://translation.io/docs/create-library#initialization) and
[sync](https://translation.io/docs/create-library#synchronization) endpoints.

You can also use the more [traditional API](https://translation.io/docs/api).

Feel free to contact us on [[email protected]](mailto:[email protected]) if
you need some help or if you want to share your library.

## License

Expand Down
16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
"require": {
"php": ">=7.0",
"gettext/gettext": "^4.4.3",
"illuminate/config": "~5.1",
"illuminate/console": "~5.1",
"illuminate/filesystem": "~5.1",
"illuminate/support": "~5.1",
"illuminate/translation": "~5.1",
"guzzlehttp/guzzle": "^6.0"
"illuminate/config": "^5.1 || ^6.0",
"illuminate/console": "^5.1 || ^6.0",
"illuminate/filesystem": "^5.1 || ^6.0",
"illuminate/support": "^5.1 || ^6.0",
"illuminate/translation": "^5.1 || ^6.0",
"guzzlehttp/guzzle": "^6.3"
},
"require-dev": {
"phpunit/phpunit": "~6.0",
"phpunit/phpunit": ">=5.7.27",
"php-vcr/php-vcr": "^1.3",
"league/flysystem-vfs": "^1.0",
"orchestra/testbench": "^3.5",
"orchestra/testbench": "^3.1 || ^4.0",
"squizlabs/php_codesniffer": "~3.0",
"codeclimate/php-test-reporter": "^0.3"
},
Expand Down
56 changes: 33 additions & 23 deletions src/GettextPOGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,36 +160,36 @@ private function path()
}

private function addStringsFromJsonFiles($translations) {
$sourceStrings = [];
$sourceStrings = [];

// Load each JSON file to get source strings
foreach ($this->JsonFiles() as $jsonFile) {
$jsonTranslations = json_decode(file_get_contents($jsonFile), true);
// Load each JSON file to get source strings
foreach ($this->JsonFiles() as $jsonFile) {
$jsonTranslations = json_decode(file_get_contents($jsonFile), true);

foreach ($jsonTranslations as $key => $value) {
$sourceStrings[] = $key;
}
}
foreach ($jsonTranslations as $key => $value) {
$sourceStrings[] = $key;
}
}

// Deduplicate them (in a perfect world, each JSON locale file must have the same sources)
$sourceStrings = array_unique($sourceStrings);
// Deduplicate them (in a perfect world, each JSON locale file must have the same sources)
$sourceStrings = array_unique($sourceStrings);

// Insert them in $translations with a special context
foreach ($sourceStrings as $sourceString) {
$translations->insert($this->jsonStringContext(), $sourceString, '');
}
// Insert them in $translations with a special context
foreach ($sourceStrings as $sourceString) {
$translations->insert($this->jsonStringContext(), $sourceString, '');
}
}

private function setPoHeaders($translations) {
$translations->setHeader('Project-Id-Version', config('app.name'));
$translations->setHeader('Report-Msgid-Bugs-To', '[email protected]');
$translations->setHeader("Plural-Forms", "nplurals=INTEGER; plural=EXPRESSION;");

// Only for testing (for VCR)
if ($this->application->environment('testing')) {
$translations->setHeader('POT-Creation-Date', '2018-01-01T12:00:00+00:00');
$translations->setHeader("PO-Revision-Date", "2018-01-02T12:00:00+00:00");
}
$translations->setHeader('Project-Id-Version', $this->appName());
$translations->setHeader('Report-Msgid-Bugs-To', '[email protected]');
$translations->setHeader("Plural-Forms", "nplurals=INTEGER; plural=EXPRESSION;");

// Only for testing (for VCR)
if ($this->application->environment('testing')) {
$translations->setHeader('POT-Creation-Date', '2018-01-01T12:00:00+00:00');
$translations->setHeader("PO-Revision-Date", "2018-01-02T12:00:00+00:00");
}
}

private function mergeWithExistingTargetPoFile($translations, $target) {
Expand Down Expand Up @@ -237,4 +237,14 @@ private function mergeWithExistingStringsFromTargetJsonFile($translations, $targ
private function jsonStringContext() {
return 'Extracted from JSON file';
}

private function appName() {
$appName = 'Laravel';

if(config('app.name') != '') {
$appName = config('app.name');
}

return $appName;
}
}
3 changes: 2 additions & 1 deletion src/Service/SourceEditSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GuzzleHttp\Client;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;

class SourceEditSyncException extends \Exception {};

Expand Down Expand Up @@ -137,7 +138,7 @@ private function timestampFromMetadataContent($metadataContent)

private function throwErrorIfConflictInMetadata($metadataContent)
{
if (str_contains($metadataContent, ['>>>>', '<<<<'])) {
if (Str::contains($metadataContent, ['>>>>', '<<<<'])) {
$metadataFilePath = $this->metadataFilePath();
throw new SourceEditSyncException($metadataFilePath . " file is corrupted and seems to have unresolved versioning conflicts. Please resolve them and try again.");
}
Expand Down
1 change: 0 additions & 1 deletion src/Service/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Tio\Laravel\SourcePOGenerator;
use Tio\Laravel\TranslationSaver;
use Tio\Laravel\GettextTranslationSaver;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Log;
Expand Down
5 changes: 3 additions & 2 deletions src/SourceSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Tio\Laravel\PrettyVarExport;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;

class SourceSaver
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public function call($sourceEdit, $sourceLocale)

// Adapt $group and $dir if the key contains subfolders:
// https://laravel.io/forum/02-23-2015-localization-load-files-from-subdirectories-at-resourceslanglocale)
if (str_contains($key, '/')) {
if (Str::contains($key, '/')) {
$subFolders = explode('/', $key);
array_pop($subFolders);
$dir = join(DIRECTORY_SEPARATOR, array_merge([$dir], $subFolders));
Expand Down Expand Up @@ -84,7 +85,7 @@ private function group($key)
{
$foldersAndGroup = explode('.', $key)[0];

if (str_contains($foldersAndGroup, '/')) {
if (Str::contains($foldersAndGroup, '/')) {
$parts = explode('/', $foldersAndGroup);
return array_pop($parts);
}
Expand Down
3 changes: 2 additions & 1 deletion src/TranslationExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\Translator;
use Illuminate\Support\Arr;
use SplFileInfo;
use Symfony\Component\Finder\Finder;

Expand Down Expand Up @@ -53,7 +54,7 @@ public function call($locale)
$group = $file->getBasename('.' . $file->getExtension());
$relativePath = $file->getRelativePath();

$data = array_dot([
$data = Arr::dot([
$group => $this->translator->getLoader()->load($locale, $relativePath . DIRECTORY_SEPARATOR . $group)
]);

Expand Down
6 changes: 4 additions & 2 deletions src/TranslationSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Tio\Laravel\PrettyVarExport;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\Translator;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use SplFileInfo;

class TranslationSaver
Expand Down Expand Up @@ -43,7 +45,7 @@ public function call($locale, $translationsDotted)

foreach ($translationsDotted as $key => $value) {
if ($value !== '' && ! is_null($value)) {
array_set($translationsWithGroups, $key, $value);
Arr::set($translationsWithGroups, $key, $value);
}
}

Expand All @@ -59,7 +61,7 @@ private function save($locale, $group, $translations)

// Adapt $group and $dir if key contains subfolders:
// https://laravel.io/forum/02-23-2015-localization-load-files-from-subdirectories-at-resourceslanglocale)
if (str_contains($group, '/')) {
if (Str::contains($group, '/')) {
$subFolders = explode('/', $group);
$group = array_pop($subFolders);
$dir = join(DIRECTORY_SEPARATOR, array_merge([$dir], $subFolders));
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestCase extends OrchestraTestCase
*/
protected $filesystem;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->filesystem = app(Filesystem::class);
Expand Down
Loading

0 comments on commit a9d8a43

Please sign in to comment.