From 874c6269ec695d554848b04b547368e05147ddd3 Mon Sep 17 00:00:00 2001 From: Konstantinos Pittas Date: Fri, 27 Jan 2023 01:20:04 +0200 Subject: [PATCH] [MM-49265] Update translations when config is changed (#21987) * update translations when config is changed * wrap and return the error message * use the configs directly instead of diff --- api4/config.go | 10 ++++++++++ api4/team_test.go | 24 ++++++++++++++++++++++++ i18n/en.json | 4 ++++ 3 files changed, 38 insertions(+) diff --git a/api4/config.go b/api4/config.go index 5f620e14d5..3d842f0319 100644 --- a/api4/config.go +++ b/api4/config.go @@ -13,6 +13,7 @@ import ( "github.com/mattermost/mattermost-server/v6/audit" "github.com/mattermost/mattermost-server/v6/config" "github.com/mattermost/mattermost-server/v6/model" + "github.com/mattermost/mattermost-server/v6/shared/i18n" "github.com/mattermost/mattermost-server/v6/shared/mlog" "github.com/mattermost/mattermost-server/v6/utils" ) @@ -184,6 +185,15 @@ func updateConfig(c *Context, w http.ResponseWriter, r *http.Request) { return } + // If the config for default server locale has changed, reinitialize the server's translations. + if oldCfg.LocalizationSettings.DefaultServerLocale != newCfg.LocalizationSettings.DefaultServerLocale { + s := newCfg.LocalizationSettings + if err = i18n.InitTranslations(*s.DefaultServerLocale, *s.DefaultClientLocale); err != nil { + c.Err = model.NewAppError("updateConfig", "api.config.update_config.translations.app_error", nil, "", http.StatusInternalServerError).Wrap(err) + return + } + } + diffs, err := config.Diff(oldCfg, newCfg) if err != nil { c.Err = model.NewAppError("updateConfig", "api.config.update_config.diff.app_error", nil, "", http.StatusInternalServerError).Wrap(err) diff --git a/api4/team_test.go b/api4/team_test.go index 4ffe1db335..d744042230 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -93,6 +93,30 @@ func TestCreateTeam(t *testing.T) { CheckForbiddenStatus(t, resp) }) + t.Run("should take under consideration the server language when creating a new team", func(t *testing.T) { + c := th.SystemAdminClient + cfg, _, err := c.GetConfig() + require.NoError(t, err) + newServerLang := "de" + cfg.LocalizationSettings.DefaultServerLocale = &newServerLang + translateFunc := i18n.GetUserTranslations(newServerLang) + + _, _, err = c.UpdateConfig(cfg) + require.NoError(t, err) + + team := th.CreateTeamWithClient(c) + channels, _, err := c.GetPublicChannelsForTeam(team.Id, 0, 1000, "") + require.NoError(t, err) + for _, ch := range channels { + if ch.Name == "off-topic" { + require.Equal(t, translateFunc("api.channel.create_default_channels.off_topic"), ch.DisplayName) + } + if ch.Name == "town-square" { + require.Equal(t, translateFunc("api.channel.create_default_channels.town_square"), ch.DisplayName) + } + } + }) + t.Run("cloud limit reached returns 400", func(t *testing.T) { th.App.Srv().SetLicense(model.NewTestLicense("cloud")) diff --git a/i18n/en.json b/i18n/en.json index eaff8f065a..3bee6fc081 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1534,6 +1534,10 @@ "id": "api.config.update_config.restricted_merge.app_error", "translation": "Failed to merge given config." }, + { + "id": "api.config.update_config.translations.app_error", + "translation": "Failed to update server translations." + }, { "id": "api.context.404.app_error", "translation": "Sorry, we could not find the page."