Skip to content

Commit

Permalink
Merge branch 'feature/chinese_locales' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
octfx committed May 9, 2024
2 parents d272e61 + 20b910a commit 515687d
Show file tree
Hide file tree
Showing 48 changed files with 335 additions and 331 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/docker-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
DB_CONNECTION: 'sqlite'
DB_DATABASE: 'db.sqlite'
run: touch database/db.sqlite; php artisan migrate
- name: Update submodules
run: git submodule update --init
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: 'sqlite'
Expand Down Expand Up @@ -109,6 +111,8 @@ jobs:
DB_USERNAME: 'scw__api'
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
run: touch database/db.sqlite; php artisan migrate
- name: Update submodules
run: git submodule update --init
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: 'mysql'
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "storage/app/api/scunpacked-data"]
path = storage/app/api/scunpacked-data
url = https://github.com/StarCitizenWiki/scunpacked-data.git
[submodule "storage/app/api/ScToolBoxLocales"]
path = storage/app/api/ScToolBoxLocales
url = https://github.com/StarCitizenToolBox/LocalizationData
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ COPY --chown=www-data:www-data / /api
RUN rm -rf storage/app/api/scunpacked-data
RUN git clone https://github.com/StarCitizenWiki/scunpacked-data --branch=master --depth=1 storage/app/api/scunpacked-data

RUN rm -rf storage/app/api/ScToolBoxLocales
RUN git clone https://github.com/StarCitizenToolBox/LocalizationData --branch=main --depth=1 storage/app/api/ScToolBoxLocales

RUN /usr/bin/composer dump-autoload --optimize --classmap-authoritative

