防止Angular将基本href与语言代码一起更新

15

我遇到了 Angular v9 的问题。我已经将 i18n 本地化功能引入了我的应用程序中。当我尝试使用特定的位置构建我的应用程序时,它会不断将子文件夹添加到基础 href 路径中(但我希望将文件保留在子文件夹中)。

复现步骤:

  1. 生成虚拟项目 ng new Project
  2. 构建生产配置 ng build --prod --localize
  3. 文件被构建到dist/en-us/文件夹中,但index.html具有<base href="/en-US/">

是否有任何方法强制 Angular 停止将语言代码添加到基础 href 中?这会导致所有应用链接都包含语言代码,这是不必要的。

我的部署配置是根据目录部署应用程序到不同的位置,每个位置都将位于根目录下:

  • hxxp://app.fr/在/fr/文件夹中的根目录
  • hxxp://app.de/在/de/文件夹中的根目录
5个回答

49

我曾遇到同样的问题,并在此问题中找到了解决方案:

https://github.com/angular/angular-cli/issues/17260

你需要为每个locale和源locale编写baseHref。

"i18n": {
    "sourceLocale": {
        "code": "en",
        "baseHref": ""
    },
    "locales": {
        "fr": {
            "baseHref": "",
            "translation": "src/locale/messages.fr.xlf"
        }
    }
}

4

这是我的解决方案,但它会破坏 Angular v.9 的新功能。

解决方案是不使用 --localize 标志。

因此,我移除了这个定义:

"i18n": {
    "sourceLocale": "de",
    "locales": {
      "fr": "src/app/locale/messages.fr.xlf",
}

然后我在以下位置创建了自定义配置:

  "configurations": {
    "fr": {
      "i18nFile": "src/app/locale/messages.fr.xlf",
      "i18nLocale": "fr",
      "outputPath": "dist/fr"
    }

并且仅仅被称为:
ng build --configuration=fr

但那很丑...因此等待更好的想法 :)


标记我的回答,因为没有更好的答案 :( - Gumowy Kaczak

1

您可以使用以下代码覆盖 baseHref:

"locales": {
  "de": {
    "translation": "src/locale/messages.de.xlf",
    "baseHref": "/"
  },
....
}

0

在构建项目时,您可以停止在dist中生成语言环境。例如:/dist/project_name/en_us/。 从angular.json文件中删除以下行。

"localize": true,
"i18nMissingTranslation": "error",

0
在 Dockerfile 中:
RUN sed -i "s/<base.*//" /usr/share/nginx/html/nl/index.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接