gettext.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gettext
  5. var (
  6. defaultManager = newDomainManager()
  7. )
  8. var (
  9. DefaultLocale = getDefaultLocale() // use $(LC_MESSAGES) or $(LANG) or "default"
  10. )
  11. // SetLocale sets and queries the program's current locale.
  12. //
  13. // If the locale is not empty string, set the new local.
  14. //
  15. // If the locale is empty string, don't change anything.
  16. //
  17. // Returns is the current locale.
  18. //
  19. // Examples:
  20. // SetLocale("") // get locale: return DefaultLocale
  21. // SetLocale("zh_CN") // set locale: return zh_CN
  22. // SetLocale("") // get locale: return zh_CN
  23. func SetLocale(locale string) string {
  24. return defaultManager.SetLocale(locale)
  25. }
  26. // BindTextdomain sets and queries program's domains.
  27. //
  28. // If the domain and path are all not empty string, bind the new domain.
  29. // If the domain already exists, return error.
  30. //
  31. // If the domain is not empty string, but the path is the empty string,
  32. // delete the domain.
  33. // If the domain don't exists, return error.
  34. //
  35. // If the domain and the path are all empty string, don't change anything.
  36. //
  37. // Returns is the all bind domains.
  38. //
  39. // Examples:
  40. // BindTextdomain("poedit", "local", nil) // bind "poedit" domain
  41. // BindTextdomain("", "", nil) // return all domains
  42. // BindTextdomain("poedit", "", nil) // delete "poedit" domain
  43. // BindTextdomain("", "", nil) // return all domains
  44. //
  45. // Use zip file:
  46. // BindTextdomain("poedit", "local.zip", nil) // bind "poedit" domain
  47. // BindTextdomain("poedit", "local.zip", zipData) // bind "poedit" domain
  48. //
  49. func BindTextdomain(domain, path string, zipData []byte) (domains, paths []string) {
  50. return defaultManager.Bind(domain, path, zipData)
  51. }
  52. // Textdomain sets and retrieves the current message domain.
  53. //
  54. // If the domain is not empty string, set the new domains.
  55. //
  56. // If the domain is empty string, don't change anything.
  57. //
  58. // Returns is the all used domains.
  59. //
  60. // Examples:
  61. // Textdomain("poedit") // set domain: poedit
  62. // Textdomain("") // get domain: return poedit
  63. func Textdomain(domain string) string {
  64. return defaultManager.SetDomain(domain)
  65. }
  66. // Gettext attempt to translate a text string into the user's native language,
  67. // by looking up the translation in a message catalog.
  68. //
  69. // It use the caller's function name as the msgctxt.
  70. //
  71. // Examples:
  72. // func Foo() {
  73. // msg := gettext.Gettext("Hello") // msgctxt is "some/package/name.Foo"
  74. // }
  75. func Gettext(msgid string) string {
  76. return PGettext(callerName(2), msgid)
  77. }
  78. // Getdata attempt to translate a resource file into the user's native language,
  79. // by looking up the translation in a message catalog.
  80. //
  81. // Examples:
  82. // func Foo() {
  83. // Textdomain("hello")
  84. // BindTextdomain("hello", "local.zip", nilOrZipData)
  85. // poems := gettext.Getdata("poems.txt")
  86. // }
  87. func Getdata(name string) []byte {
  88. return defaultManager.Getdata(name)
  89. }
  90. // NGettext attempt to translate a text string into the user's native language,
  91. // by looking up the appropriate plural form of the translation in a message
  92. // catalog.
  93. //
  94. // It use the caller's function name as the msgctxt.
  95. //
  96. // Examples:
  97. // func Foo() {
  98. // msg := gettext.NGettext("%d people", "%d peoples", 2)
  99. // }
  100. func NGettext(msgid, msgidPlural string, n int) string {
  101. return PNGettext(callerName(2), msgid, msgidPlural, n)
  102. }
  103. // PGettext attempt to translate a text string into the user's native language,
  104. // by looking up the translation in a message catalog.
  105. //
  106. // Examples:
  107. // func Foo() {
  108. // msg := gettext.PGettext("gettext-go.example", "Hello") // msgctxt is "gettext-go.example"
  109. // }
  110. func PGettext(msgctxt, msgid string) string {
  111. return PNGettext(msgctxt, msgid, "", 0)
  112. }
  113. // PNGettext attempt to translate a text string into the user's native language,
  114. // by looking up the appropriate plural form of the translation in a message
  115. // catalog.
  116. //
  117. // Examples:
  118. // func Foo() {
  119. // msg := gettext.PNGettext("gettext-go.example", "%d people", "%d peoples", 2)
  120. // }
  121. func PNGettext(msgctxt, msgid, msgidPlural string, n int) string {
  122. return defaultManager.PNGettext(msgctxt, msgid, msgidPlural, n)
  123. }
  124. // DGettext like Gettext(), but looking up the message in the specified domain.
  125. //
  126. // Examples:
  127. // func Foo() {
  128. // msg := gettext.DGettext("poedit", "Hello")
  129. // }
  130. func DGettext(domain, msgid string) string {
  131. return DPGettext(domain, callerName(2), msgid)
  132. }
  133. // DNGettext like NGettext(), but looking up the message in the specified domain.
  134. //
  135. // Examples:
  136. // func Foo() {
  137. // msg := gettext.PNGettext("poedit", "gettext-go.example", "%d people", "%d peoples", 2)
  138. // }
  139. func DNGettext(domain, msgid, msgidPlural string, n int) string {
  140. return DPNGettext(domain, callerName(2), msgid, msgidPlural, n)
  141. }
  142. // DPGettext like PGettext(), but looking up the message in the specified domain.
  143. //
  144. // Examples:
  145. // func Foo() {
  146. // msg := gettext.DPGettext("poedit", "gettext-go.example", "Hello")
  147. // }
  148. func DPGettext(domain, msgctxt, msgid string) string {
  149. return DPNGettext(domain, msgctxt, msgid, "", 0)
  150. }
  151. // DPNGettext like PNGettext(), but looking up the message in the specified domain.
  152. //
  153. // Examples:
  154. // func Foo() {
  155. // msg := gettext.DPNGettext("poedit", "gettext-go.example", "%d people", "%d peoples", 2)
  156. // }
  157. func DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string {
  158. return defaultManager.DPNGettext(domain, msgctxt, msgid, msgidPlural, n)
  159. }
  160. // DGetdata like Getdata(), but looking up the resource in the specified domain.
  161. //
  162. // Examples:
  163. // func Foo() {
  164. // msg := gettext.DGetdata("hello", "poems.txt")
  165. // }
  166. func DGetdata(domain, name string) []byte {
  167. return defaultManager.DGetdata(domain, name)
  168. }