-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.v
More file actions
117 lines (100 loc) · 2.7 KB
/
Copy pathbundle.v
File metadata and controls
117 lines (100 loc) · 2.7 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
module i18n
import os
pub struct Bundle {
default_tag LanguageTag
mut:
tags []LanguageTag
templates map[string]MessageTemplate
}
enum BundleMessageChangeKind {
insert
delete
}
struct BundleMessageChange {
key string
kind BundleMessageChangeKind
template MessageTemplate
}
pub fn new_bundle(default_language string) !Bundle {
default_tag := parse_language_tag(default_language)!
return Bundle{
default_tag: default_tag
tags: [default_tag]
templates: map[string]MessageTemplate{}
}
}
pub fn (bundle Bundle) default_language() LanguageTag {
return bundle.default_tag
}
pub fn (bundle Bundle) language_tags() []LanguageTag {
return bundle.tags.clone()
}
pub fn (mut bundle Bundle) add_messages(language string, messages []Message) ! {
tag := parse_language_tag(language)!
key := tag.key()
is_new_language := !bundle.has_language_key(key)
mut changes := []BundleMessageChange{}
for message in messages {
if message.id == '' {
return error('message id cannot be empty')
}
template_key := bundle_template_key(key, message.id)
if !message_has_plural_text(message) {
changes << BundleMessageChange{
key: template_key
kind: .delete
}
continue
}
changes << BundleMessageChange{
key: template_key
kind: .insert
template: new_message_template(message)!
}
}
if is_new_language {
bundle.tags << tag
}
for change in changes {
match change.kind {
.insert {
bundle.templates[change.key] = change.template
}
.delete {
bundle.templates.delete(change.key)
}
}
}
}
pub fn (mut bundle Bundle) load_message_file(path string) !MessageFile {
data := os.read_file(path)!
return bundle.parse_message_file_bytes(data.bytes(), path)!
}
pub fn (mut bundle Bundle) parse_message_file_bytes(data []u8, path string) !MessageFile {
message_file := parse_message_file_bytes(data, path)!
bundle.add_messages(message_file.tag.str(), message_file.messages)!
return message_file
}
fn (bundle Bundle) template_for(tag LanguageTag, id string) !MessageTemplate {
return bundle.template_for_key(tag.key(), id)!
}
fn (bundle Bundle) template_for_language(language string, id string) !MessageTemplate {
tag := parse_language_tag(language)!
return bundle.template_for(tag, id)!
}
fn (bundle Bundle) template_for_key(language_key string, id string) !MessageTemplate {
return bundle.templates[bundle_template_key(language_key, id)] or {
error('message "${id}" not found for language "${language_key}"')
}
}
fn (bundle Bundle) has_language_key(key string) bool {
for tag in bundle.tags {
if tag.key() == key {
return true
}
}
return false
}
fn bundle_template_key(language_key string, id string) string {
return language_key + '\0' + id
}