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.

94 lines
2.6KB

  1. /*
  2. Package gotext implements GNU gettext utilities.
  3. */
  4. package gotext
  5. // Global environment variables
  6. var (
  7. // Default domain to look at when no domain is specified. Used by package level functions.
  8. domain = "default"
  9. // Language set.
  10. language = "en_US"
  11. // Path to library directory where all locale directories and translation files are.
  12. library = "/tmp"
  13. // Storage for package level methods
  14. storage *Locale
  15. )
  16. // loadStorage creates a new Locale object at package level based on the Global variables settings.
  17. // It's called automatically when trying to use Get or GetD methods.
  18. func loadStorage(force bool) {
  19. if storage == nil || force {
  20. storage = NewLocale(library, language)
  21. }
  22. if _, ok := storage.domains[domain]; !ok || force {
  23. storage.AddDomain(domain)
  24. }
  25. }
  26. // GetDomain is the domain getter for the package configuration
  27. func GetDomain() string {
  28. return domain
  29. }
  30. // SetDomain sets the name for the domain to be used at package level.
  31. // It reloads the corresponding translation file.
  32. func SetDomain(dom string) {
  33. domain = dom
  34. loadStorage(true)
  35. }
  36. // GetLanguage is the language getter for the package configuration
  37. func GetLanguage() string {
  38. return language
  39. }
  40. // SetLanguage sets the language code to be used at package level.
  41. // It reloads the corresponding translation file.
  42. func SetLanguage(lang string) {
  43. language = lang
  44. loadStorage(true)
  45. }
  46. // GetLibrary is the library getter for the package configuration
  47. func GetLibrary() string {
  48. return library
  49. }
  50. // SetLibrary sets the root path for the loale directories and files to be used at package level.
  51. // It reloads the corresponding translation file.
  52. func SetLibrary(lib string) {
  53. library = lib
  54. loadStorage(true)
  55. }
  56. // Configure sets all configuration variables to be used at package level and reloads the corresponding translation file.
  57. // It receives the library path, language code and domain name.
  58. // This function is recommended to be used when changing more than one setting,
  59. // as using each setter will introduce a I/O overhead because the translation file will be loaded after each set.
  60. func Configure(lib, lang, dom string) {
  61. library = lib
  62. language = lang
  63. domain = dom
  64. loadStorage(true)
  65. }
  66. // Get uses the default domain globally set to return the corresponding translation of a given string.
  67. func Get(str string, vars ...interface{}) string {
  68. return GetD(domain, str, vars...)
  69. }
  70. // GetD returns the corresponding translation in the given domain for a given string.
  71. func GetD(dom, str string, vars ...interface{}) string {
  72. // Try to load default package Locale storage
  73. loadStorage(false)
  74. // Return translation
  75. return storage.GetD(dom, str, vars...)
  76. }