-
Notifications
You must be signed in to change notification settings - Fork 171
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
added support for packages loadTranslationsFrom method #149
Open
MeRoBo
wants to merge
5
commits into
rmariuzzo:master
Choose a base branch
from
MeRoBo:packages-loadTranslationsFrom-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b56f0e7
added support for packages which register language files via loadTran…
68bc564
fix the reindexing of arrays with array_merge with array_replace
19db227
updated unit test to support backward compat.
9f056a4
Merge branch 'master' of https://github.com/rmariuzzo/Laravel-JS-Loca…
fbe2923
Merge remote-tracking branch 'origin/master' into packages-loadTransl…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
use Illuminate\Filesystem\Filesystem as File; | ||
use Illuminate\Support\Str; | ||
use JShrink\Minifier; | ||
use Illuminate\Support\Arr; | ||
|
||
/** | ||
* The LangJsGenerator class. | ||
|
@@ -35,6 +36,12 @@ class LangJsGenerator | |
*/ | ||
protected $messagesIncluded = []; | ||
|
||
/** | ||
* List of namespace index of packages language folder | ||
* @var array | ||
*/ | ||
protected $packagesMessages = []; | ||
|
||
/** | ||
* Name of the domain in which all string-translation should be stored under. | ||
* More about string-translation: https://laravel.com/docs/master/localization#retrieving-translation-strings | ||
|
@@ -46,14 +53,17 @@ class LangJsGenerator | |
/** | ||
* Construct a new LangJsGenerator instance. | ||
* | ||
* @param File $file The file service instance. | ||
* @param File $file The file service instance. | ||
* @param string $sourcePath The source path of the language files. | ||
* @param array $messagesIncluded | ||
* @param array $packagesMessages | ||
*/ | ||
public function __construct(File $file, $sourcePath, $messagesIncluded = []) | ||
public function __construct(File $file, $sourcePath, $messagesIncluded = [], $packagesMessages = []) | ||
{ | ||
$this->file = $file; | ||
$this->sourcePath = $sourcePath; | ||
$this->messagesIncluded = $messagesIncluded; | ||
$this->packagesMessages = $packagesMessages; | ||
} | ||
|
||
/** | ||
|
@@ -125,37 +135,17 @@ protected function getMessages($noSort) | |
throw new \Exception("${path} doesn't exists!"); | ||
} | ||
|
||
foreach ($this->file->allFiles($path) as $file) { | ||
$pathName = $file->getRelativePathName(); | ||
$extension = $this->file->extension($pathName); | ||
if ($extension != 'php' && $extension != 'json') { | ||
continue; | ||
} | ||
|
||
if ($this->isMessagesExcluded($pathName)) { | ||
continue; | ||
} | ||
|
||
$key = substr($pathName, 0, -4); | ||
$key = str_replace('\\', '.', $key); | ||
$key = str_replace('/', '.', $key); | ||
|
||
if (Str::startsWith($key, 'vendor')) { | ||
$key = $this->getVendorKey($key); | ||
foreach($this->packagesMessages as $namespace => $langPath) | ||
{ | ||
foreach ($this->file->allFiles($langPath) as $file) | ||
{ | ||
$this->processFile($messages, $file, $namespace); | ||
} | ||
} | ||
|
||
$fullPath = $path.DIRECTORY_SEPARATOR.$pathName; | ||
if ($extension == 'php') { | ||
$messages[$key] = include $fullPath; | ||
} else { | ||
$key = $key.$this->stringsDomain; | ||
$fileContent = file_get_contents($fullPath); | ||
$messages[$key] = json_decode($fileContent, true); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new InvalidArgumentException('Error while decode ' . basename($fullPath) . ': ' . json_last_error_msg()); | ||
} | ||
} | ||
foreach ($this->file->allFiles($path) as $file) | ||
{ | ||
$this->processFile($messages, $file); | ||
} | ||
|
||
if (!$noSort) | ||
|
@@ -214,4 +204,43 @@ private function getVendorKey($key) | |
|
||
return $keyParts[2] .'.'. $keyParts[1] . '::' . $keyParts[3]; | ||
} | ||
|
||
private function processFile(&$messages, $file, $namespace = null) | ||
{ | ||
$pathName = $file->getRelativePathName(); | ||
$extension = $file->getExtension(); | ||
if ($extension != 'php' && $extension != 'json') { | ||
return; | ||
} | ||
|
||
if ($this->isMessagesExcluded($pathName)) { | ||
return; | ||
} | ||
|
||
$key = substr($pathName, 0, -4); | ||
$key = str_replace('\\', '.', $key); | ||
$key = str_replace('/', '.', $key); | ||
|
||
if ($namespace !== null) | ||
{ | ||
$key = 'vendor.' . $namespace . '.'. $key; | ||
} | ||
|
||
if (Str::startsWith($key, 'vendor')) { | ||
$key = $this->getVendorKey($key); | ||
} | ||
|
||
$fullPath = $file->getRealPath(); | ||
if ($extension == 'php') { | ||
$messages[$key] = array_replace(Arr::get($messages,$key,[]), include $fullPath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to check the You can add a test for it for trying to include a translation file, that isn't a valid translation file. |
||
} else { | ||
$key = $key.$this->stringsDomain; | ||
$fileContent = file_get_contents($fullPath); | ||
$messages[$key] = json_decode($fileContent, true); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new InvalidArgumentException('Error while decode ' . basename($fullPath) . ': ' . json_last_error_msg()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
return array( | ||
|
||
'important_data' => 'package important data should be overridden with lang/vendor/nonameinc/en/ declarations', | ||
'another_important_data' => 'this is from the package lang', | ||
|
||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
return array( | ||
|
||
'home' => 'Home', | ||
'family' => array( | ||
'father' => 'Dad', | ||
'mother' => 'Mom', | ||
'children' => array( | ||
'son' => 'Son', | ||
), | ||
), | ||
'plural' => 'one apple|a million apples', | ||
'random' => 'gm8ft2hrrlq1u6m54we9udi', | ||
0x045F => 'should still be error code 1119' | ||
); |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/resources/lang/vendor/nonameinc/en/messages.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
return array( | ||
'important_data' => 'should have replaced packages value for this key', | ||
'new_key' => 'this is a new key added ontop of the packages', | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
namespace Mariuzzo\LaravelJsLocalization; | ||
|
||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class NonameIncProvider extends ServiceProvider | ||
{ | ||
public function boot() | ||
{ | ||
$this->loadTranslationsFrom(__DIR__ . '/../fixtures/packages/nonameinc/lang', 'nonameinc'); | ||
} | ||
|
||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd change it to for readability
$supportedExtensions = ['json', 'php'];
!in_array($extension, $supportedExtensions)
I'm thinking laravel actually does this already somehow, maybe we should look into
The
Illuminate\Translation\Translator
usesIlluminate\Translation\FileLoader
to load/process files. Maybe we can use some of that logic