Skip to content

Commit b257557

Browse files
committed
Align naming with laravel
1 parent 931265c commit b257557

18 files changed

+668
-676
lines changed

src/Console/Commands/AddTranslationKey.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function handle()
2727
// exception is thrown
2828
if ($type === 'single') {
2929
try {
30-
$this->translation->addSingleTranslation($language, 'single', $key, $value);
30+
$this->translation->addStringKeyTranslation($language, 'single', $key, $value);
3131

3232
return $this->info(__('translation::translation.language_key_added'));
3333
} catch (\Exception $e) {
@@ -36,7 +36,7 @@ public function handle()
3636
} elseif ($type === 'group') {
3737
try {
3838
$file = str_replace('.php', '', $file);
39-
$this->translation->addGroupTranslation($language, $file, $key, $value);
39+
$this->translation->addShortKeyTranslation($language, $file, $key, $value);
4040

4141
return $this->info(__('translation::translation.language_key_added'));
4242
} catch (\Exception $e) {

src/Console/Commands/SynchroniseTranslations.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Filesystem\Filesystem;
7-
use JoeDixon\Translation\Drivers\Database;
8-
use JoeDixon\Translation\Drivers\File;
7+
use JoeDixon\Translation\Drivers\Database\Database;
8+
use JoeDixon\Translation\Drivers\File\File;
99
use JoeDixon\Translation\Drivers\Translation;
1010
use JoeDixon\Translation\Scanner;
1111

@@ -133,7 +133,7 @@ private function mergeGroupTranslations($driver, $language, $groups)
133133
if (is_array($value)) {
134134
continue;
135135
}
136-
$driver->addGroupTranslation($language, $group, $key, $value);
136+
$driver->addShortKeyTranslation($language, $group, $key, $value);
137137
}
138138
}
139139
}

src/ContractDatabaseLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ public function __construct(Translation $translation)
2525
public function load($locale, $group, $namespace = null)
2626
{
2727
if ($group == '*' && $namespace == '*') {
28-
return $this->translation->getSingleTranslationsFor($locale)->get('single', collect())->toArray();
28+
return $this->translation->allStringKeyTranslationsFor($locale)->get('single', collect())->toArray();
2929
}
3030

3131
if (is_null($namespace) || $namespace == '*') {
32-
return $this->translation->getGroupTranslationsFor($locale)->filter(function ($value, $key) use ($group) {
32+
return $this->translation->allShortKeyTranslationsFor($locale)->filter(function ($value, $key) use ($group) {
3333
return $key === $group;
3434
})->first();
3535
}
3636

37-
return $this->translation->getGroupTranslationsFor($locale)->filter(function ($value, $key) use ($group, $namespace) {
37+
return $this->translation->allShortKeyTranslationsFor($locale)->filter(function ($value, $key) use ($group, $namespace) {
3838
return $key === "{$namespace}::{$group}";
3939
})->first();
4040
}

src/Database/Factories/TranslationFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ public function definition()
2525
];
2626
}
2727

28-
public function single()
28+
public function shortKey()
2929
{
3030
return $this->state(function () {
3131
return [
32-
'group' => 'single',
33-
32+
'group' => fake()->word,
3433
];
3534
});
3635
}
3736

38-
public function group()
37+
public function stringKey()
3938
{
4039
return $this->state(function () {
4140
return [
42-
'group' => fake()->word,
41+
'group' => 'string',
42+
4343
];
4444
});
4545
}

src/Drivers/Database.php

Lines changed: 0 additions & 200 deletions
This file was deleted.

src/Drivers/Database/Database.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace JoeDixon\Translation\Drivers\Database;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Collection;
7+
use JoeDixon\Translation\Drivers\Translation;
8+
use JoeDixon\Translation\Exceptions\LanguageExistsException;
9+
use JoeDixon\Translation\Language;
10+
11+
class Database extends Translation
12+
{
13+
use InteractsWithStringKeys, InteractsWithShortKeys;
14+
15+
public function __construct(protected $sourceLanguage, protected $scanner)
16+
{
17+
}
18+
19+
/**
20+
* Get all languages.
21+
*/
22+
public function allLanguages(): Collection
23+
{
24+
return Language::all()->mapWithKeys(function ($language) {
25+
return [$language->language => $language->name ?: $language->language];
26+
});
27+
}
28+
29+
/**
30+
* Determine whether the given language exists.
31+
*/
32+
public function languageExists(string $language): bool
33+
{
34+
return $this->getLanguage($language) ? true : false;
35+
}
36+
37+
/**
38+
* Add a new language.
39+
*/
40+
public function addLanguage(string $language, ?string $name = null): void
41+
{
42+
if ($this->languageExists($language)) {
43+
throw new LanguageExistsException(__('translation::errors.language_exists', ['language' => $language]));
44+
}
45+
46+
Language::create([
47+
'language' => $language,
48+
'name' => $name,
49+
]);
50+
}
51+
52+
/**
53+
* Get all translations.
54+
*/
55+
public function allTranslations(): Collection
56+
{
57+
return $this->allLanguages()->mapWithKeys(function ($name, $language) {
58+
return [$language => $this->allTranslationsFor($language)];
59+
});
60+
}
61+
62+
/**
63+
* Get all translations for a given language.
64+
*/
65+
public function allTranslationsFor(string $language): Collection
66+
{
67+
return Collection::make([
68+
'short' => $this->allShortKeyTranslationsFor($language),
69+
'string' => $this->allStringKeyTranslationsFor($language),
70+
]);
71+
}
72+
73+
/**
74+
* Get a language from the database.
75+
*/
76+
private function getLanguage(string $language): ?Model
77+
{
78+
return Language::where('language', $language)->first();
79+
}
80+
}

0 commit comments

Comments
 (0)