Allow users to toggle interface language in their preferences

This commit is contained in:
Jonathan Allard 2014-02-07 22:24:10 -05:00
parent 1a8ebb710e
commit c513725f26
11 changed files with 57 additions and 1 deletions

View File

@ -30,6 +30,12 @@ Discourse.PreferencesController = Discourse.ObjectController.extend({
return Discourse.SiteSettings.enable_names;
}.property(),
availableLocales: function() {
return Discourse.SiteSettings.available_locales.split('|').map( function(s) {
return {name: s, value: s};
});
}.property(),
digestFrequencies: [{ name: I18n.t('user.email_digests.daily'), value: 1 },
{ name: I18n.t('user.email_digests.weekly'), value: 7 },
{ name: I18n.t('user.email_digests.bi_weekly'), value: 14 }],

View File

@ -170,6 +170,7 @@ Discourse.User = Discourse.Model.extend({
'bio_raw',
'website',
'name',
'locale',
'email_digests',
'email_direct',
'email_always',

View File

@ -66,6 +66,16 @@
</div>
</div>
<div class="control-group">
<label class="control-label">{{i18n user.locale.title}}</label>
<div class="controls">
{{combobox valueAttribute="value" content=availableLocales value=locale none="user.locale.default"}}
</div>
<div class='instructions'>
{{i18n user.locale.instructions}}
</div>
</div>
<div class="control-group">
<label class="control-label">{{i18n user.bio}}</label>
<div class="controls">

View File

@ -109,7 +109,11 @@ class ApplicationController < ActionController::Base
end
def set_locale
I18n.locale = SiteSetting.default_locale
I18n.locale = if current_user && current_user.locale.present?
current_user.locale
else
SiteSetting.default_locale
end
end
def store_preloaded(key, json)

View File

@ -23,6 +23,11 @@ class SiteSetting < ActiveRecord::Base
load_settings(file)
end
SiteSettingExtension.class_variable_get(:@@client_settings) << :available_locales
def self.available_locales
LocaleSiteSetting.values.map{ |e| e[:value] }.join('|')
end
def self.call_discourse_hub?
self.enforce_global_nicknames? && self.discourse_org_access_key.present?

View File

@ -47,6 +47,7 @@ class UserSerializer < BasicUserSerializer
end
private_attributes :email,
:locale,
:email_digests,
:email_private_messages,
:email_direct,

View File

@ -27,6 +27,7 @@ class UserUpdater
user.bio_raw = attributes.fetch(:bio_raw) { user.bio_raw }
user.name = attributes.fetch(:name) { user.name }
user.locale = attributes.fetch(:locale) { user.locale }
user.digest_after_days = attributes.fetch(:digest_after_days) { user.digest_after_days }
if attributes[:auto_track_topics_after_msecs]

View File

@ -325,6 +325,11 @@ en:
enter_email: 'Username found. Enter matching email.'
prefilled: "Email matches this registered username."
locale:
title: "Interface language"
instructions: "The language used by the forum interface. It will change when you refresh the page."
default: "(default)"
password_confirmation:
title: "Password Again"

View File

@ -299,6 +299,11 @@ fr:
enter_email: 'Pseudo trouvé. Entrez l''adresse email correspondante.'
prefilled: "L'adresse email correspond à ce pseudo enregistré."
locale:
title: "Langue de l'interface"
instructions: "La langue utilisée dans l'interface du forum. Le changement se fait lorsque vous rechargez la page."
default: "(par défaut)"
password_confirmation:
title: "Confirmation"

View File

@ -0,0 +1,5 @@
class AddLocaleToUser < ActiveRecord::Migration
def change
add_column :users, :locale, :string, limit: 10
end
end

View File

@ -44,6 +44,19 @@ describe TopicsController do
end
describe 'set_locale' do
it 'sets the one the user prefers' do
SiteSetting.stubs(:allow_user_locale).returns(true)
user = Fabricate(:user, locale: :fr)
log_in_user(user)
get :show, {topic_id: topic.id}
I18n.locale.should == :fr
end
end
end
describe 'api' do