Edge Functions Examples / Serve localized content

Serve localized content

You can use geolocation data to serve localized content according to country code.

Geolocation information is available on the Context.geo object.

export default async (request, context) => {
  const translations = {
    UNKNOWN: "Hello!",
    US: "Howdy y'all!",
    GB: "How do you do?",
    AU: "G'day, mate!",
  };

  const countryCode = context.geo?.country?.code || "UNKNOWN";
  const countryName = context.geo?.country?.name || "somewhere in the world";

  return new Response(`Your personalized greeting for ${countryName} is: ${translations[countryCode]}`, {
    headers: { "content-type": "text/html" },
  });
};

See this in action

Pro tip!

You can also combine geolocation data with URL rewrites for another way to serve internationalized content.

Explore more examples

What are Edge Functions?

Using JavaScript and TypeScript, Netlify Edge Functions give you the power to modify network requests to localize content, serve relevant ads, authenticate visitors, A/B test content, and much more! And this all happens at the Edge — directly from the worldwide location closest to each user.

To use Edge Functions on Netlify, add JavaScript or TypeScript files to an edge-functions directory in your project, and add [[edge_functions]] entries to your netlify.toml file.

Learn more in the docs.


Deploy this site to Netlify

Try out Edge Functions on Netlify today! Click the button below to deploy this site with all of its demos to your Netlify account.

Deploy to Netlify