Answers.Body: <p>The user’s locale is considered a preference and is therefore stored in a separate <code>userpreferences</code> database table. As such, you cannot simply set it on a user element.</p>
  <p>Instead, you can go through the <a href="https://github.com/craftcms/cms/blob/c122b6d84f9c2421f798634c6e42a6eee1271b9a/src/services/Users.php#L344-L361" rel="nofollow noreferrer">Users::saveUserPreferences()</a> method as follows.</p>
  <pre class="lang-php prettyprint-override"><code>Craft::$app-&gt;users-&gt;saveUserPreferences($user, ['locale' =&gt; 'en-US']);
  </code></pre>
  <p>For reference, this is called from the <a href="https://github.com/craftcms/cms/blob/c122b6d84f9c2421f798634c6e42a6eee1271b9a/src/controllers/UsersController.php#L1440" rel="nofollow noreferrer">UsersController</a> class.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-10T23:11:37.353Z
Answers.LastActivityDate: 2023-01-10T23:11:37.353Z
Answers.UserDisplayName: Ben Croker
Body: <p>Recently upgrade from Craft 2 to Craft 3. Also had to update all the custom plugins.  Once of them is to run a daily import of users from a file.  In that file we try to set the preferredLocal of that users as we have 3 locales/sites.  Once I have created the user and try to update the preferredLocale as I did in craft 2 it fails out.</p>
  <pre><code>//I get a setting read-only property error in logs when I call it like this
  $user-&gt;preferredLocale = &quot;en_us&quot;;
  
  //I get a setting unknown property error when I try it like this.  
  $user-&gt;setFieldValue('preferredLocale', 'en_us');
  </code></pre>
  <p>I have looked around but not able to find how I can change the new or existing users preferred Locale so any help or examples would be appreciated.</p>
  
Tags: <craft3><plugin-development><users>
Title: Editing users preferredLocale from a plugin controller
ThreadText: # Editing users preferredLocale from a plugin controller
  
  (Asked by: username_0 on 2023-01-10)
  
  Recently upgrade from Craft 2 to Craft 3. Also had to update all the custom plugins. Once of them is to run a daily import of users from a file. In that file we try to set the preferredLocal of that users as we have 3 locales/sites. Once I have created the user and try to update the preferredLocale as I did in craft 2 it fails out.
  
  ```
  //I get a setting read-only property error in logs when I call it like this
  $user->preferredLocale = "en_us";
  
  //I get a setting unknown property error when I try it like this.  
  $user->setFieldValue('preferredLocale', 'en_us');
  
  ```
  
  I have looked around but not able to find how I can change the new or existing users preferred Locale so any help or examples would be appreciated.
  
  **Tags:** craft3, plugin-development, users
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-01-10. Score: 1 | Accepted answer
  
  The user’s locale is considered a preference and is therefore stored in a separate `userpreferences` database table. As such, you cannot simply set it on a user element.
  
  Instead, you can go through the [Users::saveUserPreferences()](https://github.com/craftcms/cms/blob/c122b6d84f9c2421f798634c6e42a6eee1271b9a/src/services/Users.php#L344-L361) method as follows.
  
  ```php
  Craft::$app->users->saveUserPreferences($user, ['locale' => 'en-US']);
  
  ```
  
  For reference, this is called from the [UsersController](https://github.com/craftcms/cms/blob/c122b6d84f9c2421f798634c6e42a6eee1271b9a/src/controllers/UsersController.php#L1440) class.
  
  
  ---
  
  