Methods for Website Internationalization (Providing Multi-language Content)

For websites that use server-side templating engines (e.g., Laravel’s Blade) to render HTML documents, the approach to localization (internationalization) is as follows: For common pages like login and registration, you can use the __() function to translate text based on the lang= query parameter passed from the frontend. For other pages, the language-specific views should be placed in directories like resources/views/{language_code} (for Laravel) or public/{language_code} (where “public” refers to the web root directory).

For fully decoupled applications, such as those where the frontend is developed using MVVM frameworks like Vue.js or React.js for SPAs, and the backend is built with Laravel to provide APIs, localization methods differ. In this case, you should add language-specific subdomains at the domain level, such as cn.vuejs.org for the Chinese Vue.js site, ja.vuejs.org for the Japanese site, with the default vuejs.org serving the English version. Additionally, the database should support language-specific partitioning, where each language code has its own set of tables or databases.

网站国际化(提供多国语言内容)的方法

对于前端使用服务器端模板引擎(例如Laravel的blade)渲染输出HTML文档的开发方式,本地化(国际化)的方法是,通用的页面,例如登陆注册,可以使用__()函数根据前端传过来的lang=语言代码查询参数翻译文本。其他不同用的页面应该放在resources/views/语言代码目录(对于Laravel)或public/语言代码目录(public代表web根目录)下。还有一种方法是在HTML文档中使用lang=’xxx’属性标记HTML元素的内容所使用的语言,再使用:lang伪类选择器根据前端传过来的lang=语言代码,选择显示特定语言的HTML元素(由服务器端模板引擎实现),其他语言的元素都不显示(由服务器端模板引擎设置display:none;)。例如:

<style>
/*显示中文内容*/
:lang(zh_CN){
    …
}
:lang(fr){
display:none;/*不显示法文内容*/
    …
}
</style>

<div lang='zh_CN'>中文内容……</div>
<div lang='fr'>法文内容……</div>

这种方法适用于内容比较简单的国际化网页。

对于前后端完全分离的应用程序,例如前端使用Vue.js、React.js等MVVM框架开发SPA,服务器端使用Laravel开发并提供API给前端调用,本地化(国际化)的方法是,在域名层面添加语言代码子域,例如cn.vuejs.org、ja.vuejs.org分别显示中文Vue.js官网和日文Vue.js官网,主站vuejs.org默认英文Vue.js官网。 数据库也应该根据语言代码分库分表。