You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.4KB

  1. /*
  2. Package gotext implements GNU gettext utilities.
  3. For quick/simple translations you can use the package level functions directly.
  4. import (
  5. "fmt"
  6. "git.deineagentur.com/DeineAgenturUG/gotext"
  7. )
  8. func main() {
  9. // Configure package
  10. gotext.Configure("/path/to/locales/root/dir", "en_UK", "domain-name")
  11. // Translate text from default domain
  12. fmt.Println(gotext.Get("My text on 'domain-name' domain"))
  13. // Translate text from a different domain without reconfigure
  14. fmt.Println(gotext.GetD("domain2", "Another text on a different domain"))
  15. }
  16. */
  17. package gotext
  18. import (
  19. "encoding/gob"
  20. "sync"
  21. )
  22. // Global environment variables
  23. type config struct {
  24. sync.RWMutex
  25. // Default domain to look at when no domain is specified. Used by package level functions.
  26. domain string
  27. // Language set.
  28. language string
  29. // Path to library directory where all locale directories and Translation files are.
  30. library string
  31. // Storage for package level methods
  32. storage *Locale
  33. }
  34. var globalConfig *config
  35. func init() {
  36. // Init default configuration
  37. globalConfig = &config{
  38. domain: "default",
  39. language: "en_US",
  40. library: "/usr/local/share/locale",
  41. storage: nil,
  42. }
  43. // Register Translator types for gob encoding
  44. gob.Register(TranslatorEncoding{})
  45. }
  46. // loadStorage creates a new Locale object at package level based on the Global variables settings.
  47. // It's called automatically when trying to use Get or GetD methods.
  48. func loadStorage(force bool) {
  49. globalConfig.Lock()
  50. if globalConfig.storage == nil || force {
  51. globalConfig.storage = NewLocale(globalConfig.library, globalConfig.language)
  52. }
  53. if _, ok := globalConfig.storage.Domains[globalConfig.domain]; !ok || force {
  54. globalConfig.storage.AddDomain(globalConfig.domain)
  55. }
  56. globalConfig.storage.SetDomain(globalConfig.domain)
  57. globalConfig.Unlock()
  58. }
  59. // GetDomain is the domain getter for the package configuration
  60. func GetDomain() string {
  61. var dom string
  62. globalConfig.RLock()
  63. if globalConfig.storage != nil {
  64. dom = globalConfig.storage.GetDomain()
  65. }
  66. if dom == "" {
  67. dom = globalConfig.domain
  68. }
  69. globalConfig.RUnlock()
  70. return dom
  71. }
  72. // SetDomain sets the name for the domain to be used at package level.
  73. // It reloads the corresponding Translation file.
  74. func SetDomain(dom string) {
  75. globalConfig.Lock()
  76. globalConfig.domain = dom
  77. if globalConfig.storage != nil {
  78. globalConfig.storage.SetDomain(dom)
  79. }
  80. globalConfig.Unlock()
  81. loadStorage(true)
  82. }
  83. // GetLanguage is the language getter for the package configuration
  84. func GetLanguage() string {
  85. globalConfig.RLock()
  86. lang := globalConfig.language
  87. globalConfig.RUnlock()
  88. return lang
  89. }
  90. // SetLanguage sets the language code to be used at package level.
  91. // It reloads the corresponding Translation file.
  92. func SetLanguage(lang string) {
  93. globalConfig.Lock()
  94. globalConfig.language = SimplifiedLocale(lang)
  95. globalConfig.Unlock()
  96. loadStorage(true)
  97. }
  98. // GetLibrary is the library getter for the package configuration
  99. func GetLibrary() string {
  100. globalConfig.RLock()
  101. lib := globalConfig.library
  102. globalConfig.RUnlock()
  103. return lib
  104. }
  105. // SetLibrary sets the root path for the loale directories and files to be used at package level.
  106. // It reloads the corresponding Translation file.
  107. func SetLibrary(lib string) {
  108. globalConfig.Lock()
  109. globalConfig.library = lib
  110. globalConfig.Unlock()
  111. loadStorage(true)
  112. }
  113. // Configure sets all configuration variables to be used at package level and reloads the corresponding Translation file.
  114. // It receives the library path, language code and domain name.
  115. // This function is recommended to be used when changing more than one setting,
  116. // as using each setter will introduce a I/O overhead because the Translation file will be loaded after each set.
  117. func Configure(lib, lang, dom string) {
  118. globalConfig.Lock()
  119. globalConfig.library = lib
  120. globalConfig.language = SimplifiedLocale(lang)
  121. globalConfig.domain = dom
  122. globalConfig.Unlock()
  123. loadStorage(true)
  124. }
  125. // Get uses the default domain globally set to return the corresponding Translation of a given string.
  126. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
  127. func Get(str string, vars ...interface{}) string {
  128. return GetD(GetDomain(), str, vars...)
  129. }
  130. // GetN retrieves the (N)th plural form of Translation for the given string in the default domain.
  131. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
  132. func GetN(str, plural string, n int, vars ...interface{}) string {
  133. return GetND(GetDomain(), str, plural, n, vars...)
  134. }
  135. // GetD returns the corresponding Translation in the given domain for a given string.
  136. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
  137. func GetD(dom, str string, vars ...interface{}) string {
  138. // Try to load default package Locale storage
  139. loadStorage(false)
  140. // Return Translation
  141. globalConfig.RLock()
  142. if _, ok := globalConfig.storage.Domains[dom]; !ok {
  143. globalConfig.storage.AddDomain(dom)
  144. }
  145. tr := globalConfig.storage.GetD(dom, str, vars...)
  146. globalConfig.RUnlock()
  147. return tr
  148. }
  149. // GetND retrieves the (N)th plural form of Translation in the given domain for a given string.
  150. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
  151. func GetND(dom, str, plural string, n int, vars ...interface{}) string {
  152. // Try to load default package Locale storage
  153. loadStorage(false)
  154. // Return Translation
  155. globalConfig.RLock()
  156. if _, ok := globalConfig.storage.Domains[dom]; !ok {
  157. globalConfig.storage.AddDomain(dom)
  158. }
  159. tr := globalConfig.storage.GetND(dom, str, plural, n, vars...)
  160. globalConfig.RUnlock()
  161. return tr
  162. }
  163. // GetC uses the default domain globally set to return the corresponding Translation of the given string in the given context.
  164. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
  165. func GetC(str, ctx string, vars ...interface{}) string {
  166. return GetDC(GetDomain(), str, ctx, vars...)
  167. }
  168. // GetNC retrieves the (N)th plural form of Translation for the given string in the given context in the default domain.
  169. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
  170. func GetNC(str, plural string, n int, ctx string, vars ...interface{}) string {
  171. return GetNDC(GetDomain(), str, plural, n, ctx, vars...)
  172. }
  173. // GetDC returns the corresponding Translation in the given domain for the given string in the given context.
  174. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
  175. func GetDC(dom, str, ctx string, vars ...interface{}) string {
  176. // Try to load default package Locale storage
  177. loadStorage(false)
  178. // Return Translation
  179. globalConfig.RLock()
  180. tr := globalConfig.storage.GetDC(dom, str, ctx, vars...)
  181. globalConfig.RUnlock()
  182. return tr
  183. }
  184. // GetNDC retrieves the (N)th plural form of Translation in the given domain for a given string.
  185. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax.
  186. func GetNDC(dom, str, plural string, n int, ctx string, vars ...interface{}) string {
  187. // Try to load default package Locale storage
  188. loadStorage(false)
  189. // Return Translation
  190. globalConfig.RLock()
  191. tr := globalConfig.storage.GetNDC(dom, str, plural, n, ctx, vars...)
  192. globalConfig.RUnlock()
  193. return tr
  194. }