Skip to content

angular cli i18n not working in 1.0.4 #6418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vdumbrav opened this issue May 23, 2017 · 8 comments
Closed

angular cli i18n not working in 1.0.4 #6418

vdumbrav opened this issue May 23, 2017 · 8 comments
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent severity3: broken

Comments

@vdumbrav
Copy link

vdumbrav commented May 23, 2017

Bug Report or Feature Request (mark with an x)

- In version 1.0.0 work, but 1.0.4 not

Versions.

Output from: ng -v .
@angular/cli: 1.0.4
node: 7.10.0
os: linux x64
@angular/animations: 4.1.3
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/compiler-cli: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/platform-server: 4.1.3
@angular/router: 4.1.3
@angular/cli: 1.0.4

Repro steps.

The log given by the failure.

Failed to load resource: the server responded with a status of 404 (Not Found)
polyfills.bundle.js:723 Unhandled Promise rejection: xliff parse errors:
Unexpected closing tag "head". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<meta charset="utf-8">
<title>Error</title>
[ERROR ->]</head>
<body>
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
"): i18n@5:0
Unexpected closing tag "html". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
</body>
[ERROR ->]</html>
"): i18n@9:0: Error during instantiation of I18NHtmlParser! (Compiler -> JitCompiler -> CompileMetadataResolver -> DirectiveNormalizer -> HtmlParser -> I18NHtmlParser). ; Zone: <root> ; Task: Promise.then ; Value: Error: xliff parse errors:
Unexpected closing tag "head". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<meta charset="utf-8">
<title>Error</title>
[ERROR ->]</head>
<body>
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
"): i18n@5:0
Unexpected closing tag "html". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
</body>
[ERROR ->]</html>
"): i18n@9:0: Error during instantiation of I18NHtmlParser! (Compiler -> JitCompiler -> CompileMetadataResolver -> DirectiveNormalizer -> HtmlParser -> I18NHtmlParser).
    at wrappedError (vendor.bundle.js:1544)
    at injectionError (vendor.bundle.js:1590)
    at instantiationError (vendor.bundle.js:1688)
    at ReflectiveInjector_._instantiate (vendor.bundle.js:3075)
    at ReflectiveInjector_._instantiateProvider (vendor.bundle.js:3048)
    at ReflectiveInjector_._new (vendor.bundle.js:3029)
    at ReflectiveInjector_._getObjByKeyId (vendor.bundle.js:3111)
    at ReflectiveInjector_._getByKeyDefault (vendor.bundle.js:3159)
    at ReflectiveInjector_._getByKey (vendor.bundle.js:3100)
    at ReflectiveInjector_._getByReflectiveDependency (vendor.bundle.js:3084) Error: xliff parse errors:
