Skip to content

Commit

Permalink
[import] Translate attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislav-chynoransky committed Dec 14, 2024
1 parent a255080 commit 5b3cde8
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 17 deletions.
103 changes: 93 additions & 10 deletions app/Importers/UpmImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ class UpmImporter extends AbstractImporter
'dating:sk' => 'Datace',
'date_earliest' => 'Od',
'date_latest' => 'Do',
'work_type:cs' => 'Výtvarný druh',
'work_type:sk' => 'Výtvarný druh',
'object_type:cs' => 'Typ',
'object_type:sk' => 'Typ',
'medium:cs' => 'Materiál',
'medium:sk' => 'Materiál',
'technique:cs' => 'Technika',
'technique:sk' => 'Technika',
'topic:cs' => 'Námět',
'topic:sk' => 'Námět',
'inscription:cs' => 'Značení',
'inscription:sk' => 'Značení',
'related_work:sk' => 'Sbírka',
Expand All @@ -51,11 +41,24 @@ class UpmImporter extends AbstractImporter
'newline' => "\n",
];

private array $workTypeTranslationKeys;
private array $techniqueTranslationKeys;
private array $mediumTranslationKeys;
private array $objectTypeTranslationKeys;
private array $topicTranslationKeys;


protected function init()
{
$this->sanitizers[] = function ($value) {
return empty_to_null(trim($value));
};

$this->workTypeTranslationKeys = array_flip(trans('item.work_types', locale: 'cs'));
$this->techniqueTranslationKeys = array_flip(trans('item.techniques', locale: 'cs'));
$this->mediumTranslationKeys = array_flip(trans('item.media', locale: 'cs'));
$this->objectTypeTranslationKeys = array_flip(trans('item.object_types', locale: 'cs'));
$this->topicTranslationKeys = array_flip(trans('item.topics', locale: 'cs'));
}

protected function getItemId(array $record)
Expand Down Expand Up @@ -98,6 +101,86 @@ protected function hydrateAuthor(array $record): string
->join('; ');
}

protected function hydrateWorkType(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Výtvarný druh'];
}

