i18n.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright 2014 The Macaron Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. // Package i18n is a middleware that provides app Internationalization and Localization of Macaron.
  15. package i18n
  16. import (
  17. "fmt"
  18. "path"
  19. "strings"
  20. "github.com/Unknwon/com"
  21. "github.com/Unknwon/i18n"
  22. "golang.org/x/text/language"
  23. "gopkg.in/macaron.v1"
  24. )
  25. const _VERSION = "0.3.0"
  26. func Version() string {
  27. return _VERSION
  28. }
  29. // initLocales initializes language type list and Accept-Language header matcher.
  30. func initLocales(opt Options) language.Matcher {
  31. tags := make([]language.Tag, len(opt.Langs))
  32. for i, lang := range opt.Langs {
  33. tags[i] = language.Raw.Make(lang)
  34. fname := fmt.Sprintf(opt.Format, lang)
  35. // Append custom locale file.
  36. custom := []interface{}{}
  37. customPath := path.Join(opt.CustomDirectory, fname)
  38. if com.IsFile(customPath) {
  39. custom = append(custom, customPath)
  40. }
  41. var locale interface{}
  42. if data, ok := opt.Files[fname]; ok {
  43. locale = data
  44. } else {
  45. locale = path.Join(opt.Directory, fname)
  46. }
  47. err := i18n.SetMessageWithDesc(lang, opt.Names[i], locale, custom...)
  48. if err != nil && err != i18n.ErrLangAlreadyExist {
  49. panic(fmt.Errorf("fail to set message file(%s): %v", lang, err))
  50. }
  51. }
  52. return language.NewMatcher(tags)
  53. }
  54. // A Locale describles the information of localization.
  55. type Locale struct {
  56. i18n.Locale
  57. }
  58. // Language returns language current locale represents.
  59. func (l Locale) Language() string {
  60. return l.Lang
  61. }
  62. // Options represents a struct for specifying configuration options for the i18n middleware.
  63. type Options struct {
  64. // Suburl of path. Default is empty.
  65. SubURL string
  66. // Directory to load locale files. Default is "conf/locale"
  67. Directory string
  68. // File stores actual data of locale files. Used for in-memory purpose.
  69. Files map[string][]byte
  70. // Custom directory to overload locale files. Default is "custom/conf/locale"
  71. CustomDirectory string
  72. // Langauges that will be supported, order is meaningful.
  73. Langs []string
  74. // Human friendly names corresponding to Langs list.
  75. Names []string
  76. // Default language locale, leave empty to remain unset.
  77. DefaultLang string
  78. // Locale file naming style. Default is "locale_%s.ini".
  79. Format string
  80. // Name of language parameter name in URL. Default is "lang".
  81. Parameter string
  82. // Redirect when user uses get parameter to specify language.
  83. Redirect bool
  84. // Name that maps into template variable. Default is "i18n".
  85. TmplName string
  86. // Configuration section name. Default is "i18n".
  87. Section string
  88. }
  89. func prepareOptions(options []Options) Options {
  90. var opt Options
  91. if len(options) > 0 {
  92. opt = options[0]
  93. }
  94. if len(opt.Section) == 0 {
  95. opt.Section = "i18n"
  96. }
  97. sec := macaron.Config().Section(opt.Section)
  98. opt.SubURL = strings.TrimSuffix(opt.SubURL, "/")
  99. if len(opt.Langs) == 0 {
  100. opt.Langs = sec.Key("LANGS").Strings(",")
  101. }
  102. if len(opt.Names) == 0 {
  103. opt.Names = sec.Key("NAMES").Strings(",")
  104. }
  105. if len(opt.Langs) == 0 {
  106. panic("no language is specified")
  107. } else if len(opt.Langs) != len(opt.Names) {
  108. panic("length of langs is not same as length of names")
  109. }
  110. i18n.SetDefaultLang(opt.DefaultLang)
  111. if len(opt.Directory) == 0 {
  112. opt.Directory = sec.Key("DIRECTORY").MustString("conf/locale")
  113. }
  114. if len(opt.CustomDirectory) == 0 {
  115. opt.CustomDirectory = sec.Key("CUSTOM_DIRECTORY").MustString("custom/conf/locale")
  116. }
  117. if len(opt.Format) == 0 {
  118. opt.Format = sec.Key("FORMAT").MustString("locale_%s.ini")
  119. }
  120. if len(opt.Parameter) == 0 {
  121. opt.Parameter = sec.Key("PARAMETER").MustString("lang")
  122. }
  123. if !opt.Redirect {
  124. opt.Redirect = sec.Key("REDIRECT").MustBool()
  125. }
  126. if len(opt.TmplName) == 0 {
  127. opt.TmplName = sec.Key("TMPL_NAME").MustString("i18n")
  128. }
  129. return opt
  130. }
  131. type LangType struct {
  132. Lang, Name string
  133. }
  134. // I18n is a middleware provides localization layer for your application.
  135. // Paramenter langs must be in the form of "en-US", "zh-CN", etc.
  136. // Otherwise it may not recognize browser input.
  137. func I18n(options ...Options) macaron.Handler {
  138. opt := prepareOptions(options)
  139. m := initLocales(opt)
  140. return func(ctx *macaron.Context) {
  141. isNeedRedir := false
  142. hasCookie := false
  143. // 1. Check URL arguments.
  144. lang := ctx.Query(opt.Parameter)
  145. // 2. Get language information from cookies.
  146. if len(lang) == 0 {
  147. lang = ctx.GetCookie("lang")
  148. hasCookie = true
  149. } else {
  150. isNeedRedir = true
  151. }
  152. // Check again in case someone modify by purpose.
  153. if !i18n.IsExist(lang) {
  154. lang = ""
  155. isNeedRedir = false
  156. hasCookie = false
  157. }
  158. // 3. Get language information from 'Accept-Language'.
  159. // The first element in the list is chosen to be the default language automatically.
  160. if len(lang) == 0 {
  161. tags, _, _ := language.ParseAcceptLanguage(ctx.Req.Header.Get("Accept-Language"))
  162. tag, _, _ := m.Match(tags...)
  163. lang = tag.String()
  164. isNeedRedir = false
  165. }
  166. curLang := LangType{
  167. Lang: lang,
  168. }
  169. // Save language information in cookies.
  170. if !hasCookie {
  171. ctx.SetCookie("lang", curLang.Lang, 1<<31-1, "/"+strings.TrimPrefix(opt.SubURL, "/"))
  172. }
  173. restLangs := make([]LangType, 0, i18n.Count()-1)
  174. langs := i18n.ListLangs()
  175. names := i18n.ListLangDescs()
  176. for i, v := range langs {
  177. if lang != v {
  178. restLangs = append(restLangs, LangType{v, names[i]})
  179. } else {
  180. curLang.Name = names[i]
  181. }
  182. }
  183. // Set language properties.
  184. locale := Locale{i18n.Locale{lang}}
  185. ctx.Map(locale)
  186. ctx.Locale = locale
  187. ctx.Data[opt.TmplName] = locale
  188. ctx.Data["Tr"] = i18n.Tr
  189. ctx.Data["Lang"] = locale.Lang
  190. ctx.Data["LangName"] = curLang.Name
  191. ctx.Data["AllLangs"] = append([]LangType{curLang}, restLangs...)
  192. ctx.Data["RestLangs"] = restLangs
  193. if opt.Redirect && isNeedRedir {
  194. ctx.Redirect(opt.SubURL + ctx.Req.RequestURI[:strings.Index(ctx.Req.RequestURI, "?")])
  195. }
  196. }
  197. }