gi18n.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright GoFrame Author(https://goframe.org). 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. import "context"
  9. // SetPath sets the directory path storing i18n files.
  10. func SetPath(path string) error {
  11. return Instance().SetPath(path)
  12. }
  13. // SetLanguage sets the language for translator.
  14. func SetLanguage(language string) {
  15. Instance().SetLanguage(language)
  16. }
  17. // SetDelimiters sets the delimiters for translator.
  18. func SetDelimiters(left, right string) {
  19. Instance().SetDelimiters(left, right)
  20. }
  21. // T is alias of Translate for convenience.
  22. func T(ctx context.Context, content string) string {
  23. return Instance().T(ctx, content)
  24. }
  25. // Tf is alias of TranslateFormat for convenience.
  26. func Tf(ctx context.Context, format string, values ...interface{}) string {
  27. return Instance().TranslateFormat(ctx, format, values...)
  28. }
  29. // TranslateFormat translates, formats and returns the <format> with configured language
  30. // and given <values>.
  31. func TranslateFormat(ctx context.Context, format string, values ...interface{}) string {
  32. return Instance().TranslateFormat(ctx, format, values...)
  33. }
  34. // Translate translates <content> with configured language and returns the translated content.
  35. func Translate(ctx context.Context, content string) string {
  36. return Instance().Translate(ctx, content)
  37. }
  38. // GetContent retrieves and returns the configured content for given key and specified language.
  39. // It returns an empty string if not found.
  40. func GetContent(ctx context.Context, key string) string {
  41. return Instance().GetContent(ctx, key)
  42. }