Next.js Discord

Discord Forum

About i18n / Api | Route process.

Unanswered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
Hello, I'm stuck about the en/de/nl process. Actually it is working in the main page but I can not fetch data, it is always fetching the locale first. Like: ContactForm.jsx:35
POST http://localhost:3000/tr/api/route 404 (Not Found)

My code:
const handleSubmit = async (e) => {
    e.preventDefault();
    try {
      const response = await fetch("/api/route", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(formData),
      });

      if (response.ok) {
        const data = await response.json();
        setSuccessMessage(data.message);
      } else {
        throw new Error("E-mail did not send. Please try again.");
      }
    } catch (error) {
      console.error(error);
      setErrorMessage(t("Contact16"));
    }
  };


Also:
import nodemailer from "nodemailer";

export default async function handler(req, res) {
  if (req.method === "POST") {
    console.log('Gelen veri:', req.body);
    try {
      const { name, email, company, phone, message, budget } = req.body;

      const transporter = nodemailer.createTransport({
        host: "smtp-mail.outlook.com",
        secureConnection: false,
        port: 587,
        tls: {
            ciphers: 'SSLv3'
        },
        auth: {
          user: process.env.MAIL_USER,
          pass: process.env.MAIL_PASS,
        },
      });

      const mailOptions = {
        from: process.env.MAIL_USER,
        to: process.env.MAIL_TO,
        subject: "New Job Request",
        text: `
          Name: ${name}
          Email: ${email}
          Company: ${company}
          Phone: ${phone}
          Message: ${message}
          Budget: ${budget}
        `,
      };

      await transporter.sendMail(mailOptions);

      res.status(200).json({ success: true, message: "E-mail sent!" });
    } catch (error) {
      console.error(error);
      res
        .status(500)
        .json({
          success: false,
          message: error.message,
        });
    }
  } else {
    res.status(405).end();
  }
}


It is fetching the wrong locale and I did not fix. Also it contains the different error like pathname. So I'm trying to fix all of them.
Also my middleware:
import createMiddleware from "next-intl/middleware";
import { pathnames, locales, localePrefix } from "./config";

export default createMiddleware({
  defaultLocale: "en",
  locales,
  pathnames,
  localePrefix,
});



export const config = {
  matcher: [
    // Enable a redirect to a matching locale at the root
    "/",
    

    // Set a cookie to remember the previous locale for
    // all requests that have a locale prefix
    "/(tr|de|en)/:path*",

    // Enable redirects that add missing locales
    // (e.g. `/pathnames` -> `/en/pathnames`)
    "/((?!_next|_vercel|.*\\..*).*)",
  ],
};


i18n file
import { notFound } from "next/navigation";
import { getRequestConfig } from "next-intl/server";
import { locales } from "./config";

export default getRequestConfig(async ({ locale }) => {
  // Validate that the incoming `locale` parameter is valid
  if (!locales.includes(locale)) notFound();

  return {
    messages: (
      await (locale === "en"
        ? // When using Turbopack, this will enable HMR for `en`
          import("./messages/en.json")
        : import(`./messages/${locale}.json`))
    ).default,
    timeZone: "UTC",
  };
});


config
export const locales = ["en", "tr", "de"];

export const pathnames = {
  "/": "/",
};

// Use the default: `always`
export const localePrefix = undefined;


Thanks for helping...

6 Replies

Barbary LionOP
&752637460550385834
only ping moderators for moderation-related issues, do not ping us for code help
Barbary LionOP
sure...
could you @ any helper?
no. you may bump your thread by sending any messages here once per day for it to go up to the top of the forum, and make improvements to your question and the thread title, but other than that you cannot do anything. posts here are not guaranteed to get help, all moderators and helpers are volunteers
@Barbary Lion Hello, I'm stuck about the en/de/nl process. Actually it is working in the main page but I can not fetch data, it is always fetching the locale first. Like: ContactForm.jsx:35 POST http://localhost:3000/tr/api/route 404 (Not Found) My code: const handleSubmit = async (e) => { e.preventDefault(); try { const response = await fetch("/api/route", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(formData), }); if (response.ok) { const data = await response.json(); setSuccessMessage(data.message); } else { throw new Error("E-mail did not send. Please try again."); } } catch (error) { console.error(error); setErrorMessage(t("Contact16")); } }; Also: import nodemailer from "nodemailer"; export default async function handler(req, res) { if (req.method === "POST") { console.log('Gelen veri:', req.body); try { const { name, email, company, phone, message, budget } = req.body; const transporter = nodemailer.createTransport({ host: "smtp-mail.outlook.com", secureConnection: false, port: 587, tls: { ciphers: 'SSLv3' }, auth: { user: process.env.MAIL_USER, pass: process.env.MAIL_PASS, }, }); const mailOptions = { from: process.env.MAIL_USER, to: process.env.MAIL_TO, subject: "New Job Request", text: ` Name: ${name} Email: ${email} Company: ${company} Phone: ${phone} Message: ${message} Budget: ${budget} `, }; await transporter.sendMail(mailOptions); res.status(200).json({ success: true, message: "E-mail sent!" }); } catch (error) { console.error(error); res .status(500) .json({ success: false, message: error.message, }); } } else { res.status(405).end(); } } It is fetching the wrong locale and I did not fix. Also it contains the different error like pathname. So I'm trying to fix all of them. Also my middleware: import createMiddleware from "next-intl/middleware"; import { pathnames, locales, localePrefix } from "./config"; export default createMiddleware({ defaultLocale: "en", locales, pathnames, localePrefix, }); export const config = { matcher: [ // Enable a redirect to a matching locale at the root "/", // Set a cookie to remember the previous locale for // all requests that have a locale prefix "/(tr|de|en)/:path*", // Enable redirects that add missing locales // (e.g. `/pathnames` -> `/en/pathnames`) "/((?!_next|_vercel|.*\\..*).*)", ], }; i18n file import { notFound } from "next/navigation"; import { getRequestConfig } from "next-intl/server"; import { locales } from "./config"; export default getRequestConfig(async ({ locale }) => { // Validate that the incoming `locale` parameter is valid if (!locales.includes(locale)) notFound(); return { messages: ( await (locale === "en" ? // When using Turbopack, this will enable HMR for `en` import("./messages/en.json") : import(`./messages/${locale}.json`)) ).default, timeZone: "UTC", }; }); config export const locales = ["en", "tr", "de"]; export const pathnames = { "/": "/", }; // Use the default: `always` export const localePrefix = undefined; Thanks for helping...
You are using the code on api route from page router.
Try to follow the doc for route handler and change the code