Unexpected closing tag "head". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<meta charset="utf-8">
<title>Error</title>
[ERROR ->]</head>
<body>
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
"): i18n@5:0
Unexpected closing tag "html". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
</body>
[ERROR ->]</html>
"): i18n@9:0: Error during instantiation of I18NHtmlParser! (Compiler -> JitCompiler -> CompileMetadataResolver -> DirectiveNormalizer -> HtmlParser -> I18NHtmlParser).
    at wrappedError (http://192.168.84.169:4200/vendor.bundle.js:1544:34)
    at injectionError (http://192.168.84.169:4200/vendor.bundle.js:1590:52)
    at instantiationError (http://192.168.84.169:4200/vendor.bundle.js:1688:12)
    at ReflectiveInjector_._instantiate (http://192.168.84.169:4200/vendor.bundle.js:3075:19)
    at ReflectiveInjector_._instantiateProvider (http://192.168.84.169:4200/vendor.bundle.js:3048:25)
    at ReflectiveInjector_._new (http://192.168.84.169:4200/vendor.bundle.js:3029:21)
    at ReflectiveInjector_._getObjByKeyId (http://192.168.84.169:4200/vendor.bundle.js:3111:41)
    at ReflectiveInjector_._getByKeyDefault (http://192.168.84.169:4200/vendor.bundle.js:3159:45)
    at ReflectiveInjector_._getByKey (http://192.168.84.169:4200/vendor.bundle.js:3100:25)
    at ReflectiveInjector_._getByReflectiveDependency (http://192.168.84.169:4200/vendor.bundle.js:3084:21)
api.onUnhandledError @ polyfills.bundle.js:723
polyfills.bundle.js:725 Error: Uncaught (in promise): Error: xliff parse errors:
Unexpected closing tag "head". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<meta charset="utf-8">
<title>Error</title>
[ERROR ->]</head>
<body>
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
"): i18n@5:0
Unexpected closing tag "html". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
</body>
[ERROR ->]</html>
"): i18n@9:0: Error during instantiation of I18NHtmlParser! (Compiler -> JitCompiler -> CompileMetadataResolver -> DirectiveNormalizer -> HtmlParser -> I18NHtmlParser).
Error: xliff parse errors:
Unexpected closing tag "head". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<meta charset="utf-8">
<title>Error</title>
[ERROR ->]</head>
<body>
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
"): i18n@5:0
Unexpected closing tag "html". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("
<pre>Cannot GET /src/app/locale/messages.ro.xliff</pre>
</body>
[ERROR ->]</html>
"): i18n@9:0: Error during instantiation of I18NHtmlParser! (Compiler -> JitCompiler -> CompileMetadataResolver -> DirectiveNormalizer -> HtmlParser -> I18NHtmlParser).
    at wrappedError (vendor.bundle.js:1544)
    at injectionError (vendor.bundle.js:1590)
    at instantiationError (vendor.bundle.js:1688)
    at ReflectiveInjector_._instantiate (vendor.bundle.js:3075)
    at ReflectiveInjector_._instantiateProvider (vendor.bundle.js:3048)
    at ReflectiveInjector_._new (vendor.bundle.js:3029)
    at ReflectiveInjector_._getObjByKeyId (vendor.bundle.js:3111)
    at ReflectiveInjector_._getByKeyDefault (vendor.bundle.js:3159)
    at ReflectiveInjector_._getByKey (vendor.bundle.js:3100)
    at ReflectiveInjector_._getByReflectiveDependency (vendor.bundle.js:3084)
    at wrappedError (vendor.bundle.js:1544)
    at injectionError (vendor.bundle.js:1590)
    at instantiationError (vendor.bundle.js:1688)
    at ReflectiveInjector_._instantiate (vendor.bundle.js:3075)
    at ReflectiveInjector_._instantiateProvider (vendor.bundle.js:3048)
    at ReflectiveInjector_._new (vendor.bundle.js:3029)
    at ReflectiveInjector_._getObjByKeyId (vendor.bundle.js:3111)
    at ReflectiveInjector_._getByKeyDefault (vendor.bundle.js:3159)
    at ReflectiveInjector_._getByKey (vendor.bundle.js:3100)
    at ReflectiveInjector_._getByReflectiveDependency (vendor.bundle.js:3084)
    at resolvePromise (polyfills.bundle.js:850)
    at polyfills.bundle.js:901
    at ZoneDelegate.webpackJsonp.1257.ZoneDelegate.invokeTask (polyfills.bundle.js:505)
    at Zone.webpackJsonp.1257.Zone.runTask (polyfills.bundle.js:272)
    at drainMicroTaskQueue (polyfills.bundle.js:665)
    at <anonymous>

Desired functionality.

import { Injectable, TRANSLATIONS, TRANSLATIONS_FORMAT, LOCALE_ID } from '@angular/core';

@Injectable()
export class LocalizationService {
  static language: string;

  static init() {
    var lang = window.localStorage.getItem("language");
    if (lang) {
      console.log("Saved language found.");
      this.language = lang;
    }
    else {
      var anyNavigator = navigator as any;
      var language: string = anyNavigator.languages && anyNavigator.languages[0] || // Chrome / Firefox
        anyNavigator.language ||   // All browsers
        anyNavigator.userLanguage; // IE <= 10
      lang = language.substring(0, 2);
      this.language = lang;
    }
  }

  static getTranslationProviders(): Promise<Object[]> {
    this.init();
    // Get the locale id from the global
    const locale = this.language;
    // return no providers if fail to get translation file for locale
    const noProviders: Object[] = [];
    // No locale or U.S. English: no translation providers
    if (!locale || locale === 'en') {
      return Promise.resolve(noProviders);
    }
    // Ex: 'locale/messages.es.xlf`
  const translationFile = `./src/app/locale/messages.${locale}.xliff`;
    return this.getTranslations(translationFile)
      .then((translations: string) => [
        { provide: TRANSLATIONS, useValue: translations },
        { provide: TRANSLATIONS_FORMAT, useValue: 'xliff' },
        { provide: LOCALE_ID, useValue: locale }
      ])
      .catch(() => noProviders); // ignore if file not found
  }

  static getTranslations(file: string): Promise<string> {
    return (window as any).fetch(file).then((response) => {
      return response.text();
    });
  }
}

Mention any other details that might be useful.

@clydin
Copy link
Member

clydin commented May 23, 2017

Static assets accessed during runtime are required to be configured (or their containing directory) as assets within .angular-cli.json.

@vdumbrav
Copy link
Author

@clydin In version 1.0.0 work, but 1.0.4 not
I dynamically connect files (.xliff) when changing the language

@clydin
Copy link
Member

clydin commented May 23, 2017

There was a defect present prior to 1.0.1 that allowed the entire project to be accessible via ng serve. You can read through this issue comment for further details: #6158 (comment)

@ocombe
Copy link
Contributor

ocombe commented May 23, 2017

Do you have a copy of your xliff file?

@Brocco Brocco added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent severity3: broken labels May 23, 2017
@vdumbrav
Copy link
Author

@ocombe
xliff - http://textuploader.com/d970i
generate file - ./node_modules/.bin/ng-xi18n --i18nFormat=xlf --locale=ru
translate - http://xliff.brightec.co.uk/

@ocombe
Copy link
Contributor

ocombe commented Jul 18, 2017

Hard to reproduce without the original application.
I don't see anything wrong with the xliff file.

I'm guessing that you use your LocalizationService to provide providers/translations at bootstrap?
Is there any chance that you can give us access to the app code so that we can reproduce the issue? Or even better that you can make a plunkr that reproduces the bug?

@mervinl
Copy link

mervinl commented Jul 27, 2017

try xlf instead of xliff in translations_format

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent severity3: broken
Projects
None yet
Development

No branches or pull requests

5 participants