Skip to content

Commit 9404d71

Browse files
committed
Localize roles and genders
1 parent e7f92e0 commit 9404d71

File tree

11 files changed

+45
-38
lines changed

11 files changed

+45
-38
lines changed

resources/js/Components/Navbar.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import RoleBadge from "@/Components/RoleBadge.vue";
3636
<p class="truncate">
3737
{{ $page.props.auth.user.login }}
3838
</p>
39-
<RoleBadge :role="$page.props.auth.user.role.name" />
39+
<RoleBadge :role="$page.props.auth.user.role" />
4040
</div>
4141
</div>
4242
<nav class="grid space-y-1 pt-2">

resources/js/Components/RoleBadge.vue

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
<script setup>
2-
import { computed } from 'vue';
3-
import { trans } from 'laravel-vue-i18n';
2+
import { getRoleName } from "@/Utils/role.js";
43
54
const props = defineProps({
65
role: {
7-
type: String,
6+
type: Object,
87
required: true,
98
},
109
});
11-
12-
const roleName = computed(() => {
13-
return trans(`role.name.${props.role.toLowerCase()}`, 'role.name.unknown');
14-
});
1510
</script>
1611

1712
<template>
1813
<div class="badge badge-secondary rounded">
19-
{{ roleName }}
14+
{{ getRoleName(role) }}
2015
</div>
2116
</template>

resources/js/Pages/Dashboard/Users/Create.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import DashboardLayout from '@/Layouts/Dashboard.vue';
33
import { useForm } from '@inertiajs/vue3';
44
import { ref, watch, computed } from 'vue';
5+
import { getRoleName } from '@/Utils/role';
6+
import { getGenderName } from "@/Utils/gender";
57
import BackButton from "@/Components/Buttons/BackButton.vue";
68
import CreateButton from "@/Components/Buttons/CreateButton.vue";
79
import InputError from "@/Components/InputError.vue";
@@ -146,7 +148,7 @@ const store = () => {
146148
class="select select-bordered w-full focus:ring-1 focus:ring-offset-2 focus:ring-offset-base-200 focus:ring-orange-500"
147149
v-model="form.role">
148150
<option disabled value="">{{ ('Назначьте роль пользователю') }}</option>
149-
<option v-for="role in roles" :key="role.id" :value="role.id">{{ role.name }}</option>
151+
<option v-for="role in roles" :key="role.id" :value="role.id">{{ getRoleName(role) }}</option>
150152
</select>
151153
<InputError :message="form.errors.role" />
152154
</div>
@@ -225,7 +227,7 @@ const store = () => {
225227
class="select select-bordered w-full focus:ring-1 focus:ring-offset-2 focus:ring-offset-base-200 focus:ring-orange-500"
226228
v-model="form.gender">
227229
<option disabled value="">{{ ('Укажите пол пользователя') }}</option>
228-
<option v-for="gender in genders" :value="gender.id">{{ gender.name }}</option>
230+
<option v-for="gender in genders" :value="gender.id">{{ getGenderName(gender) }}</option>
229231
</select>
230232
<InputError :message="form.errors.gender" />
231233
</div>

resources/js/Pages/Dashboard/Users/Edit.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import DashboardLayout from '@/Layouts/Dashboard.vue';
33
import { useForm } from '@inertiajs/vue3';
44
import { ref, watch, computed } from 'vue';
5+
import { getRoleName } from '@/Utils/role';
6+
import { getGenderName } from "@/Utils/gender";
57
import BackButton from "@/Components/Buttons/BackButton.vue";
68
import UpdateButton from "@/Components/Buttons/UpdateButton.vue";
79
import InputError from "@/Components/InputError.vue";
@@ -139,7 +141,7 @@ function update() {
139141
v-model="form.role"
140142
required>
141143
<option disabled>{{ ('Назначьте роль пользователю...') }}</option>
142-
<option v-for="role in roles" :key="role.id" :value="role.id">{{ role.name }}</option>
144+
<option v-for="role in roles" :key="role.id" :value="role.id">{{ getRoleName(role) }}</option>
143145
</select>
144146
<InputError :message="form.errors.role"/>
145147
</div>
@@ -218,7 +220,7 @@ function update() {
218220
class="select select-bordered w-full focus:ring-1 focus:ring-offset-2 focus:ring-offset-base-200 focus:ring-orange-500"
219221
v-model="form.gender">
220222
<option disabled value="">{{ ('Укажите пол пользователя') }}</option>
221-
<option v-for="gender in genders" :value="gender.id">{{ gender.name }}</option>
223+
<option v-for="gender in genders" :value="gender.id">{{ getGenderName(gender) }}</option>
222224
</select>
223225
<InputError :message="form.errors.gender" />
224226
</div>

resources/js/Pages/Dashboard/Users/Index.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const confirmDeleteUser = (id) => {
116116
</div>
117117
</td>
118118
<td>
119-
<RoleBadge :role="user.role.name" />
119+
<RoleBadge :role="user.role" />
120120
</td>
121121
<td>{{ user.created_at }}</td>
122122
<td>

resources/js/Utils/gender.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { trans } from 'laravel-vue-i18n';
2+
3+
export const getGenderName = (gender, separator = '-') => {
4+
if (!gender?.name) {
5+
return trans('gender.name.unknown');
6+
}
7+
8+
const formattedgenderName = gender.name.toLowerCase().replace(/\s+/g, separator);
9+
const translation = trans(`gender.name.${formattedgenderName}`);
10+
11+
return translation === `gender.name.${formattedgenderName}` ? gender.name : translation;
12+
};

resources/js/Utils/role.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { trans } from 'laravel-vue-i18n';
2+
3+
export const getRoleName = (role, separator = '-') => {
4+
if (!role?.name) {
5+
return trans('role.name.unknown');
6+
}
7+
8+
const formattedRoleName = role.name.toLowerCase().replace(/\s+/g, separator);
9+
const translation = trans(`role.name.${formattedRoleName}`);
10+
11+
return translation === `role.name.${formattedRoleName}` ? role.name : translation;
12+
};

resources/lang/en.json

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
"role.name.user": "User",
2626
"role.name.unknown": "Unknown role",
2727

28+
"gender.name.male": "Male",
29+
"gender.name.female": "Female",
30+
"gender.name.not-specified": "Not specified",
31+
2832
"dashboard.page.main.title": "Dashboard",
2933
"dashboard.page.settings.title": "System settings",
3034

resources/lang/en/genders.php

-12
This file was deleted.

resources/lang/ru.json

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
"role.name.user": "Пользователь",
2626
"role.name.unknown": "Неизвестная роль",
2727

28+
"gender.name.male": "Мужской",
29+
"gender.name.female": "Женский",
30+
"gender.name.not-specified": "Не указано",
31+
2832
"dashboard.page.main.title": "Панель управления",
2933
"dashboard.page.settings.title": "Настройки системы",
3034

resources/lang/ru/genders.php

-12
This file was deleted.

0 commit comments

Comments
 (0)