Skip to content

Commit

Permalink
refactor(wip): translation module #135
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Aug 27, 2023
1 parent 6ede443 commit 8cc7194
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 232 deletions.
2 changes: 1 addition & 1 deletion frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import (
"embed"
)

//go:embed dist/* dist/*/*
//go:embed dist/* dist/*/* src/language/* src/language/*/*
var DistFS embed.FS
13 changes: 13 additions & 0 deletions frontend/src/components/SetLanguage/SetLanguage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ref, watch} from 'vue'
import {useSettingsStore} from '@/pinia'
import {useRoute} from 'vue-router'
import http from '@/lib/http'
const settings = useSettingsStore()
Expand All @@ -13,7 +14,19 @@ const route = useRoute()
const current = ref(gettext.current)
const languageAvailable = gettext.available
function init() {
if (current.value !== 'en') {
http.get('/translation/' + current.value).then(r => {
gettext.translations[current.value] = r
})
}
}
init()
watch(current, (v) => {
init()
settings.set_language(v)
gettext.current = v
// @ts-ignored
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/gettext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {createGettext} from 'vue3-gettext'
import translations from './language/translations.json'

export default createGettext({
availableLanguages: {
Expand All @@ -11,7 +10,7 @@ export default createGettext({
ru_RU: 'Ru'
},
defaultLanguage: 'en',
translations: translations,
translations: {},
silent: true
})

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"2.0.0-beta.3","build_id":12,"total_build":216}
{"version":"2.0.0-beta.3","build_id":13,"total_build":217}
2 changes: 1 addition & 1 deletion frontend/version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"2.0.0-beta.3","build_id":12,"total_build":216}
{"version":"2.0.0-beta.3","build_id":13,"total_build":217}
2 changes: 1 addition & 1 deletion frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default defineConfig({
server: {
proxy: {
'/api': {
target: 'http://127.0.0.1:9002/',
target: 'http://127.0.0.1:9001/',
changeOrigin: true,
secure: false,
ws: true
Expand Down
189 changes: 112 additions & 77 deletions go.mod

Large diffs are not rendered by default.

393 changes: 244 additions & 149 deletions go.sum

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions server/api/translation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package api

import (
"github.com/0xJacky/Nginx-UI/server/internal/translation"
"github.com/gin-gonic/gin"
"net/http"
)

func GetTranslation(c *gin.Context) {
code := c.Param("code")

c.JSON(http.StatusOK, translation.GetTranslation(code))
}
49 changes: 49 additions & 0 deletions server/internal/translation/translation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package translation

import (
"fmt"
"github.com/0xJacky/Nginx-UI/frontend"
"github.com/0xJacky/pofile/pofile"
"io"
"log"
)

var Dict map[string]pofile.Dict

func init() {
Dict = make(map[string]pofile.Dict)

langCode := []string{"zh_CN", "zh_TW", "ru_RU", "fr_FR", "es"}

for _, v := range langCode {
handlePo(v)
}
}

func handlePo(langCode string) {
file, err := frontend.DistFS.Open(fmt.Sprintf("src/language/%s/app.po", langCode))

if err != nil {
log.Fatalln(err)
}

defer file.Close()

bytes, err := io.ReadAll(file)

if err != nil {
log.Fatalln(err)
}

p, err := pofile.ParseText(string(bytes))

if err != nil {
log.Fatalln(err)
}

Dict[langCode] = p.ToDict()
}

func GetTranslation(langCode string) pofile.Dict {
return Dict[langCode]
}
3 changes: 3 additions & 0 deletions server/router/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ func InitRouter() *gin.Engine {

// node
g.GET("node", api.GetCurrentNode)

// translation
g.GET("translation/:code", api.GetTranslation)
}
}

Expand Down

0 comments on commit 8cc7194

Please sign in to comment.