Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Use i18n-tasks to manage translations in API (#11403)

Matthew Kennedy091aa498fb86
diff --git a/api/app/controllers/spree/api/v2/storefront/cart_controller.rb b/api/app/controllers/spree/api/v2/storefront/cart_controller.rb
index b237bd6ef6..1aedb7a0cd 100644
--- a/api/app/controllers/spree/api/v2/storefront/cart_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/cart_controller.rb
@@ -109,7 +109,7 @@ module Spree
coupon_codes = select_coupon_codes
- return render_error_payload(Spree.t('v2.cart.no_coupon_code', scope: 'api')) if coupon_codes.empty?
+ return render_error_payload(I18n.t('spree.api.v2.cart.no_coupon_code')) if coupon_codes.empty?
result_errors = coupon_codes.count > 1 ? select_errors(coupon_codes) : select_error(coupon_codes)
diff --git a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
index 257524f59d..dad22f6466 100644
--- a/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/wishlists_controller.rb
@@ -45,7 +45,7 @@ module Spree
if resource.destroy
head 204
else
- render_error_payload('Something went wrong')
+ render_error_payload(I18n.t('spree.api.v2.wishlist.errors.the_wishlist_could_not_be_destroyed'))
end
end
diff --git a/api/config/i18n-tasks.yml b/api/config/i18n-tasks.yml
new file mode 100644
index 0000000000..76606b535e
--- /dev/null
+++ b/api/config/i18n-tasks.yml
@@ -0,0 +1,40 @@
+base_locale: en
+data:
+ yaml:
+ write:
+ # do not wrap lines at 80 characters
+ line_width: -1
+
+# Find translate calls
+search:
+ paths:
+ - '../api/app'
+ - '../api/lib'
+
+#### For API v1 only, START ####
+ exclude:
+ - '../api/app/controllers/spree/api/v1/*'
+ - '../api/app/views/*'
+
+ignore_unused:
+ - 'spree.api.gateway_error'
+ - 'spree.api.invalid_api_key'
+ - 'spree.api.invalid_resource'
+ - 'spree.api.invalid_taxonomy_id'
+ - 'spree.api.must_specify_api_key'
+ - 'spree.api.negative_quantity'
+ - 'spree.api.order.could_not_transition'
+ - 'spree.api.order.insufficient_quantity'
+ - 'spree.api.order.invalid_shipping_method'
+ - 'spree.api.payment.credit_over_limit'
+ - 'spree.api.payment.update_forbidden'
+ - 'spree.api.shipment.cannot_ready'
+ - 'spree.api.shipment_transfer_errors_occurred'
+ - 'spree.api.shipment_transfer_success'
+ - 'spree.api.stock_location_required'
+ - 'spree.api.unauthorized'
+ - 'spree.api.wrong_shipment_target'
+#### For API v1 only, END ####
+
+# The PatternMapper for Spree.t
+<% I18n::Tasks.add_scanner 'I18n::Tasks::Scanners::PatternMapper', patterns: [['\bSpree\.t[( ]\s*%{key}', 'spree.%{key}']] %>
diff --git a/api/config/locales/en.yml b/api/config/locales/en.yml
index 7eb8f34287..83b94e8b97 100644
--- a/api/config/locales/en.yml
+++ b/api/config/locales/en.yml
@@ -1,45 +1,38 @@
+---
en:
spree:
api:
- must_specify_api_key: "You must specify an API key."
- invalid_api_key: "Invalid API key (%{key}) specified."
- unauthorized: "You are not authorized to perform that action."
- invalid_resource: "Invalid resource. Please fix errors and try again."
- resource_not_found: "The resource you were looking for could not be found."
- gateway_error: "There was a problem with the payment gateway: %{text}"
- access: "API Access"
- key: "Key"
- clear_key: "Clear key"
- regenerate_key: "Regenerate Key"
- no_key: "No key"
- generate_key: "Generate API key"
- key_generated: "Key generated"
- key_cleared: "Key cleared"
+ gateway_error: 'There was a problem with the payment gateway: %{text}'
+ invalid_api_key: Invalid API key (%{key}) specified.
+ invalid_resource: Invalid resource. Please fix errors and try again.
+ invalid_taxonomy_id: Invalid taxonomy id.
+ must_specify_api_key: You must specify an API key.
+ negative_quantity: quantity is negative
order:
- could_not_transition: "The order could not be transitioned. Please fix the errors and try again."
- invalid_shipping_method: "Invalid shipping method specified."
+ could_not_transition: The order could not be transitioned. Please fix the errors and try again.
insufficient_quantity: An item in your cart has become unavailable.
+ invalid_shipping_method: Invalid shipping method specified.
payment:
- credit_over_limit: "This payment can only be credited up to %{limit}. Please specify an amount less than or equal to this number."
- update_forbidden: "This payment cannot be updated because it is %{state}."
+ credit_over_limit: This payment can only be credited up to %{limit}. Please specify an amount less than or equal to this number.
+ update_forbidden: This payment cannot be updated because it is %{state}.
+ resource_not_found: The resource you were looking for could not be found.
shipment:
- cannot_ready: "Cannot ready shipment."
- stock_location_required: "A stock_location_id parameter must be provided in order to retrieve stock movements."
- invalid_taxonomy_id: "Invalid taxonomy id."
- shipment_transfer_errors_occurred: "Following errors occurred while attempting this action:"
- negative_quantity: "quantity is negative"
- wrong_shipment_target: "target shipment is the same as original shipment"
-
+ cannot_ready: Cannot ready shipment.
+ shipment_transfer_errors_occurred: 'Following errors occurred while attempting this action:'
+ shipment_transfer_success: Variants successfully transferred
+ stock_location_required: A stock_location_id parameter must be provided in order to retrieve stock movements.
+ unauthorized: You are not authorized to perform that action.
v2:
cart:
- wrong_quantity: "Quantity has to be greater than 0"
- no_coupon_code: "No coupon code provided and the Order doesn't have any coupon code promotions applied"
- wishlist:
- wrong_quantity: "Quantity has to be greater than 0"
- pass_variant_id: "Pass the variant id that you want to add to this wishlist, eg.{ variant_id: 20 }"
- must_be_an_integer: "Sorry, you must pass an integer value, eg. 5"
+ no_coupon_code: No coupon code provided and the Order doesn't have any coupon code promotions applied
+ wrong_quantity: Quantity has to be greater than 0
cms_sections:
- pass_position_index: "Pass 'new_position_idx' with an interger value in the request body"
+ pass_position_index: Pass 'new_position_idx' with an interger value in the request body
digitals:
- unauthorized: "Error, you are not authorized to access this asset"
- missing_file: "Missing Digital Item: attachment"
+ missing_file: 'Missing Digital Item: attachment'
+ unauthorized: Error, you are not authorized to access this asset
+ wishlist:
+ errors:
+ the_wishlist_could_not_be_destroyed: The wishlist could not be destroyed.
+ wrong_quantity: Quantity has to be greater than 0
+ wrong_shipment_target: target shipment is the same as original shipment
diff --git a/api/spec/i18n_spec.rb b/api/spec/i18n_spec.rb
new file mode 100644
index 0000000000..79583a5030
--- /dev/null
+++ b/api/spec/i18n_spec.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe I18n do
+ let(:i18n) { I18n::Tasks::BaseTask.new }
+ let(:missing_keys) { i18n.missing_keys }
+ let(:unused_keys) { i18n.unused_keys }
+ let(:inconsistent_interpolations) { i18n.inconsistent_interpolations }
+
+ it 'does not have missing keys' do
+ expect(missing_keys).to be_empty,
+ "Missing #{missing_keys.leaves.count} i18n keys, run `i18n-tasks missing' to show them"
+ end
+
+ it 'does not have unused keys' do
+ expect(unused_keys).to be_empty,
+ "#{unused_keys.leaves.count} unused i18n keys, run `i18n-tasks unused' to show them"
+ end
+
+ it 'files are normalized' do
+ # Skip this test if on TRAVIS + MRI.
+ if ENV['TRAVIS'] && RUBY_ENGINE == 'ruby'
+ skip 'Travis CI has an older version of libyaml where the formatting differs.'
+ end
+
+ non_normalized = i18n.non_normalized_paths
+ error_message = "The following files need to be normalized:\n" \
+ "#{non_normalized.map { |path| " #{path}" }.join("\n")}\n" \
+ "Please run `i18n-tasks normalize' to fix"
+ expect(non_normalized).to be_empty, error_message
+ end
+
+ it 'does not have inconsistent interpolations' do
+ error_message = "#{inconsistent_interpolations.leaves.count} i18n keys have inconsistent interpolations.\n" \
+ "Run `i18n-tasks check-consistent-interpolations' to show them"
+ expect(inconsistent_interpolations).to be_empty, error_message
+ end
+end
diff --git a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
index 1adf9a6371..b8a852d496 100644
--- a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
@@ -60,7 +60,7 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
it_behaves_like 'returns 422 HTTP status'
it 'cannot transition to address without a line item' do
- expect(json_response['error']).to include(Spree.t(:there_are_no_items_for_this_order))
+ expect(json_response['error']).to include(I18n.t('spree.there_are_no_items_for_this_order'))
end
end
@@ -85,7 +85,7 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
it_behaves_like 'returns 422 HTTP status'
it 'returns an error' do
- expect(json_response['error']).to include(Spree.t(:no_payment_found))
+ expect(json_response['error']).to include(I18n.t('spree.no_payment_found'))
end
it 'doesnt advance pass payment state' do
@@ -191,7 +191,7 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
it_behaves_like 'returns 422 HTTP status'
it 'returns an error' do
- expect(json_response['error']).to include(Spree.t(:no_payment_found))
+ expect(json_response['error']).to include(I18n.t('spree.no_payment_found'))
end
it 'doesnt completes an order' do
diff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rb
index ec98f89e76..22dbbbc5b3 100644
--- a/api/spec/spec_helper.rb
+++ b/api/spec/spec_helper.rb
@@ -28,6 +28,9 @@ end
require 'rspec/rails'
require 'ffaker'
+require 'i18n/tasks'
+
+
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each { |f| require f }
diff --git a/api/spree_api.gemspec b/api/spree_api.gemspec
index deb1b7ba59..8be4fdb742 100644
--- a/api/spree_api.gemspec
+++ b/api/spree_api.gemspec
@@ -1,36 +1,36 @@
-# -*- encoding: utf-8 -*-
-require_relative '../core/lib/spree/core/version.rb'
+require_relative '../core/lib/spree/core/version'
Gem::Specification.new do |s|
- s.name = "spree_api"
+ s.name = 'spree_api'
s.version = Spree.version
- s.authors = ["Ryan Bigg"]
- s.email = ["[email protected]"]
+ s.authors = ['Ryan Bigg']
+ s.email = ['[email protected]']
s.summary = %q{Spree's API}
s.description = %q{Spree's API}
s.homepage = 'https://spreecommerce.org'
s.license = 'BSD-3-Clause'
s.metadata = {
- "bug_tracker_uri" => "https://github.com/spree/spree/issues",
- "changelog_uri" => "https://github.com/spree/spree/releases/tag/v#{s.version}",
- "documentation_uri" => "https://dev-docs.spreecommerce.org/",
- "source_code_uri" => "https://github.com/spree/spree/tree/v#{s.version}",
+ 'bug_tracker_uri' => 'https://github.com/spree/spree/issues',
+ 'changelog_uri' => "https://github.com/spree/spree/releases/tag/v#{s.version}",
+ 'documentation_uri' => 'https://dev-docs.spreecommerce.org/',
+ 'source_code_uri' => "https://github.com/spree/spree/tree/v#{s.version}",
}
s.required_ruby_version = '>= 2.5'
s.files = `git ls-files`.split($\).reject { |f| f.match(/^spec/) && !f.match(/^spec\/fixtures/) }
- s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
- s.require_paths = ["lib"]
+ s.executables = s.files.grep(%r{^bin/}).map { |f| File.basename(f) }
+ s.require_paths = ['lib']
+ s.add_development_dependency 'i18n-tasks'
s.add_development_dependency 'jsonapi-rspec'
- s.add_development_dependency 'rswag-specs'
s.add_development_dependency 'multi_json'
+ s.add_development_dependency 'rswag-specs'
- s.add_dependency 'spree_core', s.version
- s.add_dependency 'rabl', '~> 0.14', '>= 0.14.2'
- s.add_dependency 'jsonapi-serializer', '~> 2.1'
s.add_dependency 'doorkeeper', '~> 5.3'
+ s.add_dependency 'jsonapi-serializer', '~> 2.1'
+ s.add_dependency 'rabl', '~> 0.14', '>= 0.14.2'
s.add_dependency 'responders'
+ s.add_dependency 'spree_core', s.version
end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 37602b579d..51d7d21eea 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -721,6 +721,7 @@ en:
this_link_takes_you_to_your_stores_home_page: This link takes you to your stores home page.
administration: Administration
add_new_store: Add New Store
+ adjustments_deleted: Adjustments Deleted
advertise: Advertise
agree_to_privacy_policy: Agree to Privacy Policy
agree_to_terms_of_service: Agree to Terms of Service