Variable default interface language for new users

This is also incorrect. I18n.locale is set from the header only if there is no logged in user. See here:

Now, I suspect that when the user first signs up, current_user is not set, thus kick-starting the whole get-the-language-from-header process, and then the I18n.locale got persisted into the user’s account.

However, this is certain NOT how it is done when a user redeems an invite.

1 个赞

Yes, if you look at users_controller you can see how it’s done differently.

3 个赞

Yup. I see it.

UsersController inherits from ApplicationController, which runs set_locale as a before_action.

That kick-starts the whole thing.

But users redeeming an invitation does not go through this.

2 个赞

Judging from this here:

https://github.com/discourse/discourse/blob/cf9607a0cb5753bdae1c756752021aeb2df3f264/app/controllers/invites_controller.rb#L32-L60

Would this work:

      user = invite.redeem(username: params[:username], name: params[:name], password: params[:password], user_custom_fields: params[:user_custom_fields])
      if user.present?
        user.locale = I18n.locale
        user.save!

        log_on_user(user)
        post_process_invite(user)
      end

Sorry for my Ruby – I don’t really know it. :sweat_smile:

1 个赞

As of this commit from @david, this should work cleanly now :tada:

https://github.com/discourse/discourse/pull/10289

1 个赞

Thank guys so much.
But…there’s another problem for me.:pensive:
I don’t knew how to use this😣

To allow the locale to be set for anonymous (non-logged in) users based on their browser’s language settings, you need to enable both the allow user locale and set locale from accept language header site settings. Both of those settings are found near the top of your site’s “Basic Setup” settings page.

Once those settings are enabled, users who are not logged into the site will have the Discourse user interface automatically set to the preferred language they have set in their browser. If a non-logged-in user decides to create an account on the site, their locale will be automatically set to the locale that is set by their browser. Note that this will only work if their locale is set to a language that Discourse has been translated into.

The set locale from accept language header setting has no effect on users who have already created accounts on the site. Once an account has been created, the Discourse user interface will be displayed in the locale that is set on the user’s preferences page. As long as the allow user locale setting is enabled, existing users can update their locale here:

The set locale from accept language header needs to have a howto topic written for it. I’m setting myself a reminder for that and will get the topic written soon.

2 个赞

关于此设置与翻译插件的快速提问。如果插件翻译限制设置为“所有人”,翻译插件在这种情况下是否可用?
image
(鉴于不同的区域设置是触发翻译按钮首先显示的因素)

只是通知您一下,我认为“所有人”组在此翻译设置下无法正常工作。它也不对匿名用户可用,因此建议将其设置为 TL0 组。

1 个赞