Skip to content

Commit

Permalink
fix: fix setting a locale (#6721)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin authored Jun 28, 2023
1 parent c69297f commit ddcb6e2
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Interfaces\ServiceInterface;
use App\Models\User;
use App\Services\BaseService;
use Illuminate\Support\Facades\App;
use Illuminate\Validation\Rule;

class StoreLocale extends BaseService implements ServiceInterface
{
Expand All @@ -18,7 +20,12 @@ public function rules(): array
return [
'account_id' => 'required|uuid|exists:accounts,id',
'author_id' => 'required|uuid|exists:users,id',
'locale' => 'required|string|max:10',
'locale' => [
'required',
'string',
'max:5',
Rule::in(config('localizer.supported-locales')),
],
];
}

Expand Down Expand Up @@ -49,5 +56,7 @@ private function updateUser(): void
{
$this->author->locale = $this->data['locale'];
$this->author->save();

App::setLocale($this->data['locale']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,15 @@ public static function dtoMapsPreferences(User $user): array
public static function dtoLocale(User $user): array
{
return [
'locale' => $user->locale,
'locale_i18n' => self::language($user->locale),
'languages' => collect(config('localizer.supported-locales'))->mapWithKeys(fn ($locale) => [
$locale => self::language($locale),
])->sortByCollator(fn ($value) => $value),
'id' => $user->locale,
'name' => self::language($user->locale),
'dir' => htmldir(),
'locales' => collect(config('localizer.supported-locales'))
->map(fn ($locale) => [
'id' => $locale,
'name' => self::language($locale),
])
->sortByCollator(fn ($value) => $value['name']),
'url' => [
'store' => route('settings.preferences.locale.store'),
],
Expand All @@ -205,6 +209,6 @@ public static function dtoLocale(User $user): array

public static function language(?string $code): string
{
return $code !== null ? trans('auth.lang', locale: $code) : '';
return $code !== null ? __('auth.lang', [], $code) : '';
}
}
51 changes: 51 additions & 0 deletions database/migrations/2023_06_28_102228_fix_locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
private $locales = [
0 => 'ca',
1 => 'da',
2 => 'de',
3 => 'en',
4 => 'es',
5 => 'fr',
6 => 'it',
7 => 'no',
8 => 'pl',
9 => 'pt',
10 => 'ru',
11 => 'ro',
12 => 'sv',
13 => 'tr',
14 => 'vi',
15 => 'el',
16 => 'he',
17 => 'ur',
18 => 'hi',
19 => 'bn',
20 => 'nl',
21 => 'pa',
22 => 'te',
23 => 'ml',
24 => 'zh',
25 => 'ja',
];

/**
* Run the migrations.
*/
public function up(): void
{
User::chunk(100, function ($users) {
foreach ($users as $user) {
if (array_key_exists($user->locale, $this->locales)) {
$user->locale = $this->locales[$user->locale];
$user->save();
}
}
});
}
};
37 changes: 14 additions & 23 deletions resources/js/Pages/Settings/Preferences/Partials/Locale.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup>
import { computed, ref } from 'vue';
import { ref } from 'vue';
import { useForm } from '@inertiajs/vue3';
import { loadLanguageAsync, getActiveLanguage, trans } from 'laravel-vue-i18n';
import { flash } from '@/methods.js';
Expand All @@ -15,35 +15,26 @@ const props = defineProps({
const loadingState = ref('');
const editMode = ref(false);
const localLocaleI18n = ref(props.data.locale_i18n);
const localLocale = ref(props.data.name);
const form = useForm({
locale: props.data.locale,
locale: props.data.id,
errors: [],
});
const enableEditMode = () => {
editMode.value = true;
};
const locales = computed(() => {
return _.map(props.data.languages, (value, key) => {
return { id: key, name: value };
});
});
const submit = () => {
loadingState.value = 'loading';
axios
.post(props.data.url.store, form.data())
.then((response) => {
flash(trans('Changes saved'), 'success');
localLocaleI18n.value = response.data.data.locale_i18n;
localLocale.value = response.data.data.name;
editMode.value = false;
loadingState.value = null;
if (getActiveLanguage() !== form.locale) {
loadLanguageAsync(response.data.data.locale);
loadLanguageAsync(response.data.data.id);
document.getRootNode().querySelector('html').setAttribute('dir', response.data.data.dir);
}
})
.catch((error) => {
Expand All @@ -63,34 +54,34 @@ const submit = () => {
{{ $t('Language of the application') }}
</span>

<help :url="$page.props.help_links.settings_preferences_language" :top="'5px'" />
<Help :url="$page.props.help_links.settings_preferences_language" :top="'5px'" />
</h3>
<pretty-button v-if="!editMode" :text="$t('Edit')" @click="enableEditMode" />
<PrettyButton v-if="!editMode" :text="$t('Edit')" @click="editMode = true" />
</div>

<!-- normal mode -->
<div v-if="!editMode" class="mb-6 rounded-lg border border-gray-200 bg-white dark:border-gray-700 dark:bg-gray-900">
<p class="px-5 py-2">
<span class="mb-2 block">{{ $t('Current language:') }}</span>
<span class="mb-2 block rounded bg-slate-100 px-5 py-2 text-sm dark:bg-slate-900">{{ localLocaleI18n }}</span>
<span class="mb-2 block rounded bg-slate-100 px-5 py-2 text-sm dark:bg-slate-900">{{ localLocale }}</span>
</p>
</div>

<!-- edit mode -->
<form
v-if="editMode"
v-else
class="mb-6 rounded-lg border border-gray-200 bg-gray-50 bg-white dark:border-gray-700 dark:bg-gray-900"
@submit.prevent="submit()">
<div class="border-b border-gray-200 px-5 py-2 dark:border-gray-700">
<errors :errors="form.errors" />
<Errors :errors="form.errors" />

<Dropdown v-model="form.locale" name="locale" :data="locales" />
<Dropdown v-model="form.locale" name="locale" :data="data.locales" />
</div>

<!-- actions -->
<div class="flex justify-between p-5">
<pretty-link :text="$t('Cancel')" :class="'me-3'" @click="editMode = false" />
<pretty-button :text="$t('Save')" :state="loadingState" :icon="'check'" :class="'save'" />
<PrettyLink :text="$t('Cancel')" :class="'me-3'" @click="editMode = false" />
<PrettyButton :text="$t('Save')" :state="loadingState" :icon="'check'" :class="'save'" />
</div>
</form>
</div>
Expand Down
8 changes: 6 additions & 2 deletions resources/js/Shared/Modules/ContactInformation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<!-- name -->
<div class="border-b border-gray-200 p-5 dark:border-gray-700">
<text-input
ref="newData"
ref="rename"
v-model="form.data"
:label="$t('Content')"
:type="'text'"
Expand All @@ -121,7 +121,7 @@
:required="false"
:autocomplete="false"
:maxlength="255"
@esc-key-pressed="addContactInformationModalShown = false" />
@esc-key-pressed="editedContactInformationId = 0" />
</div>

<div class="p-5">
Expand Down Expand Up @@ -215,6 +215,10 @@ export default {
this.editedContactInformationId = info.id;
this.form.contact_information_type_id = info.contact_information_type.id;
this.form.data = info.data;
this.$nextTick().then(() => {
this.$refs.rename[0].focus();
});
},
submit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ public function it_gets_the_data_needed_for_locale(): void
'locale' => 'fr',
]);
$array = UserPreferencesIndexViewHelper::dtoLocale($user);
$this->assertEquals('fr', $array['locale']);
$this->assertEquals('Français', $array['locale_i18n']);
$this->assertEquals('fr', $array['id']);
$this->assertEquals('Français', $array['name']);
$this->assertEquals('ltr', $array['dir']);
$this->assertEquals([
'store' => env('APP_URL').'/settings/preferences/locale',
], $array['url']);
Expand Down

0 comments on commit ddcb6e2

Please sign in to comment.