From 21ee013729415eee4d4969b3373af1a3ba250676 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 | 10 ++++++---- internal/txlib/utils.go | 16 +++++++--------- 3 files changed, 15 insertions(+), 17 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..886210d 100644 --- a/internal/txlib/push.go +++ b/internal/txlib/push.go @@ -493,10 +493,12 @@ 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 sendMessage("Fetching remote languages", false) diff --git a/internal/txlib/utils.go b/internal/txlib/utils.go index bd2f792..9eaee0c 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,21 +133,19 @@ 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 { +func reverseMap(remoteToLocalLanguageMappings map[string]string) map[string]string { result := make(map[string]string) - for key, value := range localToRemoteLanguageMappings { + for key, value := range remoteToLocalLanguageMappings { result[value] = key } return result