gi18n.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gi18n implements internationalization and localization.
  7. package gi18n
  8. var (
  9. // defaultManager is the default i18n instance for package functions.
  10. defaultManager = Instance()
  11. )
  12. // SetPath sets the directory path storing i18n files.
  13. func SetPath(path string) error {
  14. return defaultManager.SetPath(path)
  15. }
  16. // SetLanguage sets the language for translator.
  17. func SetLanguage(language string) {
  18. defaultManager.SetLanguage(language)
  19. }
  20. // SetDelimiters sets the delimiters for translator.
  21. func SetDelimiters(left, right string) {
  22. defaultManager.SetDelimiters(left, right)
  23. }
  24. // T is alias of Translate for convenience.
  25. func T(content string, language ...string) string {
  26. return defaultManager.T(content, language...)
  27. }
  28. // Tf is alias of TranslateFormat for convenience.
  29. func Tf(format string, values ...interface{}) string {
  30. return defaultManager.TranslateFormat(format, values...)
  31. }
  32. // Tfl is alias of TranslateFormatLang for convenience.
  33. func Tfl(language string, format string, values ...interface{}) string {
  34. return defaultManager.TranslateFormatLang(language, format, values...)
  35. }
  36. // TranslateFormat translates, formats and returns the <format> with configured language
  37. // and given <values>.
  38. func TranslateFormat(format string, values ...interface{}) string {
  39. return defaultManager.TranslateFormat(format, values...)
  40. }
  41. // TranslateFormatLang translates, formats and returns the <format> with configured language
  42. // and given <values>. The parameter <language> specifies custom translation language ignoring
  43. // configured language. If <language> is given empty string, it uses the default configured
  44. // language for the translation.
  45. func TranslateFormatLang(language string, format string, values ...interface{}) string {
  46. return defaultManager.TranslateFormatLang(format, language, values...)
  47. }
  48. // Translate translates <content> with configured language and returns the translated content.
  49. // The parameter <language> specifies custom translation language ignoring configured language.
  50. func Translate(content string, language ...string) string {
  51. return defaultManager.Translate(content, language...)
  52. }
  53. // GetValue retrieves and returns the configured content for given key and specified language.
  54. // It returns an empty string if not found.
  55. func GetContent(key string, language ...string) string {
  56. return defaultManager.GetContent(key, language...)
  57. }