Skip to content

Commit

Permalink
Make field instructions & validation errors translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Nov 12, 2024
1 parent dbe553f commit a15e47f
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 29 deletions.
26 changes: 25 additions & 1 deletion lang/en/messages.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
<?php

return [
'migrations_needed' => 'In order to keep track of import progress, the importer uses Laravel\'s Job Batching feature. It uses a <code>job_batches</code> table in your database to store information about batches. Before you can run the importer, you will need to run the <code>php artisan migrate</code> command.',
'utility_description' => 'Import entries, taxonomies, and users from XML and CSV files.',

'configuration_instructions' => 'You can add or modify your Blueprint fields to customize what data is imported and what fieldtype it will be stored in. You can save, refresh, and come back to this import config later until it\'s ready to run.',
'destination_collection_instructions' => 'Select the collection to import entries into.',
'destination_site_instructions' => 'Which site should the entries be imported into?',
'destination_taxonomy_instructions' => 'Select the taxonomy to import terms into.',
'destination_type_instructions' => 'Choose what type of data are you importing.',
'import_file_instructions' => 'Upload a CSV or XML file to import. This will replace the current file.',
'import_file_instructions_create' => 'Upload a CSV or XML file to import.',
'import_name_instructions' => 'Name this import so you can identify it later.',
'mapping_instructions' => 'Map the fields from your import to the fields in your blueprint.',
'migrations_needed' => 'In order to keep track of import progress, the importer uses Laravel\'s Job Batching feature. It uses a <code>job_batches</code> table in your database to store information about batches. Before you can run the importer, you will need to run the <code>php artisan migrate</code> command.',
'strategy_instructions' => 'Choose what should happen when importing.',
'unique_field_instructions' => 'Select a "unique field" to determine if an item already exists.',

'assets_base_url_instructions' => 'The base URL to prepend to the path.',
'assets_download_when_missing_instructions' => 'If the asset can\'t be found in the asset container, should it be downloaded?',
'assets_related_field_instructions' => 'Which field does the data reference?',
'entries_create_when_missing_instructions' => 'Create the entry if it doesn\'t exist.',
'entries_related_field_instructions' => 'Which field does the data reference?',
'terms_create_when_missing_instructions' => 'Create the term if it doesn\'t exist.',
'terms_related_field_instructions' => 'Which field does the data reference?',
'toggle_format_instructions' => 'How is the value stored?',
'toggle_values_instructions' => 'Specify the values that represent true and false in your data. You may separate multiple values with a pipe (`|`).',
'users_create_when_missing_instructions' => 'Create the user if it doesn\'t exist.',
'users_related_field_instructions' => 'Which field does the data reference?',

'csv_delimiter_instructions' => 'Specify the delimiter to be used when reading the CSV file. You will need to save the import for the options to be updated.',
];
9 changes: 9 additions & 0 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
'file_type_not_allowed' => 'Only CSV and XML files can be imported at this time.',
'mappings_not_provided' => 'You must map at least one field.',
'site_not_configured_in_collection' => 'The chosen collection is not available on this site.',
'unique_field_without_mapping' => 'Please configure a mapping for this field.',
'uploaded_file_not_found' => 'The uploaded file could not be found.',
];
2 changes: 1 addition & 1 deletion src/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private function createBlueprint(): Blueprint

$blueprint->ensureFieldHasConfig('file', [
'display' => __('File'),
'instructions' => __('Upload a CSV or XML file to import.'),
'instructions' => __('importer::messages.import_file_instructions_create'),
'required' => true,
]);

