From ec7a273550a1aaf31e80abc90c99d06a94664b5d Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Fri, 27 May 2016 09:01:35 -0400 Subject: [PATCH] Revert "PLT-1800 Load server side locale from the config.json" (#3133) --- api/apitestlib.go | 4 +- api/user.go | 1 - config/config.json | 5 - i18n/{pt-BR.json => pt.json} | 0 mattermost.go | 2 +- model/config.go | 48 ++---- model/user.go | 2 + store/sql_store_test.go | 2 +- utils/config.go | 3 +- utils/config_test.go | 2 +- utils/i18n.go | 32 ++-- web/web_test.go | 2 +- webapp/actions/global_actions.jsx | 18 +-- .../admin_console/admin_sidebar.jsx | 9 -- .../admin_console/localization_settings.jsx | 145 ------------------ .../admin_console/multiselect_settings.jsx | 80 ---------- webapp/components/root.jsx | 39 ++--- .../user_settings/user_settings_display.jsx | 11 +- webapp/i18n/i18n.jsx | 40 +---- webapp/i18n/{pt-BR.json => pt.json} | 0 webapp/package.json | 3 +- webapp/root.jsx | 7 +- webapp/sass/styles.scss | 1 - webapp/stores/user_store.jsx | 6 - 24 files changed, 66 insertions(+), 396 deletions(-) rename i18n/{pt-BR.json => pt.json} (100%) delete mode 100644 webapp/components/admin_console/localization_settings.jsx delete mode 100644 webapp/components/admin_console/multiselect_settings.jsx rename webapp/i18n/{pt-BR.json => pt.json} (100%) diff --git a/api/apitestlib.go b/api/apitestlib.go index a685528d18..ab342c6b72 100644 --- a/api/apitestlib.go +++ b/api/apitestlib.go @@ -30,7 +30,7 @@ type TestHelper struct { func SetupEnterprise() *TestHelper { if Srv == nil { utils.LoadConfig("config.json") - utils.InitTranslations(utils.Cfg.LocalizationSettings) + utils.InitTranslations() utils.Cfg.TeamSettings.MaxUsersPerTeam = 50 utils.DisableDebugLogForTest() utils.License.Features.SetDefaults() @@ -50,7 +50,7 @@ func SetupEnterprise() *TestHelper { func Setup() *TestHelper { if Srv == nil { utils.LoadConfig("config.json") - utils.InitTranslations(utils.Cfg.LocalizationSettings) + utils.InitTranslations() utils.Cfg.TeamSettings.MaxUsersPerTeam = 50 utils.DisableDebugLogForTest() NewServer() diff --git a/api/user.go b/api/user.go index 3c4e5f9116..c1b40852d9 100644 --- a/api/user.go +++ b/api/user.go @@ -239,7 +239,6 @@ func CreateUser(user *model.User) (*model.User, *model.AppError) { } user.MakeNonNil() - user.Locale = *utils.Cfg.LocalizationSettings.DefaultClientLocale if result := <-Srv.Store.User().Save(user); result.Err != nil { l4g.Error(utils.T("api.user.create_user.save.error"), result.Err) diff --git a/config/config.json b/config/config.json index 70b02ab133..68d4e8f129 100644 --- a/config/config.json +++ b/config/config.json @@ -155,10 +155,5 @@ "Enable": false, "Directory": "./data/", "EnableDaily": false - }, - "LocalizationSettings": { - "DefaultServerLocale": "en", - "DefaultClientLocale": "en", - "AvailableLocales": "en,es,fr,ja,pt-BR" } } \ No newline at end of file diff --git a/i18n/pt-BR.json b/i18n/pt.json similarity index 100% rename from i18n/pt-BR.json rename to i18n/pt.json diff --git a/mattermost.go b/mattermost.go index 59b4e44d3c..4f53828adc 100644 --- a/mattermost.go +++ b/mattermost.go @@ -78,6 +78,7 @@ func main() { parseCmds() + utils.InitTranslations() if errstr := doLoadConfig(flagConfigFile); errstr != "" { l4g.Exit(utils.T("mattermost.unable_to_load_config"), errstr) return @@ -86,7 +87,6 @@ func main() { if flagRunCmds { utils.ConfigureCmdLineLog() } - utils.InitTranslations(utils.Cfg.LocalizationSettings) pwd, _ := os.Getwd() l4g.Info(utils.T("mattermost.current_version"), model.CurrentVersion, model.BuildNumber, model.BuildDate, model.BuildHash, model.BuildHashEnterprise) diff --git a/model/config.go b/model/config.go index 08b00b90f6..674a352f0a 100644 --- a/model/config.go +++ b/model/config.go @@ -204,27 +204,20 @@ type ComplianceSettings struct { EnableDaily *bool } -type LocalizationSettings struct { - DefaultServerLocale *string - DefaultClientLocale *string - AvailableLocales *string -} - type Config struct { - ServiceSettings ServiceSettings - TeamSettings TeamSettings - SqlSettings SqlSettings - LogSettings LogSettings - FileSettings FileSettings - EmailSettings EmailSettings - RateLimitSettings RateLimitSettings - PrivacySettings PrivacySettings - SupportSettings SupportSettings - GitLabSettings SSOSettings - GoogleSettings SSOSettings - LdapSettings LdapSettings - ComplianceSettings ComplianceSettings - LocalizationSettings LocalizationSettings + ServiceSettings ServiceSettings + TeamSettings TeamSettings + SqlSettings SqlSettings + LogSettings LogSettings + FileSettings FileSettings + EmailSettings EmailSettings + RateLimitSettings RateLimitSettings + PrivacySettings PrivacySettings + SupportSettings SupportSettings + GitLabSettings SSOSettings + GoogleSettings SSOSettings + LdapSettings LdapSettings + ComplianceSettings ComplianceSettings } func (o *Config) ToJson() string { @@ -520,21 +513,6 @@ func (o *Config) SetDefaults() { o.LdapSettings.NicknameAttribute = new(string) *o.LdapSettings.NicknameAttribute = "" } - - if o.LocalizationSettings.DefaultServerLocale == nil { - o.LocalizationSettings.DefaultServerLocale = new(string) - *o.LocalizationSettings.DefaultServerLocale = DEFAULT_LOCALE - } - - if o.LocalizationSettings.DefaultClientLocale == nil { - o.LocalizationSettings.DefaultClientLocale = new(string) - *o.LocalizationSettings.DefaultClientLocale = DEFAULT_LOCALE - } - - if o.LocalizationSettings.AvailableLocales == nil { - o.LocalizationSettings.AvailableLocales = new(string) - *o.LocalizationSettings.AvailableLocales = *o.LocalizationSettings.DefaultClientLocale - } } func (o *Config) IsValid() *AppError { diff --git a/model/user.go b/model/user.go index 9b52cfa970..7dee673811 100644 --- a/model/user.go +++ b/model/user.go @@ -136,6 +136,7 @@ func (u *User) PreSave() { u.Username = strings.ToLower(u.Username) u.Email = strings.ToLower(u.Email) + u.Locale = strings.ToLower(u.Locale) u.CreateAt = GetMillis() u.UpdateAt = u.CreateAt @@ -165,6 +166,7 @@ func (u *User) PreSave() { func (u *User) PreUpdate() { u.Username = strings.ToLower(u.Username) u.Email = strings.ToLower(u.Email) + u.Locale = strings.ToLower(u.Locale) u.UpdateAt = GetMillis() if u.AuthData != nil && *u.AuthData == "" { diff --git a/store/sql_store_test.go b/store/sql_store_test.go index 19ae2caaed..474a68ac7d 100644 --- a/store/sql_store_test.go +++ b/store/sql_store_test.go @@ -16,7 +16,7 @@ var store Store func Setup() { if store == nil { utils.LoadConfig("config.json") - utils.InitTranslations(utils.Cfg.LocalizationSettings) + utils.InitTranslations() store = NewSqlStore() store.MarkSystemRanUnitTests() diff --git a/utils/config.go b/utils/config.go index a3969fc40f..313b4e29ca 100644 --- a/utils/config.go +++ b/utils/config.go @@ -246,8 +246,7 @@ func getClientConfig(c *model.Config) map[string]string { props["WebsocketPort"] = fmt.Sprintf("%v", *c.ServiceSettings.WebsocketPort) props["WebsocketSecurePort"] = fmt.Sprintf("%v", *c.ServiceSettings.WebsocketSecurePort) - props["DefaultClientLocale"] = *c.LocalizationSettings.DefaultClientLocale - props["AvailableLocales"] = *c.LocalizationSettings.AvailableLocales + props["AllowCorsFrom"] = *c.ServiceSettings.AllowCorsFrom if IsLicensed { if *License.Features.CustomBrand { diff --git a/utils/config_test.go b/utils/config_test.go index 96ef496964..6f36b30c39 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -9,5 +9,5 @@ import ( func TestConfig(t *testing.T) { LoadConfig("config.json") - InitTranslations(Cfg.LocalizationSettings) + InitTranslations() } diff --git a/utils/i18n.go b/utils/i18n.go index 300f5ca7c5..2503cd5003 100644 --- a/utils/i18n.go +++ b/utils/i18n.go @@ -7,17 +7,15 @@ import ( "strings" l4g "github.com/alecthomas/log4go" - //"github.com/cloudfoundry/jibber_jabber" + "github.com/cloudfoundry/jibber_jabber" "github.com/mattermost/platform/model" "github.com/nicksnyder/go-i18n/i18n" ) var T i18n.TranslateFunc var locales map[string]string = make(map[string]string) -var settings model.LocalizationSettings -func InitTranslations(localizationSettings model.LocalizationSettings) { - settings = localizationSettings +func InitTranslations() { InitTranslationsWithDir("i18n") } @@ -36,10 +34,14 @@ func InitTranslationsWithDir(dir string) { } func GetTranslationsBySystemLocale() i18n.TranslateFunc { - locale := *settings.DefaultServerLocale - if _, ok := locales[locale]; !ok { - l4g.Error("Failed to load system translations for '%v' attempting to fall back to '%v'", locale, model.DEFAULT_LOCALE) - locale = model.DEFAULT_LOCALE + locale := model.DEFAULT_LOCALE + if userLanguage, err := jibber_jabber.DetectLanguage(); err == nil { + if _, ok := locales[userLanguage]; ok { + locale = userLanguage + } else { + l4g.Error("Failed to load system translations for '%v' attempting to fall back to '%v'", locale, model.DEFAULT_LOCALE) + locale = model.DEFAULT_LOCALE + } } if locales[locale] == "" { @@ -70,20 +72,10 @@ func SetTranslations(locale string) i18n.TranslateFunc { } func GetTranslationsAndLocale(w http.ResponseWriter, r *http.Request) (i18n.TranslateFunc, string) { - // This is for checking against locales like pt_BR or zn_CN - headerLocaleFull := strings.Split(r.Header.Get("Accept-Language"), ",")[0] - // This is for checking agains locales like en, es headerLocale := strings.Split(strings.Split(r.Header.Get("Accept-Language"), ",")[0], "-")[0] - defaultLocale := *settings.DefaultClientLocale - if locales[headerLocaleFull] != "" { - translations := TfuncWithFallback(headerLocaleFull) - return translations, headerLocaleFull - } else if locales[headerLocale] != "" { + if locales[headerLocale] != "" { translations := TfuncWithFallback(headerLocale) return translations, headerLocale - } else if locales[defaultLocale] != "" { - translations := TfuncWithFallback(defaultLocale) - return translations, headerLocale } translations := TfuncWithFallback(model.DEFAULT_LOCALE) @@ -97,7 +89,7 @@ func TfuncWithFallback(pref string) i18n.TranslateFunc { return translated } - t, _ := i18n.Tfunc(model.DEFAULT_LOCALE) + t, _ := i18n.Tfunc("en") return t(translationID, args...) } } diff --git a/web/web_test.go b/web/web_test.go index 0a9869e401..d4d9a5f26b 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -21,7 +21,7 @@ var URL string func Setup() { if api.Srv == nil { utils.LoadConfig("config.json") - utils.InitTranslations(utils.Cfg.LocalizationSettings) + utils.InitTranslations() api.NewServer() api.StartServer() api.InitApi() diff --git a/webapp/actions/global_actions.jsx b/webapp/actions/global_actions.jsx index 93ed7517bb..2e2b1b247c 100644 --- a/webapp/actions/global_actions.jsx +++ b/webapp/actions/global_actions.jsx @@ -392,14 +392,9 @@ export function newLocalizationSelected(locale) { translations: en }); } else { - const localeInfo = I18n.getLanguageInfo(locale) || I18n.getLanguageInfo(global.window.mm_config.DefaultClientLocale); - Client.getTranslations( - localeInfo.url, - (data, res) => { - if (!data && res.text) { - data = JSON.parse(res.text); //eslint-disable-line no-param-reassign - } + I18n.getLanguageInfo(locale).url, + (data) => { AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_LOCALE, locale, @@ -413,11 +408,10 @@ export function newLocalizationSelected(locale) { } } -export function loadDefaultLocale() { - const defaultLocale = global.window.mm_config.DefaultClientLocale; - let locale = global.window.mm_user ? global.window.mm_user.locale || defaultLocale : defaultLocale; - - if (!I18n.getLanguageInfo(locale)) { +export function loadBrowserLocale() { + let locale = (navigator.languages && navigator.languages.length > 0 ? navigator.languages[0] : + (navigator.language || navigator.userLanguage)).split('-')[0]; + if (!I18n.getLanguages()[locale]) { locale = 'en'; } return newLocalizationSelected(locale); diff --git a/webapp/components/admin_console/admin_sidebar.jsx b/webapp/components/admin_console/admin_sidebar.jsx index 9548a77638..cdb7e29d53 100644 --- a/webapp/components/admin_console/admin_sidebar.jsx +++ b/webapp/components/admin_console/admin_sidebar.jsx @@ -292,15 +292,6 @@ export default class AdminSidebar extends React.Component { /> } /> - - } - /> { - return {value: locales[l].value, text: locales[l].name}; - }) - }); - } - - canSave() { - return this.state.availableLocales.join(',').indexOf(this.state.defaultClientLocale) !== -1; - } - - getConfigFromState(config) { - config.LocalizationSettings.DefaultServerLocale = this.state.defaultServerLocale; - config.LocalizationSettings.DefaultClientLocale = this.state.defaultClientLocale; - config.LocalizationSettings.AvailableLocales = this.state.availableLocales.join(','); - - return config; - } - - renderTitle() { - return ( -

- -

- ); - } - - renderSettings() { - return ( - - } - > - - } - value={this.state.defaultServerLocale} - onChange={this.handleChange} - helpText={ - - } - /> - - } - value={this.state.defaultClientLocale} - onChange={this.handleChange} - helpText={ - - } - /> - - } - selected={this.state.availableLocales} - mustBePresent={this.state.defaultClientLocale} - onChange={this.handleChange} - helpText={ - - } - noResultText={ - - } - errorText={ - - } - notPresent={ - - } - /> - - ); - } -} \ No newline at end of file diff --git a/webapp/components/admin_console/multiselect_settings.jsx b/webapp/components/admin_console/multiselect_settings.jsx deleted file mode 100644 index deba983de4..0000000000 --- a/webapp/components/admin_console/multiselect_settings.jsx +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. -// See License.txt for license information. -import React from 'react'; -import ReactSelect from 'react-select'; - -import Setting from './setting.jsx'; -import FormError from 'components/form_error.jsx'; - -export default class MultiSelectSetting extends React.Component { - constructor(props) { - super(props); - - this.handleChange = this.handleChange.bind(this); - this.state = {error: false}; - } - - handleChange(newValue) { - const values = newValue.map((n) => { - return n.value; - }); - - if (!newValue || newValue.length === 0) { - this.setState({error: this.props.errorText}); - } else if (this.props.mustBePresent && values.join(',').indexOf(this.props.mustBePresent) === -1) { - this.setState({error: this.props.notPresent}); - } else { - this.props.onChange(this.props.id, values); - this.setState({error: false}); - } - } - - componentWillReceiveProps(newProps) { - if (newProps.mustBePresent && newProps.selected.join(',').indexOf(newProps.mustBePresent) === -1) { - this.setState({error: this.props.notPresent}); - } else { - this.setState({error: false}); - } - } - - render() { - return ( - - - - - ); - } -} - -MultiSelectSetting.defaultProps = { - disabled: false -}; - -MultiSelectSetting.propTypes = { - id: React.PropTypes.string.isRequired, - values: React.PropTypes.array.isRequired, - label: React.PropTypes.node.isRequired, - selected: React.PropTypes.array.isRequired, - mustBePresent: React.PropTypes.string, - onChange: React.PropTypes.func.isRequired, - disabled: React.PropTypes.bool, - helpText: React.PropTypes.node, - noResultText: React.PropTypes.node, - errorText: React.PropTypes.node, - notPresent: React.PropTypes.node -}; \ No newline at end of file diff --git a/webapp/components/root.jsx b/webapp/components/root.jsx index c8283cbf1f..f4656d45ff 100644 --- a/webapp/components/root.jsx +++ b/webapp/components/root.jsx @@ -6,7 +6,6 @@ import * as GlobalActions from 'actions/global_actions.jsx'; import LocalizationStore from 'stores/localization_store.jsx'; -import Client from 'utils/web_client.jsx'; import {IntlProvider} from 'react-intl'; @@ -27,25 +26,9 @@ export default class Root extends React.Component { this.localizationChanged = this.localizationChanged.bind(this); this.redirectIfNecessary = this.redirectIfNecessary.bind(this); - - // Ya.... - /*eslint-disable */ - if (window.mm_config.SegmentDeveloperKey != null && window.mm_config.SegmentDeveloperKey !== "") { - !function(){var analytics=global.window.analytics=global.window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t ); } else { - let locale; - if (I18n.isLanguageAvailable(userLocale)) { - locale = I18n.getLanguageInfo(userLocale).name; - } else { - locale = I18n.getLanguageInfo(global.window.mm_config.DefaultClientLocale).name; - } + var locale = I18n.getLanguageInfo(this.props.user.locale).name; languagesSection = ( { - if (languages[l]) { - availableLanguages[l] = languages[l]; - } - }); -} - -export function getAllLanguages() { +export function getLanguages() { return languages; } -export function getLanguages() { - if (!availableLanguages) { - setAvailableLanguages(); - } - return availableLanguages; -} - export function getLanguageInfo(locale) { - if (!availableLanguages) { - setAvailableLanguages(); - } - return availableLanguages[locale]; -} - -export function isLanguageAvailable(locale) { - return !!availableLanguages[locale]; + return languages[locale]; } export function safariFix(callback) { diff --git a/webapp/i18n/pt-BR.json b/webapp/i18n/pt.json similarity index 100% rename from webapp/i18n/pt-BR.json rename to webapp/i18n/pt.json diff --git a/webapp/package.json b/webapp/package.json index fc7c07a193..deecbc1a81 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -27,9 +27,8 @@ "react-bootstrap": "0.29.3", "react-custom-scrollbars": "4.0.0-beta.1", "react-dom": "15.0.2", - "react-intl": "2.1.2", + "react-intl": "2.0.0-rc-1", "react-router": "2.4.0", - "react-select": "1.0.0-beta13", "react-textarea-autosize": "4.0.1", "superagent": "1.8.3", "twemoji": "2.0.5", diff --git a/webapp/root.jsx b/webapp/root.jsx index 40cf351de4..dc2df64ac1 100644 --- a/webapp/root.jsx +++ b/webapp/root.jsx @@ -53,7 +53,6 @@ const ActionTypes = Constants.ActionTypes; import AdminConsole from 'components/admin_console/admin_console.jsx'; import SystemAnalytics from 'components/analytics/system_analytics.jsx'; import ConfigurationSettings from 'components/admin_console/configuration_settings.jsx'; -import LocalizationSettings from 'components/admin_console/localization_settings.jsx'; import UsersAndTeamsSettings from 'components/admin_console/users_and_teams_settings.jsx'; import PrivacySettings from 'components/admin_console/privacy_settings.jsx'; import LogSettings from 'components/admin_console/log_settings.jsx'; @@ -143,8 +142,8 @@ function preRenderSetup(callwhendone) { ); function afterIntl() { + I18n.doAddLocaleData(); $.when(d1).done(() => { - I18n.doAddLocaleData(); callwhendone(); }); } @@ -363,10 +362,6 @@ function renderRootComponent() { path='configuration' component={ConfigurationSettings} /> -