forked from netlify/edge-functions-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (37 loc) · 1.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import repoLink from "../../components/repo-link.js";
export default {
title: "Serve localized content",
metaDescription: "Use geolocation data to serve localized content according to country.",
page: function () {
return `
<section>
<h1>Serve localized content</h1>
<p>You can use geolocation data to serve localized content according to country code.</p>
<p>Geolocation information is available on the <code>Context.geo</code> object.</p>
<pre><code>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" },
});
};
</code></pre>
<h2>See this in action</h2>
<ul>
<li><a href="/localized-content">Get your localized content from the Edge Function</a></li>
<li>${repoLink("localized-content.js")}</li>
</ul>
<div class="protip">
<h2>Pro tip!</h2>
<p>You can also combine geolocation data with <a href="/example/rewrite">URL rewrites</a> for another way to serve internationalized content.</p>
</div>
</section>
`;
},
};