From 847d44ab47761fd348c4db33bdb6b3a0cbbc803a Mon Sep 17 00:00:00 2001 From: Konstantinos Bairaktaris Date: Wed, 4 Sep 2024 15:23:50 +0300 Subject: [PATCH] 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. --- internal/txlib/pull.go | 6 ++---- internal/txlib/push.go | 8 +++++--- internal/txlib/utils.go | 22 ++++++++++------------ 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/internal/txlib/pull.go b/internal/txlib/pull.go index 13d8c27..da9299a 100644 --- a/internal/txlib/pull.go +++ b/internal/txlib/pull.go @@ -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 diff --git a/internal/txlib/push.go b/internal/txlib/push.go index 7704357..f9d4ad2 100644 --- a/internal/txlib/push.go +++ b/internal/txlib/push.go @@ -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 diff --git a/internal/txlib/utils.go b/internal/txlib/utils.go index bd2f792..38a132d 100644 --- a/internal/txlib/utils.go +++ b/internal/txlib/utils.go @@ -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 @@ -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 } /*