Expand Down
24 changes: 12 additions & 12 deletions src/Imports/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function getBlueprint(?Import $import = null): \Statamic\Fields\Bl
'field' => [
'type' => 'text',
'display' => __('Name'),
'instructions' => __('Name this import so you can identify it later.'),
'instructions' => __('importer::messages.import_name_instructions'),
'validate' => 'required',
],
],
Expand All @@ -43,7 +43,7 @@ public static function getBlueprint(?Import $import = null): \Statamic\Fields\Bl
'field' => [
'type' => 'files',
'display' => __('Upload a new file'),
'instructions' => __('Upload a CSV or XML file to import. This will replace the current file.'),
'instructions' => __('importer::messages.import_file_instructions'),
'max_files' => 1,
'allowed_extensions' => ['csv', 'xml'],
'validate' => [
Expand All @@ -57,11 +57,11 @@ function (string $attribute, mixed $value, Closure $fail) {
$path = "statamic/file-uploads/{$value[0]}";

if (! Storage::disk('local')->exists($path)) {
$fail('The uploaded file could not be found.')->translate();
$fail('importer::validation.file_type_not_allowed')->translate();
}

if (! in_array(Storage::disk('local')->mimeType($path), static::$allowedMimeTypes)) {
$fail('Only CSV and XML files can be imported at this time.')->translate();
$fail('importer::validation.uploaded_file_not_found')->translate();
}
},
],
Expand All @@ -80,7 +80,7 @@ function (string $attribute, mixed $value, Closure $fail) {
'field' => [
'type' => 'button_group',
'display' => __('Data Type'),
'instructions' => __('Choose what type of data are you importing.'),
'instructions' => __('importer::messages.destination_type_instructions'),
'width' => 50,
'options' => [
['key' => 'entries', 'value' => __('Entries')],
Expand All @@ -95,7 +95,7 @@ function (string $attribute, mixed $value, Closure $fail) {
'field' => [
'type' => 'collections',
'display' => __('Collection'),
'instructions' => __('Select the collection to import entries into.'),
'instructions' => __('importer::messages.destination_collection_instructions'),
'width' => 50,
'max_items' => 1,
'mode' => 'select',
Expand All @@ -108,7 +108,7 @@ function (string $attribute, mixed $value, Closure $fail) {
'field' => [
'type' => 'taxonomies',
'display' => __('Taxonomy'),
'instructions' => __('Select the taxonomy to import terms into.'),
'instructions' => __('importer::messages.destination_taxonomy_instructions'),
'width' => 50,
'max_items' => 1,
'mode' => 'select',
Expand All @@ -121,7 +121,7 @@ function (string $attribute, mixed $value, Closure $fail) {
'field' => [
'type' => 'sites',
'display' => __('Site'),
'instructions' => __('Which site should the entries be imported into?'),
'instructions' => __('importer::messages.destination_site_instructions'),
'width' => 50,
'max_items' => 1,
'mode' => 'select',
Expand All @@ -132,7 +132,7 @@ function (string $attribute, mixed $value, Closure $fail) {
$collection = Collection::find(Arr::get(request()->destination, 'collection.0'));

if (count($value) && ! $collection->sites()->contains($value[0])) {
$fail('The chosen collection is not available on this site.')->translate();
$fail('importer::validation.site_not_configured_in_collection')->translate();
}
},
],
Expand All @@ -146,7 +146,7 @@ function (string $attribute, mixed $value, Closure $fail) {
'field' => [
'type' => 'checkboxes',
'display' => __('Import Strategy'),
'instructions' => __('Choose what should happen when importing.'),
'instructions' => __('importer::messages.strategy_instructions'),
'options' => [
['key' => 'create', 'value' => __('Create new items')],
['key' => 'update', 'value' => __('Update existing items')],
Expand All @@ -173,7 +173,7 @@ function (string $attribute, mixed $value, Closure $fail) {
'array',
function (string $attribute, mixed $value, Closure $fail) {
if (collect($value)->reject(fn (array $mapping) => empty($mapping['key']))->isEmpty()) {
$fail('You must map at least one field.')->translate();
$fail('importer::validation.mappings_not_provided')->translate();
}
},
],
Expand All @@ -194,7 +194,7 @@ function (string $attribute, mixed $value, Closure $fail) {
'required',
function (string $attribute, mixed $value, Closure $fail) {
if (! collect(request()->mappings)->reject(fn ($mapping) => empty($mapping['key']))->has($value)) {
$fail('Please configure a mapping for this field.')->translate();
$fail('importer::validation.unique_field_without_mapping')->translate();
}
},
],
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function bootAddon()
Utility::extend(function () {
Utility::register('importer')
->title(__('Importer'))
->description(__('Import entries, taxonomies, and users from XML and CSV files.'))
->description(__('importer::messages.utility_description'))
->icon('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m4.5 8.5 9.5 0" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path><path d="m4.5 11.5 6 0" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path><path d="m4.5 5.5 7 0" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path><path d="m4.5 14.5 4 0" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path><path d="m4.5 17.5 4 0" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path><path d="M10.5 23.5h-9a1 1 0 0 1 -1 -1v-21a1 1 0 0 1 1 -1h13.293a1 1 0 0 1 0.707 0.293L19.207 4.5a1 1 0 0 1 0.293 0.707V8.5" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path><path d="M11.5 17.5a6 6 0 1 0 12 0 6 6 0 1 0 -12 0" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path><path d="m17.5 14.5 0 6" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path><path d="m17.5 20.5 -2.25 -2.25" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path><path d="m17.5 20.5 2.25 -2.25" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1"></path></svg>')
->action([ImportController::class, 'index'])
->routes(function ($router) {
Expand Down
2 changes: 1 addition & 1 deletion src/Sources/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function fieldItems(): array
return [
'csv_delimiter' => [
'display' => __('CSV Delimiter'),
'instructions' => __('Specify the delimiter to be used when reading the CSV file. You will need to save the import for the options to be updated.'),
'instructions' => __('importer::messages.csv_delimiter_instructions'),
'type' => 'text',
'default' => ',',
],
Expand Down
6 changes: 3 additions & 3 deletions src/Transformers/AssetsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function fieldItems(): array
'related_field' => [
'type' => 'select',
'display' => __('Related Field'),
'instructions' => __('Which field does the data reference?'),
'instructions' => __('importer::messages.assets_related_field_instructions'),
'default' => 'url',
'options' => [
['key' => 'path', 'value' => __('Path')],
Expand All @@ -66,13 +66,13 @@ public function fieldItems(): array
'base_url' => [
'type' => 'text',
'display' => __('Base URL'),
'instructions' => __('The base URL to prepend to the path.'),
'instructions' => __('importer::messages.assets_base_url_instructions'),
'if' => ['related_field' => 'url'],
],
'download_when_missing' => [
'type' => 'toggle',
'display' => __('Download when missing?'),
'instructions' => __("If the asset can't be found in the asset container, should it be downloaded?"),
'instructions' => __("importer::messages.assets_download_when_missing_instructions"),
'if' => ['related_field' => 'url'],
],
];
Expand Down
4 changes: 2 additions & 2 deletions src/Transformers/BardTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public function fieldItems(): array
'assets_base_url' => [
'type' => 'text',
'display' => __('Assets Base URL'),
'instructions' => __('The base URL to prepend to the path.'),
'instructions' => __('importer::messages.assets_base_url_instructions'),
],
'assets_download_when_missing' => [
'type' => 'toggle',
'display' => __('Download assets when missing?'),
'instructions' => __("If the asset can't be found in the asset container, should it be downloaded?"),
'instructions' => __("importer::messages.assets_download_when_missing_instructions"),
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Transformers/EntriesTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function fieldItems(): array
'related_field' => [
'type' => 'select',
'display' => __('Related Field'),
'instructions' => __('Which field does the data reference?'),
'instructions' => __('importer::messages.entries_related_field_instructions'),
'default' => 'id',
'options' => $fields
->map(fn ($field) => ['key' => $field->handle(), 'value' => $field->display()])
Expand All @@ -92,7 +92,7 @@ public function fieldItems(): array
'create_when_missing' => [
'type' => 'toggle',
'display' => __('Create entry when missing?'),
'instructions' => __("Create the entry if it doesn't exist."),
'instructions' => __("importer::messages.entries_create_when_missing_instructions"),
'default' => false,
],
];
Expand Down
4 changes: 2 additions & 2 deletions src/Transformers/TermsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function fieldItems(): array
'related_field' => [
'type' => 'select',
'display' => __('Related Field'),
'instructions' => __('Which field does the data reference?'),
'instructions' => __('importer::messages.terms_related_field_instructions'),
'default' => 'id',
'options' => $fields
->map(fn ($field) => ['key' => $field->handle(), 'value' => $field->display()])
Expand All @@ -67,7 +67,7 @@ public function fieldItems(): array
'create_when_missing' => [
'type' => 'toggle',
'display' => __('Create term when missing?'),
'instructions' => __("Create the term if it doesn't exist."),
'instructions' => __("importer::messages.terms_create_when_missing_instructions"),
'default' => false,
],
];
Expand Down
4 changes: 2 additions & 2 deletions src/Transformers/ToggleTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function fieldItems(): array
'format' => [
'type' => 'select',
'display' => __('Format'),
'instructions' => __('How is the value stored?'),
'instructions' => __('importer::messages.toggle_format_instructions'),
'options' => [
'boolean' => Statamic::trans('Booleans'),
'string' => Statamic::trans('Strings'),
Expand All @@ -39,7 +39,7 @@ public function fieldItems(): array
'values' => [
'type' => 'array',
'display' => __('Values'),
'instructions' => __('Specify the values that represent true and false in your data. You may separate multiple values with a pipe (`|`).'),
'instructions' => __('importer::messages.toggle_values_instructions'),
'mode' => 'keyed',
'keys' => [
['key' => 'true', 'value' => __('True')],
Expand Down
Loading

0 comments on commit a15e47f

Please sign in to comment.