Skip to content
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
34 changes: 29 additions & 5 deletions src/Drivers/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,18 @@ public function getGroupTranslationsFor($language)
if (Str::contains($group->getPathname(), 'vendor')) {
$vendor = Str::before(Str::after($group->getPathname(), 'vendor'.DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR);

return ["{$vendor}::{$group->getBasename('.php')}" => new Collection(Arr::dot($this->disk->getRequire($group->getPathname())))];
$group_key = empty($group->getRelativePath())
? $group->getBasename('.php')
: $group->getRelativePath().DIRECTORY_SEPARATOR.$group->getBasename('.php');

return ["{$vendor}::{$group_key}" => new Collection(Arr::dot($this->disk->getRequire($group->getPathname())))];
}

return [$group->getBasename('.php') => new Collection(Arr::dot($this->disk->getRequire($group->getPathname())))];
$group_key = empty($group->getRelativePath())
? $group->getBasename('.php')
: $group->getRelativePath().DIRECTORY_SEPARATOR.$group->getBasename('.php');

return [$group_key => new Collection(Arr::dot($this->disk->getRequire($group->getPathname())))];
});
}

Expand Down Expand Up @@ -278,14 +286,22 @@ public function saveGroupTranslations($language, $group, $translations)
*/
private function saveNamespacedGroupTranslations($language, $group, $translations)
{

[$namespace, $group] = explode('::', $group);
$directory = "{$this->languageFilesPath}".DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR."{$namespace}".DIRECTORY_SEPARATOR."{$language}";

if (str_contains($group, '/')) {
$file = Str::of($group)->afterLast('/');
$directory = $directory.DIRECTORY_SEPARATOR.Str::of($group)->beforeLast('/');
} else {
$file = $group;
}

if (! $this->disk->exists($directory)) {
$this->disk->makeDirectory($directory, 0755, true);
}

$this->disk->put("$directory".DIRECTORY_SEPARATOR."{$group}.php", "<?php\n\nreturn ".var_export($translations, true).';'.\PHP_EOL);
$this->disk->put("$directory".DIRECTORY_SEPARATOR."{$file}.php", "<?php\n\nreturn ".var_export($translations, true).';'.\PHP_EOL);
}

/**
Expand Down Expand Up @@ -335,10 +351,18 @@ public function getGroupsFor($language)
if (Str::contains($file->getPathname(), 'vendor')) {
$vendor = Str::before(Str::after($file->getPathname(), 'vendor'.DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR);

return "{$vendor}::{$file->getBasename('.php')}";
$file_key = empty($file->getRelativePath())
? $file->getBasename('.php')
: $file->getRelativePath().DIRECTORY_SEPARATOR.$file->getBasename('.php');

return "{$vendor}::{$file_key}";
}

return $file->getBasename('.php');
$file_key = empty($file->getRelativePath())
? $file->getBasename('.php')
: $file->getRelativePath().DIRECTORY_SEPARATOR.$file->getBasename('.php');

return $file_key;
});
}

Expand Down
56 changes: 51 additions & 5 deletions tests/DatabaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,31 @@ public function it_can_add_a_vendor_namespaced_translations()
}

/** @test */
public function it_can_add_a_nested_translation()
public function it_can_add_a_inner_deep_vendor_namespaced_translations()
{
$this->translation->addGroupTranslation('en', 'test', 'test.nested', 'Nested!');
$this->translation->addGroupTranslation('es', 'translation_test::test/inner/deep', 'hello', 'Hola!');

$this->assertEquals($this->translation->getGroupTranslationsFor('en')->toArray(), [
'test' => [
'test.nested' => 'Nested!',
$this->assertEquals($this->translation->allTranslationsFor('es')->toArray(), [
'group' => [
'translation_test::test/inner/deep' => [
'hello' => 'Hola!',
],
],
'single' => [],
]);
}

/** @test */
public function it_can_add_a_nested_translation()
{
$this->translation->addGroupTranslation('en', 'test', 'test.nested', 'Nested!');

$translations = $this->translation->getGroupTranslationsFor('en')->toArray();
$this->assertArrayHasKey('test', $translations);
$this->assertArrayHasKey('test.nested', $translations['test']);
$this->assertEquals('Nested!', $translations['test']['test.nested']);
}

/** @test */
public function it_can_add_nested_vendor_namespaced_translations()
{
Expand All @@ -249,6 +263,21 @@ public function it_can_add_nested_vendor_namespaced_translations()
]);
}

/** @test */
public function it_can_add_nested_inner_deep_vendor_namespaced_translations()
{
$this->translation->addGroupTranslation('es', 'translation_test::test/inner/deep', 'nested.hello', 'Hola!');

$this->assertEquals($this->translation->allTranslationsFor('es')->toArray(), [
'group' => [
'translation_test::test/inner/deep' => [
'nested.hello' => 'Hola!',
],
],
'single' => [],
]);
}

/** @test */
public function it_can_merge_a_namespaced_language_with_the_base_language()
{
Expand All @@ -266,6 +295,23 @@ public function it_can_merge_a_namespaced_language_with_the_base_language()
]);
}

