HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/affiliate.casatrade/src/hooks.server.ts
import { base } from '$app/paths';
import { defaultLocale, loadTranslations, locales } from '$lib/translations';
import type { Handle, HandleServerError } from '@sveltejs/kit';

const routeRegex = new RegExp(/^\/[^.]*([?#].*)?$/);

/** @type {import('@sveltejs/kit').Handle} */
export const handle: Handle = async ({ event, resolve }) => {
  const { url, request } = event;
  const { pathname } = url;

  // If this request is a route request
  if (routeRegex.test(pathname)) {
    // Get defined locales
    const supportedLocales = locales.get().map((l) => l.toLowerCase());

    // Try to get locale from `pathname`.
    // let locale = supportedLocales.find((l) => l === `${pathname.match(/[^/lp/]+?(?=\/|$)/)}`.toLowerCase());
    const baseCatalogs = base.split('/');
    let locale = supportedLocales.find(
      (l) => 
        l === `${pathname.match(`(?<=${baseCatalogs.map(elem => elem+'\/').join('')})([^\/]+)`)}`.toLowerCase().split(',')[0]
    );
   

    // We want to redirect the default locale to "no-locale" path
    if (locale === defaultLocale && !request.headers.get('prevent-redirect')) {
      const localeRegex = new RegExp(`\/${locale}`);
      const location = `${pathname}`.replace(localeRegex, '') || '/';
      console.log(location, pathname, localeRegex);
      return new Response(undefined, { headers: { location }, status: 301 });

      // If route locale is not supported
    } else if (!locale) {
      locale = defaultLocale;
    }

    // Add html `lang` attribute
    return resolve(
      { ...event, locals: { lang: locale } },
      {
        transformPageChunk: ({ html }) => html.replace('%lang%', `${locale}`),
      }
    );
  }

  return resolve(event);
};

/** @type {import('@sveltejs/kit').HandleServerError} */
export const handleError: ({
  event,
}: {
  event: HandleServerError & { locals: { [name: string]: string } };
}) => Promise<App.Locals> = async ({ event }) => {
  const { locals } = event;
  const { lang } = locals;

  await loadTranslations(lang, 'error');

  return locals;
};