Docker+Nginx部署Angular国际化i18n

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wf19930209/article/details/82796310

Docker+Nginx部署Angular国际化i18n

在Angular项目中添加default.conf文件

default.conf
为了支持局域网,增加一个域名,即本地的局域网ip地址。

server {
    listen       80;
    server_name  localhost;
    server_name  192.168.2.172;

    location / {
        root   /usr/share/nginx/html;

        location ~* /([a-z\-)]+)/ {
            try_files $uri /$1/index.html /index.html;
        }

        try_files $uri $uri/index.html /index.html;
    }
}

package.json

...
"build-i18n:zh": "ng build --prod --outputPath=dist/zh --i18nFile=src/locale/messages.zh.xlf --i18nFormat=xlf --locale=zh-Hans",
"build-i18n:en": "ng build --prod --outputPath=dist/en --i18nFile=src/locale/messages.en.xlf --i18nFormat=xlf --locale=en-US",
"build-i18n": "npm run build-i18n:zh; npm run build-i18n:en",
"start:docker": "docker run --rm --name i18n -v $PWD/dist:/usr/share/nginx/html:ro -v $PWD/default.conf:/etc/nginx/conf.d/default.conf:ro -p 8090:80 -d nginx",
"restart:docker": "docker restart i18n"
...

–rm:容器停止运行后,自动删除容器文件

  • 编译翻译文件
    在dist目录下生成zhen两个文件夹。
npm run build-i18n
  • 启动临时容器
    这个容器使用stop命令停止的时候,就消失了,如果想要一直存在需要将–rm参数去掉。
npm run start:docker
npm run restart:docker # 需要重新启动生效

打开浏览器输入 http://localhost:8090/zh 访问英文网站,http://localhost:8090/en 访问中文网站。

猜你喜欢

转载自blog.csdn.net/wf19930209/article/details/82796310