i18n.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package i18n
  14. import (
  15. "archive/zip"
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "os"
  20. "strings"
  21. "k8s.io/kubernetes/pkg/kubectl/generated"
  22. "github.com/chai2010/gettext-go/gettext"
  23. "k8s.io/klog"
  24. )
  25. var knownTranslations = map[string][]string{
  26. "kubectl": {
  27. "default",
  28. "en_US",
  29. "fr_FR",
  30. "zh_CN",
  31. "ja_JP",
  32. "zh_TW",
  33. "it_IT",
  34. "de_DE",
  35. "ko_KR",
  36. },
  37. // only used for unit tests.
  38. "test": {
  39. "default",
  40. "en_US",
  41. },
  42. }
  43. func loadSystemLanguage() string {
  44. // Implements the following locale priority order: LC_ALL, LC_MESSAGES, LANG
  45. // Similarly to: https://www.gnu.org/software/gettext/manual/html_node/Locale-Environment-Variables.html
  46. langStr := os.Getenv("LC_ALL")
  47. if langStr == "" {
  48. langStr = os.Getenv("LC_MESSAGES")
  49. }
  50. if langStr == "" {
  51. langStr = os.Getenv("LANG")
  52. }
  53. if langStr == "" {
  54. klog.V(3).Infof("Couldn't find the LC_ALL, LC_MESSAGES or LANG environment variables, defaulting to en_US")
  55. return "default"
  56. }
  57. pieces := strings.Split(langStr, ".")
  58. if len(pieces) != 2 {
  59. klog.V(3).Infof("Unexpected system language (%s), defaulting to en_US", langStr)
  60. return "default"
  61. }
  62. return pieces[0]
  63. }
  64. func findLanguage(root string, getLanguageFn func() string) string {
  65. langStr := getLanguageFn()
  66. translations := knownTranslations[root]
  67. if translations != nil {
  68. for ix := range translations {
  69. if translations[ix] == langStr {
  70. return langStr
  71. }
  72. }
  73. }
  74. klog.V(3).Infof("Couldn't find translations for %s, using default", langStr)
  75. return "default"
  76. }
  77. // LoadTranslations loads translation files. getLanguageFn should return a language
  78. // string (e.g. 'en-US'). If getLanguageFn is nil, then the loadSystemLanguage function
  79. // is used, which uses the 'LANG' environment variable.
  80. func LoadTranslations(root string, getLanguageFn func() string) error {
  81. if getLanguageFn == nil {
  82. getLanguageFn = loadSystemLanguage
  83. }
  84. langStr := findLanguage(root, getLanguageFn)
  85. translationFiles := []string{
  86. fmt.Sprintf("%s/%s/LC_MESSAGES/k8s.po", root, langStr),
  87. fmt.Sprintf("%s/%s/LC_MESSAGES/k8s.mo", root, langStr),
  88. }
  89. klog.V(3).Infof("Setting language to %s", langStr)
  90. // TODO: list the directory and load all files.
  91. buf := new(bytes.Buffer)
  92. w := zip.NewWriter(buf)
  93. // Make sure to check the error on Close.
  94. for _, file := range translationFiles {
  95. filename := "translations/" + file
  96. f, err := w.Create(file)
  97. if err != nil {
  98. return err
  99. }
  100. data, err := generated.Asset(filename)
  101. if err != nil {
  102. return err
  103. }
  104. if _, err := f.Write(data); err != nil {
  105. return nil
  106. }
  107. }
  108. if err := w.Close(); err != nil {
  109. return err
  110. }
  111. gettext.BindTextdomain("k8s", root+".zip", buf.Bytes())
  112. gettext.Textdomain("k8s")
  113. gettext.SetLocale(langStr)
  114. return nil
  115. }
  116. // T translates a string, possibly substituting arguments into it along
  117. // the way. If len(args) is > 0, args1 is assumed to be the plural value
  118. // and plural translation is used.
  119. func T(defaultValue string, args ...int) string {
  120. if len(args) == 0 {
  121. return gettext.PGettext("", defaultValue)
  122. }
  123. return fmt.Sprintf(gettext.PNGettext("", defaultValue, defaultValue+".plural", args[0]),
  124. args[0])
  125. }
  126. // Errorf produces an error with a translated error string.
  127. // Substitution is performed via the `T` function above, following
  128. // the same rules.
  129. func Errorf(defaultValue string, args ...int) error {
  130. return errors.New(T(defaultValue, args...))
  131. }