Skip to content

Commit

Permalink
Make language mappings deterministic
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kbairak committed Sep 4, 2024
1 parent 8f4f7ef commit 847d44a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
6 changes: 2 additions & 4 deletions internal/txlib/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,11 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
}
sendMessage("Getting info", false)

localToRemoteLanguageMappings := makeLocalToRemoteLanguageMappings(
remoteToLocalLanguageMappings := makeRemoteToLocalLanguageMappings(
*cfg,
*cfgResource,
)
remoteToLocalLanguageMappings := makeRemoteToLocalLanguageMappings(
localToRemoteLanguageMappings,
)
localToRemoteLanguageMappings := reverseMap(remoteToLocalLanguageMappings)

var err error
var resource *jsonapi.Resource
Expand Down
8 changes: 5 additions & 3 deletions internal/txlib/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,9 +493,11 @@ func (task *ResourcePushTask) Run(send func(string), abort func()) {
}
}
if args.Translation { // -t flag is set
localToRemoteLanguageMappings := makeLocalToRemoteLanguageMappings(
*cfg,
*cfgResource,
localToRemoteLanguageMappings := reverseMap(
makeRemoteToLocalLanguageMappings(
*cfg,
*cfgResource,
),
)
overrides := cfgResource.Overrides

Expand Down
22 changes: 10 additions & 12 deletions internal/txlib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func stringSliceContains(haystack []string, needle string) bool {
return false
}

func makeLocalToRemoteLanguageMappings(
func makeRemoteToLocalLanguageMappings(
cfg config.Config, cfgResource config.Resource,
) map[string]string {
// In the configuration, the language mappings are "remote code -> local
Expand All @@ -133,24 +133,22 @@ func makeLocalToRemoteLanguageMappings(
// reverse the maps

result := make(map[string]string)
for key, value := range cfg.Local.LanguageMappings {
result[value] = key
for transifexLanguageCode, localLanguageCode := range cfg.Local.LanguageMappings {
result[transifexLanguageCode] = localLanguageCode
}
for key, value := range cfgResource.LanguageMappings {
for transifexLanguageCode, localLanguageCode := range cfgResource.LanguageMappings {
// Resource language mappings overwrite "global" language mappings
result[value] = key
result[transifexLanguageCode] = localLanguageCode
}
return result
}

func makeRemoteToLocalLanguageMappings(
localToRemoteLanguageMappings map[string]string,
) map[string]string {
result := make(map[string]string)
for key, value := range localToRemoteLanguageMappings {
result[value] = key
func reverseMap(src map[string]string) map[string]string {
dst := make(map[string]string)
for key, value := range src {
dst[value] = key
}
return result
return dst
}

/*
Expand Down

0 comments on commit 847d44a

Please sign in to comment.