Skip to content

Commit

Permalink
support package translations
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Apr 2, 2019
1 parent 5ad4fb2 commit ce4ceae
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.5.0 (2018-04-02)

### Added
- support package translations, thanks for the idea to [https://github.com/sardoj](Jonathan)

## v0.4.0 (2018-03-24)

### Added
Expand Down
11 changes: 9 additions & 2 deletions resources/assets/js/I18n.js → resources/js/I18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export default class I18n
);
}

return translation.trim();
return translation;
}

/**
Expand All @@ -101,6 +101,13 @@ export default class I18n
*/
_extract(key, value = null)
{
return key.toString().split('.').reduce((t, i) => t[i] || (value || key), window[this.key]);
let path = key.toString().split('::');
let keys = path.pop().toString().split('.');

if (path.length > 0) {
path[0] += '::';
}

return path.concat(keys).reduce((t, i) => t[i] || (value || key), window[this.key]);
}
}
50 changes: 45 additions & 5 deletions src/I18nServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Pine\I18n;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Blade;
Expand All @@ -18,10 +19,10 @@ public function boot()
{
// Publish the assets
$this->publishes([
__DIR__.'/../resources/assets/js' => resource_path('assets/js/vendor'),
__DIR__.'/../resources/js' => resource_path('js/vendor'),
]);

// Register the custom blade directive
// Register the @translations blade directive
Blade::directive('translations', function ($key) {
return sprintf('<script>window[%s] = %s</script>', $key ?: "'translations'", $this->translations());
});
Expand All @@ -34,12 +35,51 @@ public function boot()
*/
protected function translations()
{
$files = File::files(resource_path('lang/'.App::getLocale()));

return collect($files)->flatMap(function ($file) {
$translations = collect($this->getFiles(resource_path('lang/')))->flatMap(function ($file) {
return [
($translation = $file->getBasename('.php')) => trans($translation),
];
});

return $translations->merge($this->packageTranslations());
}

/**
* Get the translations.
*
* @return \Illuminate\Support\Collection
*/
protected function packageTranslations()
{
return collect($this->app['translator']->getLoader()->namespaces())->flatMap(function ($path, $namespace) {
return [
($namespace .= '::') => collect($this->getFiles($path))->flatMap(function ($file) use ($namespace) {
return [
($translation = $file->getBasename('.php')) => trans($namespace . $translation),
];
}),
];
})->filter(function ($translations) {
return $translations->isNotEmpty();
});
}

/**
* Get the files for the given locale.
*
* @param string $path
* @return array
*/
protected function getFiles($path)
{
$path = Str::finish($path, '/');

if (file_exists($path . App::getLocale())) {
return File::files($path . App::getLocale());
} elseif (file_exists($path . config('app.fallback_locale'))) {
return File::files($path . config('app.fallback_locale'));
}

return [];
}
}

0 comments on commit ce4ceae

Please sign in to comment.