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
- Get your localized content from the Edge Function
- The Edge Function code: localized-content.js
Pro tip!
You can also combine geolocation data with URL rewrites for another way to serve internationalized content.
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!
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 a
/netlify/edge-functions
directory in your project.
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.