i18nresource.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 Yunion
  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 models
  15. import (
  16. "context"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. )
  24. type SI18nResourceBase struct {
  25. }
  26. type SI18nResourceBaseManager struct {
  27. }
  28. func (man *SI18nResourceBaseManager) getSModelI18nTable(model db.IModel, table cloudprovider.SModelI18nTable) *db.SModelI18nTable {
  29. entries := make([]db.IModelI18nEntry, 0)
  30. for k, _ := range table {
  31. entries = append(entries, db.NewSModelI18nEntry(k, model, table[k]))
  32. }
  33. return db.NewSModelI18nTable(entries)
  34. }
  35. func (man *SI18nResourceBaseManager) SyncI18ns(ctx context.Context, userCred mcclient.TokenCredential, model db.IModel, table cloudprovider.SModelI18nTable) error {
  36. itable := man.getSModelI18nTable(model, table)
  37. err := db.I18nManager.SyncI18ns(ctx, userCred, model, itable)
  38. if err != nil {
  39. log.Errorf("SyncI18ns for %s %s result: %s", model.Keyword(), model.GetId(), err)
  40. return errors.Wrap(err, "SyncI18ns")
  41. }
  42. return nil
  43. }
  44. /*
  45. func (self *SI18nResourceBase) RemoveI18ns(ctx context.Context, userCred mcclient.TokenCredential, model db.IModel) error {
  46. err := db.I18nManager.RemoveI18ns(ctx, userCred, model)
  47. if err != nil {
  48. return errors.Wrap(err, "RemoveI18ns")
  49. }
  50. return nil
  51. }
  52. */
  53. func (self *SI18nResourceBase) GetModelI18N(ctx context.Context, model db.IModel) *jsonutils.JSONDict {
  54. entries, err := db.I18nManager.GetModelI18N(ctx, model)
  55. if err != nil {
  56. log.Errorf("GetModelI18N error %s", err)
  57. return nil
  58. }
  59. _i18n := jsonutils.NewDict()
  60. for i := range entries {
  61. _i18n.Set(entries[i].KeyName, jsonutils.NewString(entries[i].Lookup(ctx)))
  62. }
  63. return _i18n
  64. }
  65. func (self *SI18nResourceBase) GetModelKeyI18N(ctx context.Context, model db.IModel, keyName string) (string, bool) {
  66. entries, err := db.I18nManager.GetModelKeyI18N(ctx, model, keyName)
  67. if err != nil {
  68. log.Errorf("GetModelKeyI18N error %s", err)
  69. return "", false
  70. }
  71. if len(entries) > 0 {
  72. return entries[0].Lookup(ctx), true
  73. }
  74. return "", false
  75. }