/** @test */
public function it_can_merge_a_inner_deep_namespaced_language_with_the_base_language()
{
$this->translation->addGroupTranslation('en', 'translation_test::test/inner/deep', 'hello', 'Hello');
$this->translation->addGroupTranslation('es', 'translation_test::test/inner/deep', 'hello', 'Hola!');
$translations = $this->translation->getSourceLanguageTranslationsWith('es');

$this->assertEquals($translations->toArray(), [
'group' => [
'translation_test::test/inner/deep' => [
'hello' => ['en' => 'Hello', 'es' => 'Hola!'],
],
],
'single' => [],
]);
}

/** @test */
public function a_list_of_languages_can_be_viewed()
{
Expand Down
129 changes: 95 additions & 34 deletions tests/FileDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ public function it_returns_all_translations()
$translations = $this->translation->allTranslations();

$this->assertEquals($translations->count(), 2);
$this->assertEquals(['single' => ['single' => ['Hello' => 'Hello', "What's up" => "What's up!"]], 'group' => ['test' => ['hello' => 'Hello', 'whats_up' => "What's up!"]]], $translations->toArray()['en']);
$expectations = [
'single' => ['single' => ['Hello' => 'Hello', "What's up" => "What's up!"]],
'group' => [
'test' => ['hello' => 'Hello', "whats_up" => "What's up!"],
'inner/deep/deep' => ['hello' => 'Hello', "whats_up" => "What's up!"],
'inner/inner' => ['hello' => 'Hello', "whats_up" => "What's up!"],
],
];
$this->assertEquals($expectations, $translations->toArray()['en']);
$this->assertArrayHasKey('en', $translations->toArray());
$this->assertArrayHasKey('es', $translations->toArray());
}
Expand All @@ -62,7 +70,15 @@ public function it_returns_all_translations_for_a_given_language()
{
$translations = $this->translation->allTranslationsFor('en');
$this->assertEquals($translations->count(), 2);
$this->assertEquals(['single' => ['single' => ['Hello' => 'Hello', "What's up" => "What's up!"]], 'group' => ['test' => ['hello' => 'Hello', 'whats_up' => "What's up!"]]], $translations->toArray());
$expectations = [
'single' => ['single' => ['Hello' => 'Hello', "What's up" => "What's up!"]],
'group' => [
'test' => ['hello' => 'Hello', "whats_up" => "What's up!"],
'inner/deep/deep' => ['hello' => 'Hello', "whats_up" => "What's up!"],
'inner/inner' => ['hello' => 'Hello', "whats_up" => "What's up!"],
],
];
$this->assertEquals($expectations, $translations->toArray());
$this->assertArrayHasKey('single', $translations->toArray());
$this->assertArrayHasKey('group', $translations->toArray());
}
Expand Down Expand Up @@ -105,7 +121,11 @@ public function it_can_add_a_new_translation_to_an_existing_translation_group()

$translations = $this->translation->allTranslationsFor('en');

$this->assertEquals(['test' => ['hello' => 'Hello', 'whats_up' => 'What\'s up!', 'test' => 'Testing']], $translations->toArray()['group']);
$this->assertEquals([
'test' => ['hello' => 'Hello', 'whats_up' => 'What\'s up!', 'test' => 'Testing'],
'inner/deep/deep' => ['hello' => 'Hello', "whats_up" => "What's up!"],
'inner/inner' => ['hello' => 'Hello', "whats_up" => "What's up!"],
], $translations->toArray()['group']);

