Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Resolved #85
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Feb 21, 2022
2 parents 1328ff6 + 9afd6b3 commit 460cdec
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 14 deletions.
10 changes: 1 addition & 9 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: overtrue
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
custom: # Replace with a single custom sponsorship URL
github: [overtrue]
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2
updates:
- package-ecosystem: composer
directory: "/"
schedule:
interval: daily
time: "21:00"
open-pull-requests-limit: 10
ignore:
- dependency-name: laravel-lang/lang
versions:
- 8.0.0
- 8.1.0
- 9.0.0
- 9.1.0
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ examples:
php artisan lang:publish zh_CN,zh_HK,th,tk
```

## :heart: Sponsor me

[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)

如果你喜欢我的项目并想支持它,[点击这里 :heart:](https://github.com/sponsors/overtrue)

## Project supported by JetBrains

Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.

[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/overtrue)

## PHP 扩展包开发

> 想知道如何从零开始构建 PHP 扩展包?
Expand Down
8 changes: 6 additions & 2 deletions src/Commands/Publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function handle()
$force = $this->option('force') ? 'f' : 'n';

$sourcePath = base_path('vendor/laravel-lang/lang/locales');
$sourceJsonPath = base_path('vendor/laravel-lang/lang/locales');
$targetPath = base_path('resources/lang/');

if (!is_dir($targetPath) && !mkdir($targetPath)) {
Expand All @@ -52,6 +53,7 @@ public function handle()
if ('all' == $locale) {
$files = [
\addslashes($sourcePath).'/*',
\addslashes($sourceJsonPath).'/*/*.json',
];
$message = 'all';
$copyEnFiles = true;
Expand All @@ -62,8 +64,10 @@ public function handle()

continue;
}
$file = $sourcePath.'/'.trim($filename);
$jsonFile = $sourcePath.'/'.trim($filename).'.json';

$trimFilename = trim($filename);
$file = $sourcePath.'/'.$trimFilename;
$jsonFile = $sourceJsonPath."/{$trimFilename}/{$trimFilename}".'.json';

if (!file_exists($file)) {
$this->error("'$filename' not found.");
Expand Down
50 changes: 49 additions & 1 deletion src/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
use Illuminate\Support\Str;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Translation\FileLoader as LaravelTranslationFileLoader;
use RuntimeException;

class FileLoader extends LaravelTranslationFileLoader
{
/**
* @var string
* @var array
*/
protected $paths;

/**
* @var string[]
*/
protected $customJsonPaths = [];

/**
* Create a new file loader instance.
*
Expand Down Expand Up @@ -68,4 +74,46 @@ protected function loadPath($path, $locale, $group)

return $result;
}

/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
$this->customJsonPaths[] = $path;
parent::addJsonPath($path);
}

/**
* Load a locale from the given JSON file path.
*
* @param string $locale
* @return array
*
* @throws RuntimeException
*/
protected function loadJsonPaths($locale)
{
return collect(array_merge($this->jsonPaths, [$this->path]))
->reduce(function ($output, $path) use ($locale) {
if (in_array($path, $this->customJsonPaths)) {
$locale = "{$locale}/{$locale}";
}

if ($this->files->exists($full = "{$path}/{$locale}.json")) {
$decoded = json_decode($this->files->get($full), true);

if (is_null($decoded) || json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException("Translation file [{$full}] contains an invalid JSON structure.");
}

$output = array_merge($output, $decoded);
}

return $output;
}, []);
}
}
4 changes: 2 additions & 2 deletions src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function registerLoader()
{
$this->app->singleton('translation.loader', function ($app) {
$paths = [
base_path('vendor/laravel-lang/lang/src/'),
base_path('vendor/laravel-lang/lang/locales/'),
];

if ($this->inLumen) {
Expand All @@ -57,7 +57,7 @@ protected function registerLoader()
$loader = new FileLoader($app['files'], $app['path.lang'], $paths);

if (\is_callable([$loader, 'addJsonPath'])) {
$loader->addJsonPath(base_path('vendor/laravel-lang/lang/json/'));
$loader->addJsonPath(base_path('vendor/laravel-lang/lang/locales/'));
}

return $loader;
Expand Down

0 comments on commit 460cdec

Please sign in to comment.