msgUtil.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package meta
  15. import (
  16. "bytes"
  17. "os"
  18. "path"
  19. "gopkg.in/ini.v1"
  20. kconf "github.com/lf-edge/ekuiper/internal/conf"
  21. )
  22. var gUimsg map[string]*ini.File
  23. func getMsg(language, section, key string) string {
  24. language += ".ini"
  25. if conf, ok := gUimsg[language]; ok {
  26. s := conf.Section(section)
  27. if s != nil {
  28. return s.Key(key).String()
  29. }
  30. }
  31. return ""
  32. }
  33. func ReadUiMsgDir() error {
  34. gUimsg = make(map[string]*ini.File)
  35. confDir, err := kconf.GetConfLoc()
  36. if nil != err {
  37. return err
  38. }
  39. dir := path.Join(confDir, "multilingual")
  40. dirEntries, err := os.ReadDir(dir)
  41. if nil != err {
  42. return err
  43. }
  44. for _, entry := range dirEntries {
  45. fName := entry.Name()
  46. kconf.Log.Infof("uiMsg file : %s", fName)
  47. fPath := path.Join(dir, fName)
  48. if conf, err := ini.Load(fPath); nil != err {
  49. return err
  50. } else {
  51. gUimsg[fName] = conf
  52. }
  53. }
  54. return nil
  55. }
  56. func ConstructJsonArray(jsonByteItems []fileContent) bytes.Buffer {
  57. var buf bytes.Buffer
  58. length := len(jsonByteItems)
  59. if length == 0 {
  60. buf.Write([]byte("[]"))
  61. return buf
  62. }
  63. buf.Write([]byte("["))
  64. buf.Write(jsonByteItems[0])
  65. for i := 1; i < length; i++ {
  66. buf.Write([]byte(","))
  67. buf.Write(jsonByteItems[i])
  68. }
  69. buf.Write([]byte("]"))
  70. return buf
  71. }