1. 程式人生 > 實用技巧 >設定網頁翻譯的小技巧

設定網頁翻譯的小技巧

網頁翻譯:看了網上很多答案都是利用隱藏寫多個模組,這裡可以利用瀏覽器來完成網頁翻譯,通過

// 設定英語語言程式碼
response.setHeader("Content-Language", "en");給頁面設定語言屬性,谷歌會跟瀏覽器語言進行比對,並彈出翻譯的按鈕
例子:
package Servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.util.Locale;
@WebServlet("/hai")

public class Del extends HttpServlet{

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// 設定響應內容型別
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

// 設定英語語言程式碼
response.setHeader("Content-Language", "en");

String title = "En Español";
String docType = "<!DOCTYPE html> \n";
/* out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +

"<body bgcolor=\"#f0f0f0\">\n" +
"<h1>" + "En Espa&ntilde;ol:" + "</h1>\n" +
"<h1>" + "&iexcl;Hola Mundo!" + "</h1>\n" +
"</body></html>");*/
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1>" + "english:" + "</h1>\n" +
"<h1>" + "hello world!" + "</h1>\n" +
"</body></html>");
}
}