Skip to content

Commit 21ee013

Browse files
author
Konstantinos Bairaktaris
committed
Make language mappings deterministic
Consider this scenario: Someone has in their config file: ```ini [main] lang_map = it: it-IT [RESOURCE_ID] lang_map = it: it_IT ``` Before we created a local-to-remote map that looked like this: ```json {"it-IT": "it", "it_IT", "it"} ``` And then, to create the remote-to-local map, we reversed it. The problem was that, since both values are the same, the key that ended up being selected was random, so it could either be: ```json {"it": "it-IT"} ``` or ```json {"it": "it_IT"} ``` To fix the issue, we now create remote-to-local first, going over the global configuration first and the resource-level configuration second so that the resource-level will prevail and thus take preference, and then we create the local-to-remote by reversing. So, First, we will consume the global configuration: ```json {"it": "it-IT"} ``` Then, as the resource-level configuration is consumed, it will replace the old key: ```json {"it": "it_IT"} ``` And then, the local-to-remote will end up being: ```json {"it_IT": "it"} ``` This is the deterministic and the intended behaviour, since resource-level configuration should take precedence.
1 parent 8f4f7ef commit 21ee013

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

internal/txlib/pull.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,11 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
169169
}
170170
sendMessage("Getting info", false)
171171

172-
localToRemoteLanguageMappings := makeLocalToRemoteLanguageMappings(
172+
remoteToLocalLanguageMappings := makeRemoteToLocalLanguageMappings(
173173
*cfg,
174174
*cfgResource,
175175
)
176-
remoteToLocalLanguageMappings := makeRemoteToLocalLanguageMappings(
177-
localToRemoteLanguageMappings,
178-
)
176+
localToRemoteLanguageMappings := reverseMap(remoteToLocalLanguageMappings)
179177

180178
var err error
181179
var resource *jsonapi.Resource

internal/txlib/push.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,12 @@ func (task *ResourcePushTask) Run(send func(string), abort func()) {
493493
}
494494
}
495495
if args.Translation { // -t flag is set
496-
localToRemoteLanguageMappings := makeLocalToRemoteLanguageMappings(
497-
*cfg,
498-
*cfgResource,
499-
)
496+
localToRemoteLanguageMappings := reverseMap(
497+
makeRemoteToLocalLanguageMappings(
498+
*cfg,
499+
*cfgResource,
500+
),
501+
)
500502
overrides := cfgResource.Overrides
501503

502504
sendMessage("Fetching remote languages", false)

internal/txlib/utils.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func stringSliceContains(haystack []string, needle string) bool {
124124
return false
125125
}
126126

127-
func makeLocalToRemoteLanguageMappings(
127+
func makeRemoteToLocalLanguageMappings(
128128
cfg config.Config, cfgResource config.Resource,
129129
) map[string]string {
130130
// In the configuration, the language mappings are "remote code -> local
@@ -133,21 +133,19 @@ func makeLocalToRemoteLanguageMappings(
133133
// reverse the maps
134134

135135
result := make(map[string]string)
136-
for key, value := range cfg.Local.LanguageMappings {
137-
result[value] = key
136+
for transifexLanguageCode, localLanguageCode := range cfg.Local.LanguageMappings {
137+
result[transifexLanguageCode] = localLanguageCode
138138
}
139-
for key, value := range cfgResource.LanguageMappings {
139+
for transifexLanguageCode, localLanguageCode := range cfgResource.LanguageMappings {
140140
// Resource language mappings overwrite "global" language mappings
141-
result[value] = key
141+
result[transifexLanguageCode] = localLanguageCode
142142
}
143143
return result
144144
}
145145

146-
func makeRemoteToLocalLanguageMappings(
147-
localToRemoteLanguageMappings map[string]string,
148-
) map[string]string {
146+
func reverseMap(remoteToLocalLanguageMappings map[string]string) map[string]string {
149147
result := make(map[string]string)
150-
for key, value := range localToRemoteLanguageMappings {
148+
for key, value := range remoteToLocalLanguageMappings {
151149
result[value] = key
152150
}
153151
return result

0 commit comments

Comments
 (0)