Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/storage/app/data
/storage/app/public
/storage/app/private/unesco/world-heritage-sites.json
/storage/app/private/unesco/normalized/world_heritage_sites_translation.json

/vendor
.env
Expand Down
4 changes: 4 additions & 0 deletions src/app/Console/Commands/AlgoliaImportWorldHeritages.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function handle(): int
'countries' => static function ($query): void {
$query->select(['countries.state_party_code', 'countries.name_en', 'countries.name_jp']);
},
'descriptions' => static function ($query): void {
$query->select(['world_heritage_site_id', 'short_description_ja']);
},
])
->select([
'world_heritage_sites.id',
Expand Down Expand Up @@ -137,6 +140,7 @@ public function handle(): int
'thumbnail_url' => $row->images->first()?->url,
'state_party_codes' => $statePartyCodes,
'country_names_jp' => $countryCount > 1 ? $countryNamesJp : [],
'short_description_ja' => $row->descriptions->first()?->short_description_ja,
];
}

Expand Down
143 changes: 143 additions & 0 deletions src/app/Console/Commands/TranslateShortDescriptionJapanese.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\WorldHeritageDescription;
use Illuminate\Support\Facades\Http;

class TranslateShortDescriptionJapanese extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'world-heritage:translate-short-description-japanese
{--force : Allow execution outside local environment}
{--dry-run : Skip DB writes and API calls}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle(): int
{
if (!app()->isLocal() && !$this->option('force')) {
$this->error('This command can only be run in local environment. Use --force to override.');
return 1;
}

$jsonPath = storage_path('app/private/unesco/world-heritage-sites.json');

if (!file_exists($jsonPath)) {
$this->error("JSON file not found: {$jsonPath}");
return 1;
}

$original = json_decode(file_get_contents($jsonPath), true);
$sites = $original['results'];
$total = count($sites);
$this->info("Total sites: {$total}");

$bar = $this->output->createProgressBar($total);
$bar->start();

foreach ($sites as $index => $site) {
$idNo = $site['id_no'] ?? null;
$shortDescriptionEn = $site['short_description_en'] ?? null;
$descriptionEn = $site['description_en'] ?? null;

if (!$idNo || !$shortDescriptionEn || !$descriptionEn) {
$bar->advance();
continue;
}

// DBに翻訳済みのものがあればそこから取得してJSONに反映
// if translated columns are existed in DB, fetching from there and reflected on Json File
$record = WorldHeritageDescription::where('world_heritage_site_id', $idNo)
->whereNotNull('short_description_ja')
->whereNotNull('description_ja')
->first();

if ($record) {
$sites[$index]['short_description_ja'] = $record->short_description_ja;
$sites[$index]['description_ja'] = $record->description_ja;
$bar->advance();
continue;
}

if ($this->option('dry-run')) {
$this->line(" [dry-run] Would translate id_no={$idNo}: " . mb_substr($shortDescriptionEn, 0, 50) . '...');
$bar->advance();
continue;
}

$texts = $shortDescriptionEn === $descriptionEn
? [$shortDescriptionEn]
: [$shortDescriptionEn, $descriptionEn];

$key = config('services.google.translate_key');
$params = http_build_query(['key' => $key, 'target' => 'ja', 'format' => 'text']);
foreach ($texts as $t) {
$params .= '&q=' . urlencode($t);
}

$response = Http::get('https://translation.googleapis.com/language/translate/v2?' . $params);

if (!$response->successful()) {
$this->newLine();
$this->error("Google Translate API error for id_no={$idNo}: " . $response->body());
return 1;
}

$translations = $response->json('data.translations');
$shortDescriptionJa = $translations[0]['translatedText'];
$descriptionJa = $shortDescriptionEn === $descriptionEn
? $shortDescriptionJa
: $translations[1]['translatedText'];

WorldHeritageDescription::updateOrCreate(
['world_heritage_site_id' => $idNo],
[
'short_description_en' => $shortDescriptionEn,
'short_description_ja' => $shortDescriptionJa,
'description_en' => $descriptionEn,
'description_ja' => $descriptionJa,
]
);

$sites[$index]['short_description_ja'] = $shortDescriptionJa;
$sites[$index]['description_ja'] = $descriptionJa;

$bar->advance();
usleep(200000);
}

$bar->finish();
$this->newLine();

if (!$this->option('dry-run')) {
$outputDir = storage_path('app/private/unesco/normalized');
$outputPath = $outputDir . '/world_heritage_sites_translation.json';

if (!is_dir($outputDir)) {
mkdir($outputDir, 0755, true);
}

$original['results'] = array_values($sites);
file_put_contents($outputPath, json_encode($original, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$this->info("Translation JSON saved to: {$outputPath}");
}

$this->info('Translation completed successfully.');

return 0;
}
}
10 changes: 10 additions & 0 deletions src/app/Console/Commands/WorldHeritageBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class WorldHeritageBuild extends Command
{--dump-max=0 : 0 means no limit}
{--dump-out=unesco/world-heritage-sites.json : Output path in local disk root (storage/app/private relative)}
{--dump-dry-run : Do not write dump file}

{--translate-jp : Also translate short_description to Japanese}
{--translate-jp-dry-run : Dry run for Japanese translation}

{--jp : Also import Japanese names}
{--jp-path=unesco/world-heritage-japanese-name-sorted.json : Japanese name JSON path in local disk root (storage/app/private relative)}
Expand Down Expand Up @@ -100,6 +103,13 @@ public function handle(): int
'--in' => self::NORM_DIR . '/world_heritage_sites.json',
]);

if ((bool) $this->option('translate-jp')) {
$this->callOrFail('world-heritage:translate-short-description-japanese', array_filter([
'--force' => true,
'--dry-run' => (bool) $this->option('translate-jp-dry-run') ? true : null,
], static fn ($v) => $v !== null));
}

// 4) Import pivot (FK depends on countries + sites)
$this->callOrFail('world-heritage:import-site-state-parties-split', [
'--in' => self::NORM_DIR . '/site_state_parties.json',
Expand Down
2 changes: 1 addition & 1 deletion src/app/Models/WorldHeritage.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function images(): HasMany
->orderBy('sort_order', 'asc');
}

public function worldHeritageDescriptions(): HasMany
public function descriptions(): HasMany
{
return $this->hasMany(WorldHeritageDescription::class, 'world_heritage_site_id', 'id');
}
Expand Down
7 changes: 7 additions & 0 deletions src/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@
],
],

'deepl' => [
'key' => env('DEEPL_API_KEY'),
],

'google' => [
'translate_key' => env('GOOGLE_TRANSLATE_KEY'),
]
];
Loading