-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguages.go
More file actions
150 lines (131 loc) · 3.27 KB
/
languages.go
File metadata and controls
150 lines (131 loc) · 3.27 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 WoozyMasta
// Source: github.com/woozymasta/stringtable
package stringtable
import (
"path/filepath"
"slices"
"strings"
)
// DefaultLanguages is the default DayZ language order.
var DefaultLanguages = []string{
"english",
"czech",
"german",
"russian",
"polish",
"hungarian",
"italian",
"spanish",
"french",
"chinese",
"japanese",
"portuguese",
"chinesesimp",
}
// languageCodes maps DayZ language names to stable language codes.
var languageCodes = map[string]string{
"english": "en",
"czech": "cs",
"german": "de",
"russian": "ru",
"polish": "pl",
"hungarian": "hu",
"italian": "it",
"spanish": "es",
"french": "fr",
"chinese": "zh-Hant",
"japanese": "ja",
"portuguese": "pt",
"chinesesimp": "zh-Hans",
}
// languageNamesByCode maps stable language codes back to DayZ language names.
var languageNamesByCode = map[string]string{
"en": "english",
"cs": "czech",
"de": "german",
"ru": "russian",
"pl": "polish",
"hu": "hungarian",
"it": "italian",
"es": "spanish",
"fr": "french",
"zh-Hant": "chinese",
"ja": "japanese",
"pt": "portuguese",
"zh-Hans": "chinesesimp",
}
// ParseLanguages parses comma-separated language list.
func ParseLanguages(value string) []string {
if strings.TrimSpace(value) == "" {
return slices.Clone(DefaultLanguages)
}
parts := strings.Split(value, ",")
out := make([]string, 0, len(parts))
seen := make(map[string]struct{}, len(parts))
for _, part := range parts {
lang := strings.TrimSpace(part)
if lang == "" {
continue
}
if _, ok := seen[lang]; ok {
continue
}
seen[lang] = struct{}{}
out = append(out, lang)
}
return out
}
// LanguageCode returns language code for DayZ language name.
func LanguageCode(language string) (string, bool) {
key := strings.ToLower(strings.TrimSpace(language))
if key == "" {
return "", false
}
code, ok := languageCodes[key]
return code, ok
}
// LanguageNameFromCode returns DayZ language name for language code.
func LanguageNameFromCode(code string) (string, bool) {
language, ok := languageNamesByCode[strings.TrimSpace(code)]
return language, ok
}
// ContainsLanguage reports whether language exists in list.
func ContainsLanguage(list []string, language string) bool {
return slices.Contains(list, language)
}
// ExtractLanguageName extracts language from "/path/lang.po" path.
func ExtractLanguageName(path string) string {
base := filepath.Base(path)
return strings.TrimSuffix(base, filepath.Ext(base))
}
// SelectLanguages returns deterministic language list using include/exclude.
func SelectLanguages(available, include, exclude []string) []string {
base := include
if len(base) == 0 {
base = available
}
if len(base) == 0 {
base = DefaultLanguages
}
excluded := make(map[string]struct{}, len(exclude))
for _, language := range exclude {
excluded[language] = struct{}{}
}
out := make([]string, 0, len(base))
seen := make(map[string]struct{}, len(base))
for _, language := range base {
if language == "" {
continue
}
if _, ok := seen[language]; ok {
continue
}
if _, ok := excluded[language]; ok {
continue
}
seen[language] = struct{}{}
out = append(out, language)
}
return out
}