return str($record['Výtvarný druh'])
->split('/\s*;\s*/')
->map(function (string $workType) use ($locale) {
$key = $this->workTypeTranslationKeys[$workType] ?? null;
return $key ? trans("item.work_types.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydrateTechnique(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Technika'];
}

return str($record['Technika'])
->split('/\s*;\s*/')
->map(function (string $technique) use ($locale) {
$key = $this->techniqueTranslationKeys[$technique] ?? null;
return $key ? trans("item.techniques.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydrateMedium(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Materiál'];
}

return str($record['Materiál'])
->split('/\s*;\s*/')
->map(function (string $medium) use ($locale) {
$key = $this->mediumTranslationKeys[$medium] ?? null;
return $key ? trans("item.media.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydrateObjectType(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Typ'];
}

return str($record['Typ'])
->split('/\s*;\s*/')
->map(function (string $objectType) use ($locale) {
$key = $this->objectTypeTranslationKeys[$objectType] ?? null;
return $key ? trans("item.object_types.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydrateTopic(array $record, string $locale): ?string
{
if ($locale === 'cs') {
return $record['Námět'];
}

return str($record['Námět'])
->split('/\s*;\s*/')
->map(function (string $topic) use ($locale) {
$key = $this->topicTranslationKeys[$topic] ?? null;
return $key ? trans("item.topics.$key", locale: $locale) : null;
})
->filter()
->join('; ') ?: null;
}

protected function hydratePlace(array $record, string $locale): ?string
{
if (!in_array($locale, ['cs', 'sk'])) {
Expand Down
1 change: 1 addition & 0 deletions app/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Item extends Model implements IndexableModel, TranslatableContract
'exhibition',
'box',
'location',
'object_type',
];

public static $rules = array(
Expand Down
4 changes: 4 additions & 0 deletions lang/cs/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,12 @@
'zväčšovanie' => 'zvětšování',
'čiernobiela fotografia' => 'černobílá fotografie',
],
'object_types' => [
'kniha' => 'kniha',
],
'topics' => [
'figurálna kompozícia' => 'figurální kompozice',
'ornament' => 'ornament',
],
'state_editions' => [
'autorizovaný pozitív' => 'autorizovaný pozitiv',
Expand Down
4 changes: 4 additions & 0 deletions lang/en/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,12 @@
'zväčšovanie' => 'enlarging',
'čiernobiela fotografia' => 'black-and-white photograph',
],
'object_types' => [
'kniha' => 'book',
],
'topics' => [
'figurálna kompozícia' => 'figurative composition',
'ornament' => 'ornament',
],
'state_editions' => [
'autorizovaný pozitív' => 'authorized photographic print',
Expand Down
4 changes: 4 additions & 0 deletions lang/sk/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,12 @@
'zväčšovanie' => 'zväčšovanie',
'čiernobiela fotografia' => 'čiernobiela fotografia',
],
'object_types' => [
'kniha' => 'kniha',
],
'topics' => [
'figurálna kompozícia' => 'figurálna kompozícia',
'ornament' => 'ornament',
],
'state_editions' => [
'autorizovaný pozitív' => 'autorizovaný pozitív',
Expand Down
24 changes: 17 additions & 7 deletions tests/Importers/UpmImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,21 @@ public function testImport()
$this->assertEquals('ze souboru', $item->translate('cs')->relationship_type);
$this->assertEquals('collection', $item->translate('en')->relationship_type);
$this->assertEquals('Sbírka užité grafiky', $item->translate('cs')->related_work);
$this->assertEquals('užité umění;grafický design', $item->translate('cs')->work_type); // todo translate
$this->assertEquals('celokožená vazba, zlacení', $item->translate('cs')->technique); // todo translate
$this->assertEquals('kniha', $item->translate('cs')->object_type); // todo translate
$this->assertEquals('kůže;papír', $item->translate('cs')->medium); // todo translate
$this->assertEquals('ornament', $item->translate('cs')->topic); // todo translate
$this->assertEquals('úžitkové umenie; grafický dizajn', $item->translate('sk')->work_type);
$this->assertEquals('užité umění;grafický design', $item->translate('cs')->work_type);
$this->assertEquals('applied arts; graphic design', $item->translate('en')->work_type);
$this->assertEquals('kresba', $item->translate('sk')->technique);
$this->assertEquals('kresba', $item->translate('cs')->technique);
$this->assertEquals('drawing', $item->translate('en')->technique);
$this->assertEquals('kniha', $item->translate('sk')->object_type);
$this->assertEquals('kniha', $item->translate('cs')->object_type);
$this->assertEquals('book', $item->translate('en')->object_type);
$this->assertEquals('papier', $item->translate('sk')->medium);
$this->assertEquals('papír', $item->translate('cs')->medium);
$this->assertEquals('paper', $item->translate('en')->medium);
$this->assertEquals('ornament', $item->translate('sk')->topic);
$this->assertEquals('ornament', $item->translate('cs')->topic);
$this->assertEquals('ornament', $item->translate('en')->topic);
$this->assertEquals('1928', $item->translate('cs')->dating);
$this->assertEquals('Praha', $item->translate('cs')->place);
$this->assertEquals('Rösller', $item->translate('cs')->inscription);
Expand Down Expand Up @@ -96,8 +106,8 @@ private function fakeData(array $overrides = []): array
"Do" => "1928",
"Výtvarný druh" => "užité umění;grafický design",
"Typ" => "kniha",
"Materiál" => "kůže;papír",
"Technika" => "celokožená vazba, zlacení",
"Materiál" => "papír",
"Technika" => "kresba",
"Rozměry" => "v=16,5cm (konvice čajová s víčkem větší); v=13,5-14cm (konvice čajová s víčkem menší)",
"Námět" => "ornament",
"tagy" => "",
Expand Down

0 comments on commit 5b3cde8

Please sign in to comment.