Add more term fixes and markdown format fixes

This commit is contained in:
Will
2019-03-03 00:40:37 +08:00
parent accc3cc768
commit d5355ad2ec
69 changed files with 573 additions and 393 deletions

View File

@@ -35,11 +35,12 @@ func msg(locale, key string) string {
}
return ""
}
```
上面範例示範了不同 locale 的文字翻譯,實現了中文和英文對於同一個 key 顯示不同語言的實現,上面實現了中文的文字訊息,如果想切換到英文版本,只需要把 lang 設定為 en 即可。
有些時候僅是 key-value 替換是不能滿足需要的,例如"I am 30 years old",中文表達是"我今年 30 歲了",而此處的 30 是一個變數,該怎麼辦呢?這個時候,我們可以結合`fmt.Printf`函式來實現,請看下面的程式碼:
```Go
en["how old"] ="I am %d years old"
@@ -66,9 +67,10 @@ loc,_:=time.LoadLocation(msg(lang,"time_zone"))
t:=time.Now()
t = t.In(loc)
fmt.Println(t.Format(time.RFC3339))
```
我們可以透過類似處理文字格式的方式來解決時間格式的問題,舉例如下:
```Go
en["date_format"]="%Y-%m-%d %H:%M:%S"
@@ -87,10 +89,11 @@ func date(fomate string,t time.Time) string{
//%d 替換成 24
}
```
## 本地化貨幣值
各個地區的貨幣表示也不一樣,處理方式也與日期差不多,細節請看下面程式碼:
```Go
en["money"] ="USD %d"
@@ -101,8 +104,8 @@ fmt.Println(money_format(msg(lang,"date_format"),100))
func money_format(fomate string,money int64) string{
return fmt.Sprintf(fomate,money)
}
```
## 本地化檢視和資源
我們可能會根據 Locale 的不同來展示檢視這些檢視包含不同的圖片、css、js 等各種靜態資源。那麼應如何來處理這些資訊呢?首先我們應按 locale 來組織檔案資訊,請看下面的檔案目錄安排:
```html
@@ -120,9 +123,10 @@ views
|--css
index.tpl
login.tpl
```
有了這個目錄結構後我們就可以在渲染的地方這樣來實現程式碼:
```Go
s1, _ := template.ParseFiles("views/"+lang+"/index.tpl")