From 3ec9cc33c0c9652275cb61299b31db62f92c0b8a Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Wed, 28 Aug 2024 13:31:49 +0800 Subject: [PATCH 1/6] feat: install page support i18n --- ui/src/i18n/init.ts | 60 +++++++++++++++++-- .../Install/components/FirstStep/index.tsx | 3 +- ui/src/pages/Install/index.tsx | 3 +- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/ui/src/i18n/init.ts b/ui/src/i18n/init.ts index 861889003..59ffaf46a 100644 --- a/ui/src/i18n/init.ts +++ b/ui/src/i18n/init.ts @@ -21,7 +21,20 @@ import { initReactI18next } from 'react-i18next'; import i18next from 'i18next'; import en_US from '@i18n/en_US.yaml'; -// import zh_CN from '@i18n/zh_CN.yaml'; +import es_ES from '@i18n/es_ES.yaml'; +import pt_BR from '@i18n/pt_BR.yaml'; +import pt_PT from '@i18n/pt_PT.yaml'; +import de_DE from '@i18n/de_DE.yaml'; +import fr_FR from '@i18n/fr_FR.yaml'; +import ja_JP from '@i18n/ja_JP.yaml'; +import it_IT from '@i18n/it_IT.yaml'; +import ru_RU from '@i18n/ru_RU.yaml'; +import zh_CN from '@i18n/zh_CN.yaml'; +import zh_TW from '@i18n/zh_TW.yaml'; +import ko_KR from '@i18n/ko_KR.yaml'; +import vi_VN from '@i18n/vi_VN.yaml'; +import sk_SK from '@i18n/sk_SK.yaml'; +import fa_IR from '@i18n/fa_IR.yaml'; import { DEFAULT_LANG, LANG_RESOURCE_STORAGE_KEY } from '@/common/constants'; import Storage from '@/utils/storage'; @@ -34,9 +47,48 @@ const initResources = { en_US: { translation: en_US.ui, }, - // zh_CN: { - // translation: zh_CN.ui, - // }, + es_ES: { + translation: es_ES.ui, + }, + pt_BR: { + translation: pt_BR.ui, + }, + pt_PT: { + translation: pt_PT.ui, + }, + de_DE: { + translation: de_DE.ui, + }, + fr_FR: { + translation: fr_FR.ui, + }, + ja_JP: { + translation: ja_JP.ui, + }, + it_IT: { + translation: it_IT.ui, + }, + ru_RU: { + translation: ru_RU.ui, + }, + zh_CN: { + translation: zh_CN.ui, + }, + zh_TW: { + translation: zh_TW.ui, + }, + ko_KR: { + translation: ko_KR.ui, + }, + vi_VN: { + translation: vi_VN.ui, + }, + sk_SK: { + translation: sk_SK.ui, + }, + fa_IR: { + translation: fa_IR.ui, + }, }; const storageLang = Storage.get(LANG_RESOURCE_STORAGE_KEY); diff --git a/ui/src/pages/Install/components/FirstStep/index.tsx b/ui/src/pages/Install/components/FirstStep/index.tsx index 3f3f423f2..547e91c72 100644 --- a/ui/src/pages/Install/components/FirstStep/index.tsx +++ b/ui/src/pages/Install/components/FirstStep/index.tsx @@ -32,7 +32,7 @@ interface Props { visible: boolean; } const Index: FC = ({ visible, data, changeCallback, nextCallback }) => { - const { t } = useTranslation('translation', { keyPrefix: 'install' }); + const { t, i18n } = useTranslation('translation', { keyPrefix: 'install' }); const [langs, setLangs] = useState(); @@ -65,6 +65,7 @@ const Index: FC = ({ visible, data, changeCallback, nextCallback }) => { value={data.value} isInvalid={data.isInvalid} onChange={(e) => { + i18n.changeLanguage(e.target.value); changeCallback({ lang: { value: e.target.value, diff --git a/ui/src/pages/Install/index.tsx b/ui/src/pages/Install/index.tsx index 5c8f5c842..c34a6e3dc 100644 --- a/ui/src/pages/Install/index.tsx +++ b/ui/src/pages/Install/index.tsx @@ -47,7 +47,7 @@ import { } from './components'; const Index: FC = () => { - const { t } = useTranslation('translation', { keyPrefix: 'install' }); + const { t, i18n } = useTranslation('translation', { keyPrefix: 'install' }); const [step, setStep] = useState(1); const [loading, setLoading] = useState(true); const [errorData, setErrorData] = useState<{ [propName: string]: any }>({ @@ -293,6 +293,7 @@ const Index: FC = () => { useEffect(() => { configYmlCheck(); + i18n.changeLanguage(Storage.get(CURRENT_LANG_STORAGE_KEY)); }, []); if (loading) { From c751364974a98f1a1f0a70bc853432a659e59451 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Wed, 4 Sep 2024 09:10:06 +0800 Subject: [PATCH 2/6] fix: add getInstallLanguageConfig --- cmd/answer/main.go | 38 ------------- docs/docs.go | 29 ++++++++++ docs/swagger.json | 29 ++++++++++ docs/swagger.yaml | 19 +++++++ internal/install/install_controller.go | 14 +++++ internal/install/install_server.go | 1 + internal/service/mock/siteinfo_repo_mock.go | 19 ------- ui/src/i18n/init.ts | 56 ------------------- .../Install/components/FirstStep/index.tsx | 5 +- ui/src/pages/Install/index.tsx | 5 +- ui/src/services/install/index.ts | 4 ++ ui/src/utils/localize.ts | 17 ++++++ 12 files changed, 119 insertions(+), 117 deletions(-) delete mode 100644 cmd/answer/main.go diff --git a/cmd/answer/main.go b/cmd/answer/main.go deleted file mode 100644 index fb91f5942..000000000 --- a/cmd/answer/main.go +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -//go:generate go run github.com/swaggo/swag/cmd/swag init -g ./cmd/answer/main.go -d ../../ -o ../../docs - -package main - -import ( - answercmd "github.com/apache/incubator-answer/cmd" -) - -// main godoc -// @title "apache answer" -// @description = "apache answer api" -// @version = "v0.0.1" -// @BasePath = "/" -// @securityDefinitions.apikey ApiKeyAuth -// @in header -// @name Authorization -func main() { - answercmd.Main() -} diff --git a/docs/docs.go b/docs/docs.go index 7f5bd8718..32942b7d1 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -7226,6 +7226,35 @@ const docTemplate = `{ } } }, + "/installation/language/config": { + "get": { + "description": "get installation language config mapping", + "produces": [ + "application/json" + ], + "tags": [ + "Lang" + ], + "summary": "get installation language config mapping", + "parameters": [ + { + "type": "string", + "description": "Accept-Language", + "name": "Accept-Language", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.RespBody" + } + } + } + } + }, "/installation/language/options": { "get": { "description": "get installation language options", diff --git a/docs/swagger.json b/docs/swagger.json index 093e0dfcc..3aa86c6b0 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -7200,6 +7200,35 @@ } } }, + "/installation/language/config": { + "get": { + "description": "get installation language config mapping", + "produces": [ + "application/json" + ], + "tags": [ + "Lang" + ], + "summary": "get installation language config mapping", + "parameters": [ + { + "type": "string", + "description": "Accept-Language", + "name": "Accept-Language", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.RespBody" + } + } + } + } + }, "/installation/language/options": { "get": { "description": "get installation language options", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 6d96c8a17..4188b6c0b 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -7239,6 +7239,25 @@ paths: summary: init environment tags: - installation + /installation/language/config: + get: + description: get installation language config mapping + parameters: + - description: Accept-Language + in: header + name: Accept-Language + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/handler.RespBody' + summary: get installation language config mapping + tags: + - Lang /installation/language/options: get: description: get installation language options diff --git a/internal/install/install_controller.go b/internal/install/install_controller.go index 7f57efbf1..05f7daa67 100644 --- a/internal/install/install_controller.go +++ b/internal/install/install_controller.go @@ -20,6 +20,7 @@ package install import ( + "encoding/json" "net/http" "os" "path/filepath" @@ -37,6 +38,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/copier" "github.com/segmentfault/pacman/errors" + "github.com/segmentfault/pacman/i18n" "github.com/segmentfault/pacman/log" ) @@ -51,6 +53,18 @@ func LangOptions(ctx *gin.Context) { handler.HandleResponse(ctx, nil, translator.LanguageOptions) } +// GetLangMapping get installation language config mapping +// @Summary get installation language config mapping +// @Description get installation language config mapping +// @Tags Lang +// @Param Accept-Language header string true "Accept-Language" +// @Produce json +// @Success 200 {object} handler.RespBody{} +// @Router /installation/language/config [get] +func GetLangMapping(ctx *gin.Context) { + // TODO +} + // CheckConfigFileAndRedirectToInstallPage if config file not exist try to redirect to install page // @Summary if config file not exist try to redirect to install page // @Description if config file not exist try to redirect to install page diff --git a/internal/install/install_server.go b/internal/install/install_server.go index f6087fea6..76a2fffe3 100644 --- a/internal/install/install_server.go +++ b/internal/install/install_server.go @@ -64,6 +64,7 @@ func NewInstallHTTPServer() *gin.Engine { installApi.GET(c.UI.BaseURL+"/", CheckConfigFileAndRedirectToInstallPage) installApi.GET(c.UI.BaseURL+"/install", WebPage) installApi.GET(c.UI.BaseURL+"/50x", WebPage) + installApi.GET("/installation/language/config", GetLangMapping) installApi.GET("/installation/language/options", LangOptions) installApi.POST("/installation/db/check", CheckDatabase) installApi.POST("/installation/config-file/check", CheckConfigFile) diff --git a/internal/service/mock/siteinfo_repo_mock.go b/internal/service/mock/siteinfo_repo_mock.go index abc1fe072..cdc72b801 100644 --- a/internal/service/mock/siteinfo_repo_mock.go +++ b/internal/service/mock/siteinfo_repo_mock.go @@ -1,22 +1,3 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - // Code generated by MockGen. DO NOT EDIT. // Source: ./siteinfo_service.go diff --git a/ui/src/i18n/init.ts b/ui/src/i18n/init.ts index 59ffaf46a..919335cba 100644 --- a/ui/src/i18n/init.ts +++ b/ui/src/i18n/init.ts @@ -21,20 +21,6 @@ import { initReactI18next } from 'react-i18next'; import i18next from 'i18next'; import en_US from '@i18n/en_US.yaml'; -import es_ES from '@i18n/es_ES.yaml'; -import pt_BR from '@i18n/pt_BR.yaml'; -import pt_PT from '@i18n/pt_PT.yaml'; -import de_DE from '@i18n/de_DE.yaml'; -import fr_FR from '@i18n/fr_FR.yaml'; -import ja_JP from '@i18n/ja_JP.yaml'; -import it_IT from '@i18n/it_IT.yaml'; -import ru_RU from '@i18n/ru_RU.yaml'; -import zh_CN from '@i18n/zh_CN.yaml'; -import zh_TW from '@i18n/zh_TW.yaml'; -import ko_KR from '@i18n/ko_KR.yaml'; -import vi_VN from '@i18n/vi_VN.yaml'; -import sk_SK from '@i18n/sk_SK.yaml'; -import fa_IR from '@i18n/fa_IR.yaml'; import { DEFAULT_LANG, LANG_RESOURCE_STORAGE_KEY } from '@/common/constants'; import Storage from '@/utils/storage'; @@ -47,48 +33,6 @@ const initResources = { en_US: { translation: en_US.ui, }, - es_ES: { - translation: es_ES.ui, - }, - pt_BR: { - translation: pt_BR.ui, - }, - pt_PT: { - translation: pt_PT.ui, - }, - de_DE: { - translation: de_DE.ui, - }, - fr_FR: { - translation: fr_FR.ui, - }, - ja_JP: { - translation: ja_JP.ui, - }, - it_IT: { - translation: it_IT.ui, - }, - ru_RU: { - translation: ru_RU.ui, - }, - zh_CN: { - translation: zh_CN.ui, - }, - zh_TW: { - translation: zh_TW.ui, - }, - ko_KR: { - translation: ko_KR.ui, - }, - vi_VN: { - translation: vi_VN.ui, - }, - sk_SK: { - translation: sk_SK.ui, - }, - fa_IR: { - translation: fa_IR.ui, - }, }; const storageLang = Storage.get(LANG_RESOURCE_STORAGE_KEY); diff --git a/ui/src/pages/Install/components/FirstStep/index.tsx b/ui/src/pages/Install/components/FirstStep/index.tsx index 547e91c72..cb79fdfb8 100644 --- a/ui/src/pages/Install/components/FirstStep/index.tsx +++ b/ui/src/pages/Install/components/FirstStep/index.tsx @@ -24,6 +24,7 @@ import { useTranslation } from 'react-i18next'; import type { LangsType, FormValue, FormDataType } from '@/common/interface'; import Progress from '../Progress'; import { getInstallLangOptions } from '@/services'; +import { setupInstallLanguage } from '@/utils/localize'; interface Props { data: FormValue; @@ -32,7 +33,7 @@ interface Props { visible: boolean; } const Index: FC = ({ visible, data, changeCallback, nextCallback }) => { - const { t, i18n } = useTranslation('translation', { keyPrefix: 'install' }); + const { t } = useTranslation('translation', { keyPrefix: 'install' }); const [langs, setLangs] = useState(); @@ -65,7 +66,7 @@ const Index: FC = ({ visible, data, changeCallback, nextCallback }) => { value={data.value} isInvalid={data.isInvalid} onChange={(e) => { - i18n.changeLanguage(e.target.value); + setupInstallLanguage(e.target.value); changeCallback({ lang: { value: e.target.value, diff --git a/ui/src/pages/Install/index.tsx b/ui/src/pages/Install/index.tsx index c34a6e3dc..4b43889ad 100644 --- a/ui/src/pages/Install/index.tsx +++ b/ui/src/pages/Install/index.tsx @@ -37,6 +37,7 @@ import { } from '@/utils'; import { CURRENT_LANG_STORAGE_KEY } from '@/common/constants'; import { BASE_ORIGIN } from '@/router/alias'; +import { setupInstallLanguage } from '@/utils/localize'; import { FirstStep, @@ -47,7 +48,7 @@ import { } from './components'; const Index: FC = () => { - const { t, i18n } = useTranslation('translation', { keyPrefix: 'install' }); + const { t } = useTranslation('translation', { keyPrefix: 'install' }); const [step, setStep] = useState(1); const [loading, setLoading] = useState(true); const [errorData, setErrorData] = useState<{ [propName: string]: any }>({ @@ -293,7 +294,7 @@ const Index: FC = () => { useEffect(() => { configYmlCheck(); - i18n.changeLanguage(Storage.get(CURRENT_LANG_STORAGE_KEY)); + setupInstallLanguage(Storage.get(CURRENT_LANG_STORAGE_KEY)); }, []); if (loading) { diff --git a/ui/src/services/install/index.ts b/ui/src/services/install/index.ts index 70e581a29..b4b7dc0eb 100644 --- a/ui/src/services/install/index.ts +++ b/ui/src/services/install/index.ts @@ -38,3 +38,7 @@ export const installBaseInfo = (params) => { export const getInstallLangOptions = () => { return request.get('/installation/language/options'); }; + +export const getInstallLanguageConfig = () => { + return request.get('/installation/language/config'); +}; diff --git a/ui/src/utils/localize.ts b/ui/src/utils/localize.ts index eea132a84..f446922c3 100644 --- a/ui/src/utils/localize.ts +++ b/ui/src/utils/localize.ts @@ -35,6 +35,7 @@ import { } from '@/common/constants'; import { getAdminLanguageOptions, + getInstallLanguageConfig, getLanguageConfig, getLanguageOptions, } from '@/services'; @@ -61,6 +62,15 @@ export const loadLanguageOptions = async (forAdmin = false) => { }; const pullLanguageConf = (res) => { + if (window.location.pathname === '/install') { + return getInstallLanguageConfig().then((langConf) => { + if (langConf && langConf.ui) { + res.resources = langConf.ui; + Storage.set(LANG_RESOURCE_STORAGE_KEY, res); + } + }); + } + return getLanguageConfig().then((langConf) => { if (langConf && langConf.ui) { res.resources = langConf.ui; @@ -68,6 +78,7 @@ const pullLanguageConf = (res) => { } }); }; + const addI18nResource = async (langName) => { const res = { lng: langName, resources: undefined }; const storageResource = Storage.get(LANG_RESOURCE_STORAGE_KEY); @@ -149,3 +160,9 @@ export const setupAppTheme = () => { const theme = getCurrentTheme(); changeTheme(theme); }; + +export const setupInstallLanguage = async (lang) => { + await addI18nResource(lang); + localeDayjs(lang); + i18next.changeLanguage(lang); +}; From fb9bece01c68b5c02fe5b3b3ef99472475685eb0 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Wed, 4 Sep 2024 09:13:20 +0800 Subject: [PATCH 3/6] fix: format code --- cmd/answer/main.go | 38 +++++++++++++++++++++ internal/service/mock/siteinfo_repo_mock.go | 19 +++++++++++ ui/src/utils/localize.ts | 4 ++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 cmd/answer/main.go diff --git a/cmd/answer/main.go b/cmd/answer/main.go new file mode 100644 index 000000000..fb91f5942 --- /dev/null +++ b/cmd/answer/main.go @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +//go:generate go run github.com/swaggo/swag/cmd/swag init -g ./cmd/answer/main.go -d ../../ -o ../../docs + +package main + +import ( + answercmd "github.com/apache/incubator-answer/cmd" +) + +// main godoc +// @title "apache answer" +// @description = "apache answer api" +// @version = "v0.0.1" +// @BasePath = "/" +// @securityDefinitions.apikey ApiKeyAuth +// @in header +// @name Authorization +func main() { + answercmd.Main() +} diff --git a/internal/service/mock/siteinfo_repo_mock.go b/internal/service/mock/siteinfo_repo_mock.go index cdc72b801..abc1fe072 100644 --- a/internal/service/mock/siteinfo_repo_mock.go +++ b/internal/service/mock/siteinfo_repo_mock.go @@ -1,3 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + // Code generated by MockGen. DO NOT EDIT. // Source: ./siteinfo_service.go diff --git a/ui/src/utils/localize.ts b/ui/src/utils/localize.ts index f446922c3..fcc322a51 100644 --- a/ui/src/utils/localize.ts +++ b/ui/src/utils/localize.ts @@ -162,7 +162,9 @@ export const setupAppTheme = () => { }; export const setupInstallLanguage = async (lang) => { - await addI18nResource(lang); + if (!i18next.getDataByLanguage(lang)?.translation) { + await addI18nResource(lang); + } localeDayjs(lang); i18next.changeLanguage(lang); }; From 26afc0979c07dca8cbc358031228ab9fce3421cb Mon Sep 17 00:00:00 2001 From: kumfo Date: Wed, 4 Sep 2024 10:02:03 +0800 Subject: [PATCH 4/6] feat(install): add get language mapping api --- internal/install/install_controller.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/install/install_controller.go b/internal/install/install_controller.go index 05f7daa67..ddfed6b9b 100644 --- a/internal/install/install_controller.go +++ b/internal/install/install_controller.go @@ -38,7 +38,6 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/copier" "github.com/segmentfault/pacman/errors" - "github.com/segmentfault/pacman/i18n" "github.com/segmentfault/pacman/log" ) @@ -62,7 +61,15 @@ func LangOptions(ctx *gin.Context) { // @Success 200 {object} handler.RespBody{} // @Router /installation/language/config [get] func GetLangMapping(ctx *gin.Context) { - // TODO + t, err := translator.NewTranslator(&translator.I18n{BundleDir: cli.I18nPath}) + if err != nil { + handler.HandleResponse(ctx, err, nil) + return + } + trData, _ := t.Dump(handler.GetLang(ctx)) + var resp map[string]any + _ = json.Unmarshal(trData, &resp) + handler.HandleResponse(ctx, nil, resp) } // CheckConfigFileAndRedirectToInstallPage if config file not exist try to redirect to install page From 1d33c8827c4960e35459d65101f7c598b0d85e45 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Wed, 4 Sep 2024 21:13:38 +0800 Subject: [PATCH 5/6] fix: change fail --- docs/docs.go | 6 +++--- docs/swagger.json | 6 +++--- docs/swagger.yaml | 6 +++--- internal/install/install_controller.go | 6 ++++-- ui/src/services/install/index.ts | 4 ++-- ui/src/utils/localize.ts | 6 ++---- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 32942b7d1..69078bf4d 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -7239,9 +7239,9 @@ const docTemplate = `{ "parameters": [ { "type": "string", - "description": "Accept-Language", - "name": "Accept-Language", - "in": "header", + "description": "installation language", + "name": "lang", + "in": "query", "required": true } ], diff --git a/docs/swagger.json b/docs/swagger.json index 3aa86c6b0..4ca27f0ad 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -7213,9 +7213,9 @@ "parameters": [ { "type": "string", - "description": "Accept-Language", - "name": "Accept-Language", - "in": "header", + "description": "installation language", + "name": "lang", + "in": "query", "required": true } ], diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 4188b6c0b..b9c36562d 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -7243,9 +7243,9 @@ paths: get: description: get installation language config mapping parameters: - - description: Accept-Language - in: header - name: Accept-Language + - description: installation language + in: query + name: lang required: true type: string produces: diff --git a/internal/install/install_controller.go b/internal/install/install_controller.go index ddfed6b9b..0a1165693 100644 --- a/internal/install/install_controller.go +++ b/internal/install/install_controller.go @@ -38,6 +38,7 @@ import ( "github.com/gin-gonic/gin" "github.com/jinzhu/copier" "github.com/segmentfault/pacman/errors" + "github.com/segmentfault/pacman/i18n" "github.com/segmentfault/pacman/log" ) @@ -56,7 +57,7 @@ func LangOptions(ctx *gin.Context) { // @Summary get installation language config mapping // @Description get installation language config mapping // @Tags Lang -// @Param Accept-Language header string true "Accept-Language" +// @Param lang query string true "installation language" // @Produce json // @Success 200 {object} handler.RespBody{} // @Router /installation/language/config [get] @@ -66,7 +67,8 @@ func GetLangMapping(ctx *gin.Context) { handler.HandleResponse(ctx, err, nil) return } - trData, _ := t.Dump(handler.GetLang(ctx)) + lang := ctx.Query("lang") + trData, _ := t.Dump(i18n.Language(lang)) var resp map[string]any _ = json.Unmarshal(trData, &resp) handler.HandleResponse(ctx, nil, resp) diff --git a/ui/src/services/install/index.ts b/ui/src/services/install/index.ts index b4b7dc0eb..47ae75faa 100644 --- a/ui/src/services/install/index.ts +++ b/ui/src/services/install/index.ts @@ -39,6 +39,6 @@ export const getInstallLangOptions = () => { return request.get('/installation/language/options'); }; -export const getInstallLanguageConfig = () => { - return request.get('/installation/language/config'); +export const getInstallLanguageConfig = (lang: string) => { + return request.get(`/installation/language/config?lang=${lang}`); }; diff --git a/ui/src/utils/localize.ts b/ui/src/utils/localize.ts index fcc322a51..0b45a2222 100644 --- a/ui/src/utils/localize.ts +++ b/ui/src/utils/localize.ts @@ -63,7 +63,7 @@ export const loadLanguageOptions = async (forAdmin = false) => { const pullLanguageConf = (res) => { if (window.location.pathname === '/install') { - return getInstallLanguageConfig().then((langConf) => { + return getInstallLanguageConfig(res.lng).then((langConf) => { if (langConf && langConf.ui) { res.resources = langConf.ui; Storage.set(LANG_RESOURCE_STORAGE_KEY, res); @@ -162,9 +162,7 @@ export const setupAppTheme = () => { }; export const setupInstallLanguage = async (lang) => { - if (!i18next.getDataByLanguage(lang)?.translation) { - await addI18nResource(lang); - } + await addI18nResource(lang); localeDayjs(lang); i18next.changeLanguage(lang); }; From 2e67b9ca6f7c29c2b0a030958788c4a7f20e9f59 Mon Sep 17 00:00:00 2001 From: LinkinStars Date: Wed, 11 Sep 2024 16:41:45 +0800 Subject: [PATCH 6/6] fix(swagger): fix title and description on swagger docs --- cmd/answer/main.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/answer/main.go b/cmd/answer/main.go index fb91f5942..9ba33b6f4 100644 --- a/cmd/answer/main.go +++ b/cmd/answer/main.go @@ -26,10 +26,9 @@ import ( ) // main godoc -// @title "apache answer" -// @description = "apache answer api" -// @version = "v0.0.1" -// @BasePath = "/" +// @title Apache Answer +// @description Apache Answer API +// @BasePath / // @securityDefinitions.apikey ApiKeyAuth // @in header // @name Authorization