AUTO LANGUAGE button
Two-language site on /vn and /en: sends Vietnamese visitors to /vn and everyone else to /en, remembers the choice for a year, adds a VI / EN button, and never redirects Google’s crawler.
Tag name: <nut-tu-chuyen-ngon-ngu> · Vietnamese name: TỰ CHUYỂN NGÔN NGỮ
Open the demo on its own page →
Install in one minute
Upload nut-tu-chuyen-ngon-ngu.js to your site, then paste this where the button should appear:
<script defer src="/nut/nut-tu-chuyen-ngon-ngu.js"></script>
<nut-tu-chuyen-ngon-ngu></nut-tu-chuyen-ngon-ngu>What it does
- International visitors (browser not in Vietnamese and time zone not Vietnam) are sent to
yoursite/en/.... - Vietnamese visitors are sent to
yoursite/vn/.... - The choice is remembered for a year, and the VI / EN button switches by hand at any time.
- Google’s crawler is never redirected, so both versions get indexed.
Inner pages keep their path: /vn/post/abc ↔ /en/post/abc. Want English right at the site root (no /en)? See the LANGUAGE SWITCH button.
Redirect from the top of the page (recommended)
The button loads last, so visitors may see a brief flash before being redirected. Paste this right after the <head> tag so the page redirects before it paints; it shares the lang cookie with the button:
<script>(function(){var P={vi:"/vn",en:"/en"},K="lang",h=location.pathname,m=document.cookie.match(new RegExp("(?:^|; )"+K+"=(vi|en)")),c=m&&m[1];if(!c){if(/bot|crawl|spider|slurp|lighthouse|headless|preview|facebookexternalhit/i.test(navigator.userAgent))return;var v=(navigator.languages||[navigator.language||""]).some(function(l){return/^vi/i.test(l)});try{var z=Intl.DateTimeFormat().resolvedOptions().timeZone;if(z==="Asia/Ho_Chi_Minh"||z==="Asia/Saigon")v=true}catch(e){}c=v?"vi":"en";document.cookie=K+"="+c+";path=/;max-age=31536000;SameSite=Lax"}var a=h===P.vi||h.indexOf(P.vi+"/")===0?"vi":h===P.en||h.indexOf(P.en+"/")===0?"en":"",g=a?h.slice(P[a].length)||"/":h;if(c!==a)location.replace(P[c]+g+location.search+location.hash)})();</script>Server-side redirect (optional)
PHP sites can redirect on the server, which is fastest and works without JavaScript:
<?php
// Đặt ở đầu index.php. /vn = tiếng Việt · /en = tiếng Anh · đường dẫn khác -> đưa về đúng bản của khách
$duong = strtok($_SERVER['REQUEST_URI'], '?');
$bot = preg_match('/bot|crawl|spider|preview/i', $_SERVER['HTTP_USER_AGENT'] ?? '');
if (!preg_match('#^/(vn|en)(/|$)#', $duong) && !$bot) {
$chon = $_COOKIE['lang'] ?? '';
if ($chon !== 'vi' && $chon !== 'en') { // lần đầu: đoán theo ngôn ngữ trình duyệt
$chon = preg_match('/^\s*vi\b/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '') ? 'vi' : 'en';
setcookie('lang', $chon, time() + 31536000, '/'); // nhớ một năm
}
header('Vary: Accept-Language, Cookie');
header('Location: ' . ($chon === 'vi' ? '/vn' : '/en') . $duong, true, 302); exit;
}
$ngonNgu = strpos($duong, '/vn') === 0 ? 'vi' : 'en'; // dùng cho <html lang="..."> và nội dung trangAdd these three lines to <head> so Google understands the two language versions:
<link rel="alternate" hreflang="vi" href="https://tenmien.com/vn/">
<link rel="alternate" hreflang="en" href="https://tenmien.com/en/">
<link rel="alternate" hreflang="x-default" href="https://tenmien.com/en/">Button attributes
| Attribute | Default | Meaning |
|---|---|---|
tien-to-vn | /vn | Path of the Vietnamese version. |
tien-to-en | /en | Path of the English version. |
tu-dong | co | Send visitors to their version, by remembered choice or by browser language on the first visit. Set khong if you already use the head snippet or the server redirect. |
cookie | lang | Name of the cookie that stores the choice (vi or en) for one year. |
Shared attributes
| Attribute | Meaning |
|---|---|
mau | Button background, set it to your brand color |
mau-chu | Text and icon color |
nhan | Button text |
kieu | icon = 44px round icon only · vien = outline button |
rong | Full-width button |
ghi-luot | Path to the PHP file that logs clicks |
Version history
- 1.0.0 (2026-10-06) — First release: /vn and /en versions, automatic redirect by browser language and time zone, choice remembered for a year, search crawlers left alone, plus a head snippet and a server-side redirect snippet.