Skip to content

Commit

Permalink
#198 - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofrewak committed Jan 23, 2024
1 parent b386aa7 commit c5868d7
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 166 deletions.
26 changes: 24 additions & 2 deletions app/Http/Middleware/Localize.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,38 @@

use Closure;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\ResponseFactory;
use Symfony\Component\HttpFoundation\Response;

class Localize
{
public function __construct(protected ResponseFactory $factory)
{
}

public function handle(Request $request, Closure $next): Response
{
if (str_starts_with($request->header("Accept-Language"), "pl")) {
app()->setLocale("pl");
$uri = $request->getRequestUri();
$parts = explode("/", $uri);

if(isset($parts[1])) {
if(empty($parts[1])) {
if (str_starts_with($request->header("Accept-Language"), "pl")) {
app()->setLocale("pl");
} else {
app()->setLocale("en");
}
} else {
app()->setLocale(match($parts[1]) {
"pl" => "pl",
default => "en",
});
}
}

$this->factory->share("locale", app()->getLocale());

return $next($request);
}
}
390 changes: 230 additions & 160 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"laravel-vite-plugin": "^0.8.1",
"lodash": "^4.17.21",
"tailwindcss": "^3.3.5",
"vue": "^3.3.4"
"vue": "^3.3.4",
"vue-i18n": "^9.9.0"
},
"devDependencies": {
"@blumilksoftware/eslint-config": "^2.0.0",
Expand Down
7 changes: 7 additions & 0 deletions resources/js/Locale/en/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
pages: {
home: {
title: "Index"
}
}
}
7 changes: 7 additions & 0 deletions resources/js/Locale/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import en from "@/Locale/en"
import pl from "@/Locale/pl"

export default {
en: en,
pl: pl,
}
4 changes: 4 additions & 0 deletions resources/js/Locale/locale.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Locale {
English = 'en',
Polish = 'pl',
}
7 changes: 7 additions & 0 deletions resources/js/Locale/pl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
pages: {
home: {
title: "Indeks"
}
}
}
12 changes: 9 additions & 3 deletions resources/js/Pages/Home.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<script setup>
import { Head } from '@inertiajs/vue3'
import Navigation from "@/Layout/Navigation.vue";
import {Head, usePage} from '@inertiajs/vue3'
import Navigation from '@/Layout/Navigation.vue'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
const locale = usePage().props.locale
</script>

<template>
<Head title="Strona główna"></Head>

<Navigation></Navigation>
<div class="w-full h-screen mx-auto flex flex-col items-center justify-center gap-12">
<img src="/logo.png" alt="Blumilk logo" class="w-32">
strona główna
{{ t('pages.home.title') }} {{ locale }}
</div>
</template>
17 changes: 17 additions & 0 deletions resources/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@ import '../css/app.css'
import { createApp, h, type DefineComponent } from 'vue'
import { createInertiaApp } from '@inertiajs/vue3'
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers'
import { createI18n } from 'vue-i18n'
import messages from '@/Locale'
import { Locale } from '@/Locale/locale.type'
import { usePage } from '@inertiajs/vue3'

const page = usePage()
// const locale = page.props.locale as string

console.log(page)

const appName = import.meta.env.VITE_APP_NAME || 'Laravel'

createInertiaApp({
title: (title) => `${title} - ${appName}`,
resolve: async (name) => await resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob<DefineComponent>('./Pages/**/*.vue')),
setup({ el, App, props, plugin }) {
const i18n = createI18n({
legacy: false,
locale: Locale.English,
fallbackLocale: Locale.English,
messages: messages,
})

createApp({ render: () => h(App, props) })
.use(plugin)
.use(i18n as any)
.mount(el)
},
progress: {
Expand Down

0 comments on commit c5868d7

Please sign in to comment.