### Final Image
Expand Down
7 changes: 3 additions & 4 deletions app/Console/Commands/SC/ImportItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class ImportItems extends AbstractQueueCommand
*/
public function handle(): int
{
$labels = (new Labels())->getData();
$manufacturers = (new Manufacturers())->getData();

$files = File::allFiles(scdata('items')) + Storage::allFiles(scdata('ships'));
Expand All @@ -67,13 +66,13 @@ public function handle(): int
->tap(function (Collection $chunks) {
$this->createProgressBar($chunks->count());
})
->each(function (Collection $chunk) use ($labels, $manufacturers) {
->each(function (Collection $chunk) use ($manufacturers) {
$this->bar->advance();

$chunk->map(function (string $file) use ($labels, $manufacturers) {
$chunk->map(function (string $file) use ($manufacturers) {
return [
'file' => $file,
'item' => (new Item($file, $labels, $manufacturers))->getData(),
'item' => (new Item($file, $manufacturers))->getData(),
];
})
->filter(function (array $data) {
Expand Down
6 changes: 3 additions & 3 deletions app/Console/Commands/SC/ImportMissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use App\Models\SC\Reputation\Reward;
use App\Models\SC\Reputation\Scope;
use App\Models\SC\Reputation\Standing;
use Exception;
use App\Models\System\Language;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

Expand Down Expand Up @@ -168,7 +168,7 @@ private function importGiver(): void

if (! empty($data['description'])) {
$giver->translations()->updateOrCreate([
'locale_code' => 'en_EN',
'locale_code' => Language::ENGLISH,
], [
'translation' => $data['description'],
]);
Expand Down Expand Up @@ -240,7 +240,7 @@ private function importMissions(): void

if (! str_starts_with($data['description'], '@')) {
$mission->translations()->updateOrCreate([
'locale_code' => 'en_EN',
'locale_code' => Language::ENGLISH,
], [
'translation' => $data['description'],
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Console\Commands\AbstractQueueCommand;
use App\Jobs\StarCitizen\Galactapedia\TranslateArticle;
use App\Models\StarCitizen\Galactapedia\Article;
use App\Models\System\Language;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

Expand Down Expand Up @@ -39,7 +40,7 @@ public function handle(): int
'translations',
function (Builder $query) {
$query
->where('locale_code', 'en_EN')
->where('locale_code', Language::ENGLISH)
->whereRaw("translation <> ''");
}
)
Expand Down
38 changes: 16 additions & 22 deletions app/Http/Controllers/Api/AbstractApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Events\ApiRouteCalled;
use App\Http\Controllers\Controller;
use App\Models\System\Language;
use App\Transformers\Api\LocalizableTransformerInterface;
use App\Transformers\Api\V1\AbstractV1Transformer as V1Transformer;
use Carbon\Carbon;
Expand Down Expand Up @@ -62,8 +63,9 @@
description: 'Localization to use.',
collectionFormat: 'csv',
enum: [
'de_DE',
'en_EN',
Language::ENGLISH,
Language::GERMAN,
Language::CHINESE,
]
),
)]
Expand Down Expand Up @@ -131,7 +133,7 @@ abstract class AbstractApiController extends Controller
/**
* AbstractApiController constructor.
*
* @param Request $request API Request
* @param Request $request API Request
*/
public function __construct(Request $request)
{
Expand All @@ -158,12 +160,12 @@ protected function processRequestParams(): void
*/
private function processLimit(): void
{
if ($this->request->has(self::LIMIT) && null !== $this->request->get(self::LIMIT, null)) {
$itemLimit = (int)$this->request->get(self::LIMIT);
if ($this->request->has(self::LIMIT) && $this->request->get(self::LIMIT, null) !== null) {
$itemLimit = (int) $this->request->get(self::LIMIT);

if ($itemLimit > 0) {
$this->limit = $itemLimit;
} elseif (0 === $itemLimit) {
} elseif ($itemLimit === 0) {
$this->limit = 0;
} else {
$this->errors[self::LIMIT] = static::INVALID_LIMIT_STRING;
Expand All @@ -176,15 +178,13 @@ private function processLimit(): void
*/
private function processLocale(): void
{
if ($this->request->has(self::LOCALE) && null !== $this->request->get(self::LOCALE, null)) {
if ($this->request->has(self::LOCALE) && $this->request->get(self::LOCALE, null) !== null) {
$this->setLocale($this->request->get(self::LOCALE));
}
}

/**
* Set the Locale
*
* @param string $localeCode
*/
protected function setLocale(string $localeCode): void
{
Expand Down Expand Up @@ -215,9 +215,7 @@ protected function disablePagination(): self
* Creates the API Response, Collection if no pagination, Paginator if a limit is set
* Item if a single model is given
*
* @param Builder|Model|Collection $query
*
* @return Response
* @param Builder|Model|Collection $query
*/
protected function getResponse($query): Response
{
Expand Down Expand Up @@ -253,7 +251,6 @@ protected function getResponse($query): Response
'forwarded-for' => $this->request->header('X-Forwarded-For', '127.0.0.1'),
]);


$resource = new \League\Fractal\Resource\Collection(
$query->get(),
$this->transformer
Expand All @@ -275,11 +272,11 @@ protected function getMeta(): array
'processed_at' => Carbon::now()->toDateTimeString(),
];

if (!empty($this->errors)) {
if (! empty($this->errors)) {
$meta['errors'] = $this->errors;
}

if (!empty($this->transformer->getAvailableIncludes())) {
if (! empty($this->transformer->getAvailableIncludes())) {
$meta['valid_relations'] = array_map(
'Illuminate\Support\Str::snake',
$this->transformer->getAvailableIncludes()
Expand All @@ -294,19 +291,19 @@ protected function getMeta(): array
*/
private function processIncludes(): void
{
if ($this->request->has('include') && null !== $this->request->get('include', null)) {
if ($this->request->has('include') && $this->request->get('include', null) !== null) {
$this->checkIncludes($this->request->get('include', []));
}
}

/**
* Processes the given 'include' model relation key
*
* @param string|array $relations
* @param string|array $relations
*/
protected function checkIncludes($relations): void
{
if (!is_array($relations)) {
if (! is_array($relations)) {
$relations = explode(',', $relations);
}

Expand All @@ -322,7 +319,7 @@ static function ($relation) {
)
->each(
function ($relation) {
if (!in_array($relation, $this->transformer->getAvailableIncludes(), true)) {
if (! in_array($relation, $this->transformer->getAvailableIncludes(), true)) {
$this->errors['include'][] = sprintf(static::INVALID_RELATION_STRING, Str::snake($relation));
}
}
Expand All @@ -331,9 +328,6 @@ function ($relation) {

/**
* Cleans the name for query use
*
* @param string $name
* @return string
*/
protected function cleanQueryName(string $name): string
{
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/Api/V2/AbstractApiV2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Events\ApiRouteCalled;
use App\Http\Controllers\Controller;
use App\Models\System\Language;
use Illuminate\Http\Request;
use OpenApi\Attributes as OA;
use Spatie\QueryBuilder\AllowedInclude;
Expand Down Expand Up @@ -48,8 +49,9 @@
schema: new OA\Schema(
description: 'Localization to use.',
enum: [
'de_DE',
'en_EN',
Language::ENGLISH,
Language::GERMAN,
Language::CHINESE,
]
),
)]
Expand Down
12 changes: 7 additions & 5 deletions app/Http/Resources/AbstractTranslationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
title: 'Grouped Translations',
description: 'Translations of an entity',
properties: [
new OA\Property(property: 'en_EN', type: 'string'),
new OA\Property(property: 'de_DE', type: 'string'),
new OA\Property(property: Language::ENGLISH, type: 'string'),
new OA\Property(property: Language::GERMAN, type: 'string'),
new OA\Property(property: Language::CHINESE, type: 'string'),
],
type: 'object'
)]
Expand All @@ -38,14 +39,15 @@ protected function getTranslation($model, Request $request, $translationKey = 't
$translations = $model->translationsCollection();

$locale = $request->get('locale');

if (! empty($locale)) {
return $this->getSingleTranslation($translations, $request->get('locale'), $translationKey);
}

$translations = $translations->map(
function ($translation) use ($translationKey, $translations) {
if ($translation instanceof Language) {
return $this->getSingleTranslation($translations, 'en_EN', $translationKey);
return $this->getSingleTranslation($translations, Language::ENGLISH, $translationKey);
}

return $this->getSingleTranslation($translation, $translationKey);
Expand Down Expand Up @@ -75,8 +77,8 @@ private function getSingleTranslation($translations, string $locale, $translatio

if ($translations->has($locale) && ! $translations->get($locale) instanceof Language) {
$translation = $translations->get($locale)[$translationKey];
} elseif ($translations->has('en_EN')) {
$translation = $translations->get('en_EN')[$translationKey];
} elseif ($translations->has(Language::ENGLISH)) {
$translation = $translations->get(Language::ENGLISH)[$translationKey];
}

return $translation;
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Resources/SC/Vehicle/VehicleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Http\Resources\SC\ItemSpecification\ArmorResource;
use App\Http\Resources\SC\Shop\ShopResource;
use App\Http\Resources\TranslationResourceFactory;
use App\Models\System\Language;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use OpenApi\Attributes as OA;
Expand Down Expand Up @@ -353,10 +354,10 @@ public function toArray(Request $request): array
'armor' => new ArmorResource($this->armor),
]),
'foci' => [
['en_EN' => $this->career],
[Language::ENGLISH => $this->career],
],
'type' => [
'en_EN' => $this->role,
Language::ENGLISH => $this->role,
],
'description' => TranslationResourceFactory::getTranslationResource($request, $this),
'size_class' => $this->size,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/TranslationCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TranslationCollection extends ResourceCollection
* @param Request $request
* @return array
*/
public function toArray($request): array
public function toArray(Request $request): array
{
$out = [];
foreach ($this->collection as $translation) {
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Resources/TranslationResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Http\Resources;

use App\Models\System\Language;
use App\Models\System\Translation\AbstractHasTranslations;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\MissingValue;
Expand All @@ -12,8 +13,7 @@
class TranslationResourceFactory
{
/**
* @param Request $request
* @param AbstractBaseResource|AbstractHasTranslations|MissingValue $model
* @param AbstractBaseResource|AbstractHasTranslations|MissingValue $model
* @return TranslationCollection|mixed
*/
public static function getTranslationResource(Request $request, $model): mixed
Expand All @@ -33,7 +33,7 @@ public static function getTranslationResource(Request $request, $model): mixed
$transformed = $collection->toArray($request);

if ($request->has('locale')) {
return $transformed[$request->get('locale')] ?? $transformed['en_EN'] ?? new MissingValue();
return $transformed[$request->get('locale')] ?? $transformed[Language::ENGLISH] ?? new MissingValue();
}

return $collection;
Expand Down
Loading

0 comments on commit 515687d

Please sign in to comment.