From 77c3f127908ec62571337f4978b898df2fa39d75 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Tue, 12 Nov 2024 19:58:16 +0000 Subject: [PATCH] Translations (#33) * Fix issue when you have `strings.php` lang file * Make field instructions & validation errors translatable * Fix styling --------- Co-authored-by: duncanmcclean --- lang/en/messages.php | 26 ++++++++++++++++++++++- lang/en/validation.php | 9 ++++++++ src/Http/Controllers/ImportController.php | 2 +- src/Imports/Blueprint.php | 24 ++++++++++----------- src/ServiceProvider.php | 2 +- src/Sources/Csv.php | 2 +- src/Transformers/AssetsTransformer.php | 6 +++--- src/Transformers/BardTransformer.php | 4 ++-- src/Transformers/EntriesTransformer.php | 4 ++-- src/Transformers/TermsTransformer.php | 4 ++-- src/Transformers/ToggleTransformer.php | 10 +++++---- src/Transformers/UsersTransformer.php | 4 ++-- 12 files changed, 66 insertions(+), 31 deletions(-) create mode 100644 lang/en/validation.php diff --git a/lang/en/messages.php b/lang/en/messages.php index 4b7a66b..d0b0ecf 100644 --- a/lang/en/messages.php +++ b/lang/en/messages.php @@ -1,8 +1,32 @@ 'In order to keep track of import progress, the importer uses Laravel\'s Job Batching feature. It uses a job_batches table in your database to store information about batches. Before you can run the importer, you will need to run the php artisan migrate 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 job_batches table in your database to store information about batches. Before you can run the importer, you will need to run the php artisan migrate 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.', ]; diff --git a/lang/en/validation.php b/lang/en/validation.php new file mode 100644 index 0000000..c35a529 --- /dev/null +++ b/lang/en/validation.php @@ -0,0 +1,9 @@ + '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.', +]; diff --git a/src/Http/Controllers/ImportController.php b/src/Http/Controllers/ImportController.php index dd39b82..869ee8e 100644 --- a/src/Http/Controllers/ImportController.php +++ b/src/Http/Controllers/ImportController.php @@ -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, ]); diff --git a/src/Imports/Blueprint.php b/src/Imports/Blueprint.php index 44dc4ee..de86bd8 100644 --- a/src/Imports/Blueprint.php +++ b/src/Imports/Blueprint.php @@ -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', ], ], @@ -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' => [ @@ -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(); } }, ], @@ -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')], @@ -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', @@ -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', @@ -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', @@ -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(); } }, ], @@ -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')], @@ -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(); } }, ], @@ -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(); } }, ], diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index e136967..9ef9107 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -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('') ->action([ImportController::class, 'index']) ->routes(function ($router) { diff --git a/src/Sources/Csv.php b/src/Sources/Csv.php index eb0861b..85469a5 100644 --- a/src/Sources/Csv.php +++ b/src/Sources/Csv.php @@ -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' => ',', ], diff --git a/src/Transformers/AssetsTransformer.php b/src/Transformers/AssetsTransformer.php index 58b72e0..196d693 100644 --- a/src/Transformers/AssetsTransformer.php +++ b/src/Transformers/AssetsTransformer.php @@ -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')], @@ -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'], ], ]; diff --git a/src/Transformers/BardTransformer.php b/src/Transformers/BardTransformer.php index fac7d79..4835dd1 100644 --- a/src/Transformers/BardTransformer.php +++ b/src/Transformers/BardTransformer.php @@ -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'), ], ]; } diff --git a/src/Transformers/EntriesTransformer.php b/src/Transformers/EntriesTransformer.php index cfd384d..623f543 100644 --- a/src/Transformers/EntriesTransformer.php +++ b/src/Transformers/EntriesTransformer.php @@ -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()]) @@ -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, ], ]; diff --git a/src/Transformers/TermsTransformer.php b/src/Transformers/TermsTransformer.php index 8e54855..7bcab3c 100644 --- a/src/Transformers/TermsTransformer.php +++ b/src/Transformers/TermsTransformer.php @@ -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()]) @@ -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, ], ]; diff --git a/src/Transformers/ToggleTransformer.php b/src/Transformers/ToggleTransformer.php index 4bb963b..9c74ac1 100644 --- a/src/Transformers/ToggleTransformer.php +++ b/src/Transformers/ToggleTransformer.php @@ -2,6 +2,8 @@ namespace Statamic\Importer\Transformers; +use Statamic\Statamic; + class ToggleTransformer extends AbstractTransformer { public function transform(string $value): bool @@ -27,17 +29,17 @@ public function fieldItems(): array 'format' => [ 'type' => 'select', 'display' => __('Format'), - 'instructions' => __('How is the value stored?'), + 'instructions' => __('importer::messages.toggle_format_instructions'), 'options' => [ - 'boolean' => __('Booleans'), - 'string' => __('Strings'), + 'boolean' => Statamic::trans('Booleans'), + 'string' => Statamic::trans('Strings'), ], 'validate' => 'required', ], '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')], diff --git a/src/Transformers/UsersTransformer.php b/src/Transformers/UsersTransformer.php index f6cf1e1..0b19548 100644 --- a/src/Transformers/UsersTransformer.php +++ b/src/Transformers/UsersTransformer.php @@ -57,7 +57,7 @@ public function fieldItems(): array 'related_field' => [ 'type' => 'select', 'display' => __('Related Field'), - 'instructions' => __('Which field does the data reference?'), + 'instructions' => __('importer::messages.users_related_field_instructions'), 'default' => 'id', 'options' => User::blueprint() ->fields() @@ -71,7 +71,7 @@ public function fieldItems(): array 'create_when_missing' => [ 'type' => 'toggle', 'display' => __('Create user when missing?'), - 'instructions' => __("Create the user if it doesn't exist."), + 'instructions' => __('importer::messages.users_create_when_missing_instructions'), 'default' => false, 'unless' => ['related_field' => 'name'], ],