Skip to content
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

Issue 310 include lang namespaces #342

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions database/migrations/2019_10_24_115350_create_namespaces_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNamespacesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ltm_namespaces', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('namespace');
$table->string('path');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ltm_namespaces');
}
}
94 changes: 73 additions & 21 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;
use Barryvdh\TranslationManager\Models\Translation;
use Barryvdh\TranslationManager\Models\TranslationNamespace;
use Barryvdh\TranslationManager\Events\TranslationsExportedEvent;

class Manager
Expand Down Expand Up @@ -51,40 +52,60 @@ protected function getIgnoredLocales()
return ($result && is_array($result)) ? $result : [];
}

public function importTranslations($replace = false, $base = null, $import_group = false)
public function importTranslations($replace = false)
{
$counter = 0;
//allows for vendor lang files to be properly recorded through recursion.
$vendor = true;
if ($base == null) {

// Main app and vendor translations
$counter += $this->importArrayTranslations($replace);

// other registered namespaces
$namespaces = \Lang::getLoader()->namespaces();
foreach ($namespaces as $namespace => $dir) {
$counter += $this->importArrayTranslations($replace, $dir, $namespace);
}

// Json translations
$counter += $this->importJsonTranslations($replace);

return $counter;
}

/**
* Import array translations
*
* @return int
*/
public function importArrayTranslations($replace = false, $base = null, $namespace = null)
{
$counter = 0;

if (!$base) {
$base = $this->app['path.lang'];
$vendor = false;
} else {
$vendor = $namespace === null;
}

foreach ($this->files->directories($base) as $langPath) {
$locale = basename($langPath);

//import langfiles for each vendor
// import langfiles for each vendor
if ($locale == 'vendor') {
foreach ($this->files->directories($langPath) as $vendor) {
$counter += $this->importTranslations($replace, $vendor);
$counter += $this->importArrayTranslations($replace, $vendor);
}

continue;
}
$vendorName = $this->files->name($this->files->dirname($langPath));

foreach ($this->files->allfiles($langPath) as $file) {
$info = pathinfo($file);
$group = $info['filename'];
if ($import_group) {
if ($import_group !== $group) {
continue;
}
}

if (in_array($group, $this->config['exclude_groups'])) {
continue;
}

$subLangPath = str_replace($langPath.DIRECTORY_SEPARATOR, '', $info['dirname']);
$subLangPath = str_replace(DIRECTORY_SEPARATOR, '/', $subLangPath);
$langPath = str_replace(DIRECTORY_SEPARATOR, '/', $langPath);
Expand All @@ -93,22 +114,45 @@ public function importTranslations($replace = false, $base = null, $import_group
$group = $subLangPath.'/'.$group;
}

if (! $vendor) {
$translations = \Lang::getLoader()->load($locale, $group);
if (!$vendor) {
$translations = \Lang::getLoader()->load($locale, $group, $namespace);
if ($namespace) {
$group = $namespace .'::'. $group;
}
} else {
$translations = include $file;
$group = 'vendor/'.$vendorName;
$vendorName = $this->files->name($this->files->dirname($langPath));
$group = 'vendor/'. $vendorName .'/'. $group;
}

if ($translations && is_array($translations)) {
foreach (Arr::dot($translations) as $key => $value) {
$importedTranslation = $this->importTranslation($key, $value, $locale, $group, $replace);
$counter += $importedTranslation ? 1 : 0;
}

if ($namespace) {
TranslationNamespace::updateOrCreate(
['namespace' => $namespace],
['path' => $base]
);
}
}
}
}

return $counter;
}

/**
* Import Json translations
*
* @return int
*/
public function importJsonTranslations($replace = false)
{
$counter = 0;

foreach ($this->files->files($this->app['path.lang']) as $jsonTranslationFile) {
if (strpos($jsonTranslationFile, '.json') === false) {
continue;
Expand Down Expand Up @@ -268,17 +312,24 @@ public function exportTranslations($group = null, $json = false)
$tree = $this->makeTree(Translation::ofTranslatedGroup($group)
->orderByGroupKeys(Arr::get($this->config, 'sort_keys', false))
->get());
$namespaces = TranslationNamespace::all()->pluck('path', 'namespace');

$group_name = $group;
foreach ($tree as $locale => $groups) {
if (isset($groups[$group])) {
$translations = $groups[$group];
if (isset($groups[$group_name])) {
$translations = $groups[$group_name];
$path = $this->app['path.lang'];

$locale_path = $locale.DIRECTORY_SEPARATOR.$group;
if (Str::contains($group, '::')) {
list($namespace, $group) = explode('::', $group, 2);
$path = $namespaces[$namespace] ?? $this->app['path.lang'];
}

if ($vendor) {
$path = $basePath.'/'.$group.'/'.$locale;
$locale_path = Str::after($group, '/');
list($package, $group) = explode('/', Str::after($group, 'vendor/'), 2);
$path = $basePath . DIRECTORY_SEPARATOR .'vendor'. DIRECTORY_SEPARATOR . $package;
}
$locale_path = $locale.DIRECTORY_SEPARATOR.$group;
$subfolders = explode(DIRECTORY_SEPARATOR, $locale_path);
array_pop($subfolders);

Expand Down Expand Up @@ -371,6 +422,7 @@ public function cleanTranslations()
public function truncateTranslations()
{
Translation::truncate();
TranslationNamespace::truncate();
}

public function getLocales()
Expand Down
11 changes: 11 additions & 0 deletions src/Models/TranslationNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Barryvdh\TranslationManager\Models;

use Illuminate\Database\Eloquent\Model;

class TranslationNamespace extends Model
{
protected $table = 'ltm_namespaces';
protected $guarded = ['id', 'created_at', 'updated_at'];
}