Skip to content

Commit

Permalink
Merge branch 'dev' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Sep 11, 2024
2 parents 6fb7131 + 2e67b9c commit 462e1d0
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 8 deletions.
7 changes: 3 additions & 4 deletions cmd/answer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": "installation language",
"name": "lang",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.RespBody"
}
}
}
}
},
"/installation/language/options": {
"get": {
"description": "get installation language options",
Expand Down
29 changes: 29 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "installation language",
"name": "lang",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.RespBody"
}
}
}
}
},
"/installation/language/options": {
"get": {
"description": "get installation language options",
Expand Down
19 changes: 19 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7239,6 +7239,25 @@ paths:
summary: init environment
tags:
- installation
/installation/language/config:
get:
description: get installation language config mapping
parameters:
- description: installation language
in: query
name: lang
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
Expand Down
23 changes: 23 additions & 0 deletions internal/install/install_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package install

import (
"encoding/json"
"net/http"
"os"
"path/filepath"
Expand All @@ -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"
)

Expand All @@ -51,6 +53,27 @@ 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 lang query string true "installation language"
// @Produce json
// @Success 200 {object} handler.RespBody{}
// @Router /installation/language/config [get]
func GetLangMapping(ctx *gin.Context) {
t, err := translator.NewTranslator(&translator.I18n{BundleDir: cli.I18nPath})
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
lang := ctx.Query("lang")
trData, _ := t.Dump(i18n.Language(lang))
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
// @Summary if config file not exist try to redirect to install page
// @Description if config file not exist try to redirect to install page
Expand Down
1 change: 1 addition & 0 deletions internal/install/install_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions ui/src/i18n/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ 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 { DEFAULT_LANG, LANG_RESOURCE_STORAGE_KEY } from '@/common/constants';
import Storage from '@/utils/storage';
Expand All @@ -34,9 +33,6 @@ const initResources = {
en_US: {
translation: en_US.ui,
},
// zh_CN: {
// translation: zh_CN.ui,
// },
};

const storageLang = Storage.get(LANG_RESOURCE_STORAGE_KEY);
Expand Down
2 changes: 2 additions & 0 deletions ui/src/pages/Install/components/FirstStep/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,6 +66,7 @@ const Index: FC<Props> = ({ visible, data, changeCallback, nextCallback }) => {
value={data.value}
isInvalid={data.isInvalid}
onChange={(e) => {
setupInstallLanguage(e.target.value);
changeCallback({
lang: {
value: e.target.value,
Expand Down
2 changes: 2 additions & 0 deletions ui/src/pages/Install/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -293,6 +294,7 @@ const Index: FC = () => {

useEffect(() => {
configYmlCheck();
setupInstallLanguage(Storage.get(CURRENT_LANG_STORAGE_KEY));
}, []);

if (loading) {
Expand Down
4 changes: 4 additions & 0 deletions ui/src/services/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ export const installBaseInfo = (params) => {
export const getInstallLangOptions = () => {
return request.get('/installation/language/options');
};

export const getInstallLanguageConfig = (lang: string) => {
return request.get(`/installation/language/config?lang=${lang}`);
};
17 changes: 17 additions & 0 deletions ui/src/utils/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from '@/common/constants';
import {
getAdminLanguageOptions,
getInstallLanguageConfig,
getLanguageConfig,
getLanguageOptions,
} from '@/services';
Expand All @@ -61,13 +62,23 @@ export const loadLanguageOptions = async (forAdmin = false) => {
};

const pullLanguageConf = (res) => {
if (window.location.pathname === '/install') {
return getInstallLanguageConfig(res.lng).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;
Storage.set(LANG_RESOURCE_STORAGE_KEY, res);
}
});
};

const addI18nResource = async (langName) => {
const res = { lng: langName, resources: undefined };
const storageResource = Storage.get(LANG_RESOURCE_STORAGE_KEY);
Expand Down Expand Up @@ -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);
};

0 comments on commit 462e1d0

Please sign in to comment.