mirror of
https://github.com/ditkrg/jsonapi-swagger.git
synced 2026-01-22 22:06:50 +00:00
support i18n
This commit is contained in:
parent
68d5eda6ae
commit
00d8e1d6a1
@ -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
|
||||
@ -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]
|
||||
|
||||
24
lib/i18n/en.yml
Normal file
24
lib/i18n/en.yml
Normal file
@ -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'
|
||||
24
lib/i18n/zh-CN.yml
Normal file
24
lib/i18n/zh-CN.yml
Normal file
@ -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: '详情链接'
|
||||
@ -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
|
||||
|
||||
16
lib/jsonapi/swagger/railtie.rb
Normal file
16
lib/jsonapi/swagger/railtie.rb
Normal file
@ -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
|
||||
Loading…
Reference in New Issue
Block a user