-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
89 lines (79 loc) · 2.07 KB
/
file.go
File metadata and controls
89 lines (79 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package i18n
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
yaml "gopkg.in/yaml.v3"
)
// File is a Translator that loads its translations from a file.
// Supported formats: JSON (.json) and YAML (.yaml, .yml).
type File struct {
m *Map
}
// NewFileFrom loads translations from the given source file and returns a File translator.
// The file format is detected by extension: .json, .yaml, .yml
func NewFileFrom(path string) (*File, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
ext := strings.ToLower(filepath.Ext(path))
// Use a generic map to support either string values or plural objects
translations := make(map[string]interface{})
switch ext {
case ".json":
if err := json.Unmarshal(data, &translations); err != nil {
return nil, fmt.Errorf("parse json: %w", err)
}
case ".yaml", ".yml":
if err := yaml.Unmarshal(data, &translations); err != nil {
return nil, fmt.Errorf("parse yaml: %w", err)
}
default:
return nil, fmt.Errorf("unsupported file extension: %s", ext)
}
m := NewMap()
for k, v := range translations {
switch tv := v.(type) {
case string:
m.Add(k, tv)
case map[string]interface{}:
singular := ""
plural := ""
if s, ok := tv["singular"].(string); ok {
singular = s
}
if s, ok := tv["s"].(string); ok {
singular = s
}
if p, ok := tv["plural"].(string); ok {
plural = p
}
if p, ok := tv["p"].(string); ok {
plural = p
}
if singular != "" || plural != "" {
if plural == "" {
// allow singular-only objects
m.Add(k, singular)
} else {
m.AddPlural(k, singular, plural)
}
}
default:
// Unsupported type, ignore silently
}
}
return &File{m: m}, nil
}
func (f *File) Translate(key string) string {
return f.m.Translate(key)
}
func (f *File) TranslateWith(key string, args map[string]interface{}) string {
return f.m.TranslateWith(key, args)
}
func (f *File) TranslatePlural(key string, number int64, args map[string]interface{}) string {
return f.m.TranslatePlural(key, number, args)
}