Skip to content

Commit

Permalink
Align naming with laravel
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Jul 12, 2022
1 parent 931265c commit b257557
Show file tree
Hide file tree
Showing 18 changed files with 668 additions and 676 deletions.
4 changes: 2 additions & 2 deletions src/Console/Commands/AddTranslationKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function handle()
// exception is thrown
if ($type === 'single') {
try {
$this->translation->addSingleTranslation($language, 'single', $key, $value);
$this->translation->addStringKeyTranslation($language, 'single', $key, $value);

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

return $this->info(__('translation::translation.language_key_added'));
} catch (\Exception $e) {
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Commands/SynchroniseTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use JoeDixon\Translation\Drivers\Database;
use JoeDixon\Translation\Drivers\File;
use JoeDixon\Translation\Drivers\Database\Database;
use JoeDixon\Translation\Drivers\File\File;
use JoeDixon\Translation\Drivers\Translation;
use JoeDixon\Translation\Scanner;

Expand Down Expand Up @@ -133,7 +133,7 @@ private function mergeGroupTranslations($driver, $language, $groups)
if (is_array($value)) {
continue;
}
$driver->addGroupTranslation($language, $group, $key, $value);
$driver->addShortKeyTranslation($language, $group, $key, $value);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ContractDatabaseLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public function __construct(Translation $translation)
public function load($locale, $group, $namespace = null)
{
if ($group == '*' && $namespace == '*') {
return $this->translation->getSingleTranslationsFor($locale)->get('single', collect())->toArray();
return $this->translation->allStringKeyTranslationsFor($locale)->get('single', collect())->toArray();
}

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

return $this->translation->getGroupTranslationsFor($locale)->filter(function ($value, $key) use ($group, $namespace) {
return $this->translation->allShortKeyTranslationsFor($locale)->filter(function ($value, $key) use ($group, $namespace) {
return $key === "{$namespace}::{$group}";
})->first();
}
Expand Down
10 changes: 5 additions & 5 deletions src/Database/Factories/TranslationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ public function definition()
];
}

public function single()
public function shortKey()
{
return $this->state(function () {
return [
'group' => 'single',

'group' => fake()->word,
];
});
}

public function group()
public function stringKey()
{
return $this->state(function () {
return [
'group' => fake()->word,
'group' => 'string',

];
});
}
Expand Down
200 changes: 0 additions & 200 deletions src/Drivers/Database.php

This file was deleted.

80 changes: 80 additions & 0 deletions src/Drivers/Database/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace JoeDixon\Translation\Drivers\Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use JoeDixon\Translation\Drivers\Translation;
use JoeDixon\Translation\Exceptions\LanguageExistsException;
use JoeDixon\Translation\Language;

class Database extends Translation
{
use InteractsWithStringKeys, InteractsWithShortKeys;

public function __construct(protected $sourceLanguage, protected $scanner)
{
}

/**
* Get all languages.
*/
public function allLanguages(): Collection
{
return Language::all()->mapWithKeys(function ($language) {
return [$language->language => $language->name ?: $language->language];
});
}

/**
* Determine whether the given language exists.
*/
public function languageExists(string $language): bool
{
return $this->getLanguage($language) ? true : false;
}

/**
* Add a new language.
*/
public function addLanguage(string $language, ?string $name = null): void
{
if ($this->languageExists($language)) {
throw new LanguageExistsException(__('translation::errors.language_exists', ['language' => $language]));
}

Language::create([
'language' => $language,
'name' => $name,
]);
}

/**
* Get all translations.
*/
public function allTranslations(): Collection
{
return $this->allLanguages()->mapWithKeys(function ($name, $language) {
return [$language => $this->allTranslationsFor($language)];
});
}

/**
* Get all translations for a given language.
*/
public function allTranslationsFor(string $language): Collection
{
return Collection::make([
'short' => $this->allShortKeyTranslationsFor($language),
'string' => $this->allStringKeyTranslationsFor($language),
]);
}

/**
* Get a language from the database.
*/
private function getLanguage(string $language): ?Model
{
return Language::where('language', $language)->first();
}
}
Loading

0 comments on commit b257557

Please sign in to comment.