123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import { logger } from 'log';
- import { Cookies, SetCookie } from 'cookies';
-
- function getLanguageCookieValue(country) {
- let languageCookieValue = '';
- if (country === 'US') {
- languageCookieValue = 'en_US';
- } else if (country === 'KR') {
- languageCookieValue = 'ko_KR';
- } else if (country === 'ES') {
- languageCookieValue = 'es_ES';
- } else if (country === 'FR') {
- languageCookieValue = 'fr_FR';
- } else if (country === 'DE') {
- languageCookieValue = 'de_DE';
- }
- return languageCookieValue;
- }
-
- export function onClientRequest(request) {
- let cookies = new Cookies(request.getHeader('Cookie'));
- let languageCookie = cookies.get('language');
- logger.log('language cookie value: %s', languageCookie);
- if (languageCookie) {
- logger.log('language cookie exists. Do nothing.');
- } else {
- let country = request.userLocation.country;
- if (country) {
- let languageCookieValue = getLanguageCookieValue(country);
- if (languageCookieValue) {
- let headers = { 'Location': ['/'], 'X-Redirect': [languageCookieValue] };
- request.respondWith(302, headers, {});
- } else {
- logger.log('cannot find languageCookieValue. Do nothing.');
- }
- } else {
- logger.log('cannot find country code. Do nothing.');
- }
- }
- }
-
- export function onClientResponse(request, response) {
- logger.log('Adding a header in ClientResponse');
- let languageCookieValue = response.getHeader('X-Redirect');
- if (languageCookieValue) {
- let cookie = new SetCookie();
- cookie.name = 'language';
- cookie.value = languageCookieValue;
- response.setHeader('Set-Cookie', cookie.toHeader());
- response.setHeader('X-GeoRedirect', 'True');
- } else {
- response.setHeader('X-GeoRedirect', 'False')
- }
- }
|