file_put_contents(
app()['path.lang'].'/en/test.php',
Expand Down Expand Up @@ -145,7 +165,11 @@ public function it_can_get_a_collection_of_group_names_for_a_given_language()
{
$groups = $this->translation->getGroupsFor('en');

$this->assertEquals($groups->toArray(), ['test']);
$this->assertEquals([
'inner/deep/deep',
'inner/inner',
'test',
], $groups->toArray());
}

/** @test */
Expand All @@ -160,6 +184,14 @@ public function it_can_merge_a_language_with_the_base_language()
'hello' => ['en' => 'Hello', 'es' => 'Hola!'],
'whats_up' => ['en' => "What's up!", 'es' => ''],
],
'inner/deep/deep' => [
'hello' => ['en' => 'Hello', 'es' => ''],
'whats_up' => ['en' => "What's up!", 'es' => ''],
],
'inner/inner' => [
'hello' => ['en' => 'Hello', 'es' => ''],
'whats_up' => ['en' => "What's up!", 'es' => ''],
],
],
'single' => [
'single' => [
Expand Down Expand Up @@ -196,18 +228,32 @@ public function it_can_add_a_vendor_namespaced_translations()
}

/** @test */
public function it_can_add_a_nested_translation()
public function it_can_add_a_inner_deep_vendor_namespaced_translations()
{
$this->translation->addGroupTranslation('en', 'test', 'test.nested', 'Nested!');
$this->translation->addGroupTranslation('es', 'translation_test::test/inner/deep', 'hello', 'Hola!');

$this->assertEquals($this->translation->getGroupTranslationsFor('en')->toArray(), [
'test' => [
'hello' => 'Hello',
'test.nested' => 'Nested!',
'whats_up' => 'What\'s up!',
$this->assertEquals($this->translation->allTranslationsFor('es')->toArray(), [
'group' => [
'translation_test::test/inner/deep' => [
'hello' => 'Hola!',
],
],
'single' => [],
]);

\File::deleteDirectory(__DIR__.'/fixtures/lang/vendor');
}

/** @test */
public function it_can_add_a_nested_translation()
{
$this->translation->addGroupTranslation('en', 'test', 'test.nested', 'Nested!');

$translations = $this->translation->getGroupTranslationsFor('en')->toArray();
$this->assertArrayHasKey('test', $translations);
$this->assertArrayHasKey('test.nested', $translations['test']);
$this->assertEquals('Nested!', $translations['test']['test.nested']);

file_put_contents(
app()['path.lang'].'/en/test.php',
"<?php\n\nreturn ".var_export(['hello' => 'Hello', 'whats_up' => 'What\'s up!'], true).';'.\PHP_EOL
Expand All @@ -232,39 +278,54 @@ public function it_can_add_nested_vendor_namespaced_translations()
}

/** @test */
public function it_can_merge_a_namespaced_language_with_the_base_language()
public function it_can_add_nested_inner_deep_vendor_namespaced_translations()
{
$this->translation->addGroupTranslation('en', 'translation_test::test', 'hello', 'Hello');
$this->translation->addGroupTranslation('es', 'translation_test::test', 'hello', 'Hola!');
$translations = $this->translation->getSourceLanguageTranslationsWith('es');
$this->translation->addGroupTranslation('es', 'translation_test::test/inner/deep', 'nested.hello', 'Hola!');

$this->assertEquals($translations->toArray(), [
$this->assertEquals($this->translation->allTranslationsFor('es')->toArray(), [
'group' => [
'test' => [
'hello' => ['en' => 'Hello', 'es' => ''],
'whats_up' => ['en' => "What's up!", 'es' => ''],
],
'translation_test::test' => [
'hello' => ['en' => 'Hello', 'es' => 'Hola!'],
],
],
'single' => [
'single' => [
'Hello' => [
'en' => 'Hello',
'es' => '',
],
"What's up" => [
'en' => "What's up!",
'es' => '',
],
'translation_test::test/inner/deep' => [
'nested.hello' => 'Hola!',
],
],
'single' => [],
]);

\File::deleteDirectory(__DIR__.'/fixtures/lang/vendor');
}

/** @test */
public function it_can_merge_a_namespaced_language_with_the_base_language()
{
$this->translation->addGroupTranslation('en', 'translation_test::test', 'hello', 'Hello');
$this->translation->addGroupTranslation('es', 'translation_test::test', 'hello', 'Hola!');
$translations = $this->translation->getSourceLanguageTranslationsWith('es');


$this->assertArrayHasKey('translation_test::test', $translations->toArray()['group']);
$this->assertArrayHasKey('hello', $translations->toArray()['group']['translation_test::test']);
$this->assertEquals('Hello', $translations->toArray()['group']['translation_test::test']['hello']['en']);
$this->assertEquals('Hola!', $translations->toArray()['group']['translation_test::test']['hello']['es']);

\File::deleteDirectory(__DIR__.'/fixtures/lang/vendor');
}

/** @test */
public function it_can_merge_a_inner_deep_namespaced_language_with_the_base_language()
{
$this->translation->addGroupTranslation('en', 'translation_test::test/inner/deep', 'hello', 'Hello');
$this->translation->addGroupTranslation('es', 'translation_test::test/inner/deep', 'hello', 'Hola!');
$translations = $this->translation->getSourceLanguageTranslationsWith('es');


$this->assertArrayHasKey('translation_test::test/inner/deep', $translations->toArray()['group']);
$this->assertArrayHasKey('hello', $translations->toArray()['group']['translation_test::test/inner/deep']);
$this->assertEquals('Hello', $translations->toArray()['group']['translation_test::test/inner/deep']['hello']['en']);
$this->assertEquals('Hola!', $translations->toArray()['group']['translation_test::test/inner/deep']['hello']['es']);

\File::deleteDirectory(__DIR__.'/fixtures/lang/vendor');
}

/** @test */
public function a_list_of_languages_can_be_viewed()
{
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/lang/en/inner/deep/deep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return array (
'hello' => 'Hello',
'whats_up' => 'What\'s up!',
);
6 changes: 6 additions & 0 deletions tests/fixtures/lang/en/inner/inner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return array (
'hello' => 'Hello',
'whats_up' => 'What\'s up!',
);