56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package catalog
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
|
||
|
|
"github.com/descrybe/descrybe-v2/apps/api/internal/company"
|
||
|
|
"github.com/google/uuid"
|
||
|
|
)
|
||
|
|
|
||
|
|
type importLanguageContextKey struct{}
|
||
|
|
|
||
|
|
// WithImportLanguage marks the language of a supplier import. An empty code means
|
||
|
|
// the company primary language. The HTTP layer validates explicit codes.
|
||
|
|
func WithImportLanguage(ctx context.Context, lang string) context.Context {
|
||
|
|
return context.WithValue(ctx, importLanguageContextKey{}, lang)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Service) importLanguage(ctx context.Context, companyID uuid.UUID) (string, bool) {
|
||
|
|
primary := company.LoadLanguage(ctx, s.Pool, companyID)
|
||
|
|
lang, _ := ctx.Value(importLanguageContextKey{}).(string)
|
||
|
|
if lang == "" {
|
||
|
|
lang = primary
|
||
|
|
}
|
||
|
|
return lang, lang == primary
|
||
|
|
}
|
||
|
|
|
||
|
|
// localizeCatalogImportNames changes presentation only; it never overwrites
|
||
|
|
// the primary-language columns in storage. The full translations map remains
|
||
|
|
// present in API responses for clients that need several languages.
|
||
|
|
func localizeCatalogImportNames(ctx context.Context, items []map[string]any) {
|
||
|
|
lang, _ := ctx.Value(importLanguageContextKey{}).(string)
|
||
|
|
if lang == "" {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
for _, item := range items {
|
||
|
|
var all map[string]map[string]string
|
||
|
|
switch raw := item["localized_fields"].(type) {
|
||
|
|
case []byte:
|
||
|
|
_ = json.Unmarshal(raw, &all)
|
||
|
|
case string:
|
||
|
|
_ = json.Unmarshal([]byte(raw), &all)
|
||
|
|
default:
|
||
|
|
b, _ := json.Marshal(raw)
|
||
|
|
_ = json.Unmarshal(b, &all)
|
||
|
|
}
|
||
|
|
if fields := all[lang]; fields != nil {
|
||
|
|
if val := fields["name"]; val != "" {
|
||
|
|
item["name"] = val
|
||
|
|
}
|
||
|
|
if val := fields["description"]; val != "" {
|
||
|
|
item["description"] = val
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|