diff --git a/lib/generators/jsonapi/swagger/swagger_generator.rb b/lib/generators/jsonapi/swagger/swagger_generator.rb index 43819ca..eefa312 100644 --- a/lib/generators/jsonapi/swagger/swagger_generator.rb +++ b/lib/generators/jsonapi/swagger/swagger_generator.rb @@ -67,8 +67,15 @@ module Jsonapi end end - def safe_encode(comment) - comment&.force_encoding('ASCII-8BIT') + def t(key, options={}) + options[:scope] = :jsonapi_swagger + options[:default] = key.to_s.humanize + content = I18n.t(key, options) + safe_encode(content) + end + + def safe_encode(content) + content&.force_encoding('ASCII-8BIT') end end end \ No newline at end of file diff --git a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb index 2a35820..00eef4f 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb @@ -7,16 +7,16 @@ RSpec.describe '<%= resouces_name %>', type: :request do end path '/<%= route_resouces %>' do - get '<%= route_resouces %>' do + get '<%= route_resouces %> <%= t(:list) %>' do tags '<%= route_resouces %>' produces 'application/vnd.api+json' - parameter name: :'page[number]', in: :query, type: :string, description: '页码', required: false - parameter name: :include, in: :query, type: :string, description: '包含关联数据', required: false - parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '包含字段', required: false + parameter name: :'page[number]', in: :query, type: :string, description: '<%= t(:page_num) %>', required: false + parameter name: :include, in: :query, type: :string, description: '<%= t(:include_related_data) %>', required: false + parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false <% relationships.each_key do |relation| -%> - parameter name: :'fields[<%= relation.to_s.pluralize %>]', in: :query, type: :string, description: '包含字段', required: false + parameter name: :'fields[<%= relation.to_s.pluralize %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false <% end -%> - response '200', '获取列表' do + response '200', '<%= t(:get_list) %>' do schema type: :object, properties: { data: { @@ -28,9 +28,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do links: { type: :object, properties: { - self: { type: :string, description: '详情链接'}, + self: { type: :string, description: '<%= t(:detail_link) %>'}, }, - description: '详情链接' + description: '<%= t(:detail_link) %>' }, attributes: { type: :object, @@ -39,7 +39,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do <%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, <%if columns_with_comment[attr][:is_array] -%> items: { type: :<%= columns_with_comment[attr][:items_type] %>},<% end -%> 'x-nullable': <%= columns_with_comment[attr][:nullable] %>, description: '<%= columns_with_comment[attr][:comment] %>'}, <% end -%> }, - description: '属性' + description: '<%= t(:attributes) %>' }, relationships: { type: :object, @@ -52,38 +52,38 @@ RSpec.describe '<%= resouces_name %>', type: :request do links: { type: :object, properties: { - self: { type: :string, description: '关联<%= relation_name_camelize %>列表链接' }, - related: { type: :string, description: '相关<%= relation_name_camelize %>链接' }, + self: { type: :string, description: '<%= t(:associate_list_link, model: relation_name_camelize) %>' }, + related: { type: :string, description: '<%= t(:related_link, model: relation_name_camelize) %>' }, }, - description: '相关的<%= relation_name_camelize %>链接' + description: '<%= t(:related_link, model: relation_name_camelize)%>' }, }, - description: '相关<%= relation_name_camelize %>' + description: '<%= t(:related_model, model: relation_name_camelize)%>' }, <% end -%> }, - description: '关联数据' + description: '<%= t(:associate_data) %>' } }, }, - description: '数据' + description: '<%= t(:data) %>' }, meta: { type: :object, properties: { - record_count: { type: :integer, description: '总记录数'}, - page_count: { type: :integer, description: '页数'}, + record_count: { type: :integer, description: '<%= t(:record_count) %>'}, + page_count: { type: :integer, description: '<%= t(:page_count) %>'}, }, - description: '分页记录数' + description: '<%= t(:meta) %>' }, links: { type: :object, properties: { - first: { type: :string, description: '第一页'}, - next: { type: :string, description: '下一页'}, - last: { type: :string, description: '最后一页'}, + first: { type: :string, description: '<%= t(:first_page_link) %>'}, + next: { type: :string, description: '<%= t(:next_page_link) %>'}, + last: { type: :string, description: '<%= t(:last_page_link) %>'}, }, - description: '分页链接' }, + description: '<%= t(:page_links) %>' }, }, required: [:data] run_test! @@ -92,16 +92,16 @@ RSpec.describe '<%= resouces_name %>', type: :request do end path '/<%= route_resouces %>/{id}' do - get '<%= route_resouces %>详情' do + get '<%= route_resouces %> <%= t(:detail) %>' do tags '<%= route_resouces %>' produces 'application/vnd.api+json' parameter name: :id, in: :path, type: :integer, description: 'ID', required: true - parameter name: :include, in: :query, type: :string, description: '包含关联数据', required: false - parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '包含字段', required: false + parameter name: :include, in: :query, type: :string, description: '<%= t(:include_related_data) %>', required: false + parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false <% relationships.each_key do |relation| -%> - parameter name: :'fields[<%= relation.to_s.pluralize %>]', in: :query, type: :string, description: '包含字段', required: false + parameter name: :'fields[<%= relation.to_s.pluralize %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false <% end -%> - response '200', '获取详情' do + response '200', '<%= t(:get_detail) %>' do schema type: :object, properties: { data: { @@ -112,9 +112,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do links: { type: :object, properties: { - self: { type: :string, description: '详情链接'}, + self: { type: :string, description: '<%= t(:detail_link) %>'}, }, - description: '详情链接' + description: '<%= t(:detail_link) %>' }, attributes: { type: :object, @@ -123,7 +123,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do <%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, <%if columns_with_comment[attr][:is_array] -%> items: { type: :<%= columns_with_comment[attr][:items_type] %>},<% end -%> 'x-nullable': <%= columns_with_comment[attr][:nullable] %>, description: '<%= columns_with_comment[attr][:comment] %>'}, <% end -%> }, - description: '属性' + description: '<%= t(:attributes) %>' }, relationships: { type: :object, @@ -136,20 +136,20 @@ RSpec.describe '<%= resouces_name %>', type: :request do links: { type: :object, properties: { - self: { type: :string, description: '关联<%= relation_name_camelize %>列表链接' }, - related: { type: :string, description: '相关<%= relation_name_camelize %>链接' }, + self: { type: :string, description: '<%= t(:associate_list_link, model: relation_name_camelize) %>' }, + related: { type: :string, description: '<%= t(:related_link, model: relation_name_camelize )%>' }, }, - description: '相关的<%= relation_name_camelize %>链接' + description: '<%= t(:related_link, model: relation_name_camelize) %>' }, }, - description: '相关<%= relation_name_camelize %>' + description: '<%= t(:related_link, model: relation_name_camelize) %>' }, <% end -%> }, - description: '关联数据' + description: '<%= t(:associate_data) %>' } }, - description: '数据' + description: '<%= t(:data) %>' }, }, required: [:data] diff --git a/lib/i18n/en.yml b/lib/i18n/en.yml new file mode 100644 index 0000000..a446ba0 --- /dev/null +++ b/lib/i18n/en.yml @@ -0,0 +1,24 @@ +en: + jsonapi_swagger: + page_num: 'Page Number' + include_related_data: 'Include Related Data' + display_field: 'Display Field' + list: 'List' + get_list: 'Fetch List' + detail_link: 'Detail Link' + attributes: 'Attributes' + associate_list_link: 'Associate to %{model} Link' + related_link: 'Related %{model} Link' + related_model: 'Related %{model}' + associate_data: 'Associate to Date' + data: 'Data' + record_count: 'Total Record Count' + page_count: 'Page Count' + meta: 'Meta' + first_page_link: 'First Page' + next_page_link: 'Next Page' + last_page_link: 'Last Page' + page_links: 'Page Link' + detail: 'Detail' + get_dtail: 'Fetch Detail' + detail_link: 'Detail Link' \ No newline at end of file diff --git a/lib/i18n/zh-CN.yml b/lib/i18n/zh-CN.yml new file mode 100644 index 0000000..9e9227a --- /dev/null +++ b/lib/i18n/zh-CN.yml @@ -0,0 +1,24 @@ +zh-CN: + jsonapi_swagger: + page_num: '页码' + include_related_data: '包含关联数据' + display_field: '显示字段' + list: '列表' + get_list: '获取列表' + detail_link: '详情链接' + attributes: '属性' + associate_list_link: '关联%{model}链接' + related_link: '相关%{model}链接' + related_model: '相关%{model}' + associate_data: '关联数据' + data: '数据' + record_count: '总记录数' + page_count: '页数' + meta: '元数据' + first_page_link: '第一页链接' + next_page_link: '下一页链接' + last_page_link: '最后一页链接' + page_links: '分页链接' + detail: '详情' + get_dtail: '获取详情' + detail_link: '详情链接' \ No newline at end of file diff --git a/lib/jsonapi/swagger.rb b/lib/jsonapi/swagger.rb index 669ce7e..d7b164f 100644 --- a/lib/jsonapi/swagger.rb +++ b/lib/jsonapi/swagger.rb @@ -1,10 +1,10 @@ # frozen_string_literal: true require 'jsonapi/swagger/version' +require 'jsonapi/swagger/railtie' if defined?(Rails) module Jsonapi module Swagger class Error < StandardError; end - # Your code goes here... end end diff --git a/lib/jsonapi/swagger/railtie.rb b/lib/jsonapi/swagger/railtie.rb new file mode 100644 index 0000000..70ee731 --- /dev/null +++ b/lib/jsonapi/swagger/railtie.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Jsonapi + module Swagger + class Railtie < ::Rails::Railtie + initializer 'jsonapi-swagger-i18n' do |app| + locates = app.config.i18n.available_locales + locates_dir = File.expand_path('../../i18n', __dir__) + locates.each do |loc| + locate_file = File.join(locates_dir, "#{loc}.yml") + I18n.load_path.push(locate_file) if File.exist?(locate_file) && !I18n.load_path.include?(locate_file) + end + end + end + end +end