Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swagger handlebar localization with i18n #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"trace": "^3.1.1"
},
"dependencies": {
"i18n": "^0.15.1",
"thought-plugin-jsdoc": "2"
},
"scripts": {
Expand Down
16 changes: 16 additions & 0 deletions packages/bootprint-openapi/handlebars/helpers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
const _ = require('lodash')
const highlight = require('highlight.js')
const i18n = require('i18n')
const handlebars = require('handlebars')
const path = require('path')

i18n.configure({
directory: path.join(__dirname, 'i18'), // Path to the directory containing language files
defaultLocale: 'ru', // Default language
queryParameter: 'lang', // Query parameter to specify the language (e.g., ?lang=ru)
cookie: 'language' // Cookie name to store the language preference
})

// Set the default language
i18n.setLocale('en') // todo: get from params

highlight.configure({
useBR: true
})

module.exports = {
i18n: function(key) {
return new handlebars.SafeString(i18n.__(key))
},
'swagger--collection-format': function(value, paramName) {
return {
csv: 'comma separated (`' + paramName + '=aaa,bbb`)',
Expand Down
21 changes: 21 additions & 0 deletions packages/bootprint-openapi/handlebars/i18/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"baseURL": "Base URL",
"version": "Version",
"requestFormat": "Default request format",
"responseFormat": "Default response format",
"requestType": "Request Type:",
"protocolUsedObjects": "Schema definitions",
"scenarios": "Tags",
"descriptionOfParameters": "Parameter definitions",
"messages": "Paths",
"defaultFormat": "Uses default",
"descriptionOfResponse": "Response definitions",
"defaultContentType": "Uses default content-types",
"security": "Security",
"path": "Path",
"operations": "Operations",
"description": "Description",
"summary": "Summary",
"scenario": "Tag",
"operation": "operation"
}
21 changes: 21 additions & 0 deletions packages/bootprint-openapi/handlebars/i18/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"baseURL": "Базовый URL",
"version": "Версия",
"requestFormat": "Формат запросов по умолчанию",
"responseFormat": "Формат ответов по умолчанию",
"requestType": "Тип запросов:",
"protocolUsedObjects": "Используемые в протоколе объекты",
"scenarios": "Сценарии",
"descriptionOfParameters": "Описание параметров",
"messages": "Сообщения",
"defaultFormat": "Формат по умолчанию",
"descriptionOfResponse": "Описание ответа",
"defaultContentType": "Content-type по умолчанию",
"security": "Безопасность",
"path": "Путь",
"operations": "Сообщение",
"description": "Описание",
"summary": "Сводка",
"scenario": "Сценарий",
"operation": "operation"
}
10 changes: 5 additions & 5 deletions packages/bootprint-openapi/handlebars/partials/base/body.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
--}}
<h1>{{info.title}}</h1>
<p class="sw-info">
{{#if basePath}}Base URL: <span class="sw-info-basePath">{{basePath}}</span>,{{/if}}
Version: <span class="sw-info-version">{{info.version}}</span>{{#if externalDocs}}, {{>swagger/externalDocs}}{{/if}}
{{#if basePath}}{{i18n "baseURL"}} <span class="sw-info-basePath">{{basePath}}</span>,{{/if}}
{{i18n "version"}}: <span class="sw-info-version">{{info.version}}</span>{{#if externalDocs}}, {{>swagger/externalDocs}}{{/if}}
</p>
<p>{{md info.description}}</p>

{{#if consumes}}
<div id="sw-default-consumes" class="sw-default-value">
<span class="sw-default-value-header">Default request content-types:</span>
<span class="sw-default-value-header">{{i18n "requestFormat"}} (content-type):</span>
{{#each consumes}}{{.}}{{#unless @last}}, {{/unless}}{{/each}}
</div>
{{/if}}
{{#if produces}}
<div id="sw-default-produces" class="sw-default-value">
<span class="sw-default-value-header">Default response content-types:</span>
<span class="sw-default-value-header">{{i18n "responseFormat"}} (content-type):</span>
{{#each produces}}{{.}}{{#unless @last}}, {{/unless}}{{/each}}
</div>
{{/if}}

<div id="sw-schemes" class="sw-default-value">
<span class="sw-default-value-header">Schemes:</span>
<span class="sw-default-value-header">{{i18n "requestType"}}</span>
{{#each schemes}}{{.}}{{#unless @last}}, {{/unless}}{{/each}}
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--}}

{{#if definitions}}
<h2>Schema definitions</h2>
<h2>{{i18n "protocolUsedObjects"}}</h2>

{{#eachSorted definitions}}
{{>swagger/model model=. title=@key anchor="/definitions" }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div class="operation-summary">{{md summary stripParagraph="true"}}</div>
<h3 class="panel-title"><span class="operation-name">{{toUpperCase method}}</span> <strong>{{path}}</strong></h3>
{{#if tags}}
Tags:
{{i18n "scenarios"}}:
{{#each tags}}
<a href="#tag-{{htmlId .}}">{{.}}</a>{{#unless @last}}, {{/unless}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@api public
--}}
{{#if parameters}}
<h2>Parameter definitions</h2>
<h2>{{i18n "descriptionOfParameters"}}</h2>
<table id="table-parameter-definitions" class="table">
<thead>
<tr>
Expand All @@ -23,4 +23,4 @@
</tbody>
</table>
{{/if}}


Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@params {Path[]} paths a list of Swagger Path-Objects
@api public
--}}
<h2>Paths</h2>
<h2>{{i18n "messages"}}</h2>

{{#eachSorted paths}}
{{>swagger/path pathItems=. path=@key}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{{#if consumes}}
<p>{{>swagger/list-of-labels values=consumes}}</p>
{{else if @root.consumes}}
<p><a href="#sw-default-consumes">Uses default content-types: </a>
<p><a href="#sw-default-consumes">{{i18n "defaultFormat"}} (content-type): </a>
{{>swagger/list-of-labels [email protected]}}
</p>
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Renders the response definitions
--}}
{{#if responses}}
<h2>Response definitions</h2>
<h2>{{i18n "descriptionOfResponse"}}</h2>

<dl id="sw-response-definitions">
{{#each responses}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{{#if produces}}
<p>{{>swagger/list-of-labels values=produces}}</p>
{{else if @root.produces}}
<p><a href="#sw-default-produces">Uses default content-types: </a>
<p><a href="#sw-default-produces">{{i18n "defaultContentType"}}: </a>
{{>swagger/list-of-labels [email protected]}}
</p>
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--}}

{{#if securityDefinitions}}
<h2>Security</h2>
<h2>{{i18n "security"}}</h2>

{{#each securityDefinitions}}
<div id="security-definition-{{@key}}" class="panel panel-security-definition panel-security-definition-{{type}}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
@todo params
@api public
--}}
<h2 id="swagger--summary-no-tags">Summary</h2>
<h2 id="swagger--summary-no-tags">{{i18n "summary"}}</h2>
<table class="table table-bordered table-condensed swagger--summary">
<thead>
<tr>
<th>Path</th>
<th>Operation</th>
<th>Description</th>
<th>{{i18n "path"}}</th>
<th>{{i18n "operation"}}</th>
<th>{{i18n "description"}}</th>
</tr>
</thead>
<tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
@api public
--}}

<h2 id="swagger--summary-tags">Summary</h2>
<h2 id="swagger--summary-tags">{{i18n "summary"}}</h2>
{{#eachSorted tags}}
<h3 id="tag-{{htmlId name}}" class="swagger-summary-tag">Tag: {{name}}</h3>
<h3 id="tag-{{htmlId name}}" class="swagger-summary-tag">{{i18n "scenario"}}: {{name}}</h3>
{{md description}}
<p class="sw-tag-external-doc">
{{>swagger/externalDocs}}
</p>
<table class="table table-bordered table-condensed swagger--summary">
<thead>
<tr>
<th>Operation</th>
<th>Description</th>
<th>{{i18n "operation"}}</th>
<th>{{i18n "description"}}</th>
</tr>
</thead>
<tbody>
Expand Down