Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
62e9c890
Commit
62e9c890
authored
Jun 27, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
middleware: support i18n
parent
0f170a80
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
82 deletions
+48
-82
i18n.go
middleware/i18n.go
+48
-82
No files found.
middleware/i18n.go
View file @
62e9c890
...
...
@@ -10,85 +10,51 @@
package
middleware
//import (
// "github.com/astaxie/beego/config"
// "os"
// "path"
//)
//type Translation struct {
// filetype string
// CurrentLocal string
// Locales map[string]map[string]string
//}
//func NewLocale(filetype string) *Translation {
// return &Translation{
// filetype: filetype,
// CurrentLocal: "zh",
// Locales: make(map[string]map[string]string),
// }
//}
//func (t *Translation) loadTranslations(dirPath string) error {
// dir, err := os.Open(dirPath)
// if err != nil {
// return err
// }
// defer dir.Close()
// names, err := dir.Readdirnames(-1)
// if err != nil {
// return err
// }
// for _, name := range names {
// fullPath := path.Join(dirPath, name)
// fi, err := os.Stat(fullPath)
// if err != nil {
// return err
// }
// if fi.IsDir() {
// continue
// } else {
// if err := t.loadTranslation(fullPath, name); err != nil {
// return err
// }
// }
// }
// return nil
//}
//func (t *Translation) loadTranslation(fullPath, locale string) error {
// sourceKey2Trans, ok := t.Locales[locale]
// if !ok {
// sourceKey2Trans = make(map[string]string)
// t.Locales[locale] = sourceKey2Trans
// }
// for _, m := range trf.Messages {
// if m.Translation != "" {
// sourceKey2Trans[sourceKey(m.Source, m.Context)] = m.Translation
// }
// }
// return nil
//}
//func (t *Translation) SetLocale(local string) {
// t.CurrentLocal = local
//}
//func (t *Translation) Translate(key string) string {
// if ct, ok := t.Locales[t.CurrentLocal]; ok {
// if v, o := ct[key]; o {
// return v
// }
// }
// return key
//}
import
(
"encoding/json"
"io/ioutil"
"os"
)
type
Translation
struct
{
filepath
string
CurrentLocal
string
Locales
map
[
string
]
map
[
string
]
string
}
func
NewLocale
(
filepath
string
,
defaultlocal
string
)
*
Translation
{
i18n
:=
make
(
map
[
string
]
map
[
string
]
string
)
file
,
err
:=
os
.
Open
(
filepath
)
if
err
!=
nil
{
panic
(
"open "
+
filepath
+
" err :"
+
err
.
Error
())
}
data
,
err
:=
ioutil
.
ReadAll
(
file
)
if
err
!=
nil
{
panic
(
"read "
+
filepath
+
" err :"
+
err
.
Error
())
}
err
=
json
.
Unmarshal
(
data
,
&
i18n
)
if
err
!=
nil
{
panic
(
"json.Unmarshal "
+
filepath
+
" err :"
+
err
.
Error
())
}
return
&
Translation
{
filepath
:
filepath
,
CurrentLocal
:
defaultlocal
,
Locales
:
i18n
,
}
}
func
(
t
*
Translation
)
SetLocale
(
local
string
)
{
t
.
CurrentLocal
=
local
}
func
(
t
*
Translation
)
Translate
(
key
string
,
local
string
)
string
{
if
local
==
""
{
local
=
t
.
CurrentLocal
}
if
ct
,
ok
:=
t
.
Locales
[
key
];
ok
{
if
v
,
o
:=
ct
[
local
];
o
{
return
v
}
}
return
key
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment