Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Error when trying too translate a falsy value with translate filter #1511

Closed
4kochi opened this issue Jun 6, 2016 · 6 comments
Closed

Error when trying too translate a falsy value with translate filter #1511

4kochi opened this issue Jun 6, 2016 · 6 comments
Labels
Milestone

Comments

@4kochi
Copy link

4kochi commented Jun 6, 2016

Subject of the issue

When using a falsy JavaScript value the translation filter does not work and raises an exception. It works with the translate directive.

Our use case are validation messages with ng-message

<div ng-message="server">
    <span 
        translate="{{form.age.$error.serverMsg}}" 
        translate-value-expected="{{form.age.$error.expected | translate}}">
    </span>
</div>

The corresponding translation key looks like this:

"SERVER_VALIDATION_MINIMUM": "Wert muß größer/gleich {{expected}} sein"

Your environment

  • 2.11.0
  • 1.5.5
  • Safari Technology Preview 9.1.1

Steps to reproduce

Does not work: <p>{{ 0 | translate }}</p>
Works: <p translate>{{ 0 }}</p>

Expected behavior

The value should be translated. Or at least returned as is if there is no translation key.

Actual behavior

Exception in the console:

Error: text.replace is not a function. (In 'text.replace(escapedStartRegexp, startSymbol)', 'text.replace' is undefined)

bildschirmfoto 2016-06-06 um 14 37 14

@knalli
Copy link
Member

knalli commented Jun 6, 2016

It looks like the filter is missing a "is string" condition.

@knalli knalli added the bug label Jun 6, 2016
@knalli knalli added this to the 2.11.1 milestone Jun 6, 2016
knalli added a commit that referenced this issue Jun 26, 2016
@knalli
Copy link
Member

knalli commented Jun 26, 2016

@4kochi I have add some tests.. but they are not failing. What I'm missing here?

@4kochi
Copy link
Author

4kochi commented Jun 26, 2016

Ok. Thanks. I will have a look.

@4kochi
Copy link
Author

4kochi commented Jun 27, 2016

Seems to be a problem with angular 1.5.x. The command TEST_SCOPE=angular_1.5.x grunt test fails. The other angular versions are fine.

@knalli
Copy link
Member

knalli commented Jun 27, 2016

https://travis-ci.org/angular-translate/angular-translate/builds/140397869

Yeah, yesterday I have thought it was only temporarily. Looks like there has been changed something.

knalli added a commit that referenced this issue Jul 17, 2016
Since AngularJS 1.5, the input values (i.e. of a filter) will used 'as is' which means numbers (as translation ids) will not be transformed to strings anymore. This generates unexpected errors.

Because this fix, the result is that explicit typed number values will be return (transformed as a string).

```
// before and < AJS 1.5
$filter('translate)(0) === 0
// after and < AJS 1.5
$filter('translate)(0) === '0'

// before and AJS 1.5+
$filter('translate)(0) exception
// after and AJS 1.5+
$filter('translate)(0) === '0'

```

Fixes #1511
@knalli
Copy link
Member

knalli commented Jul 17, 2016

Got it.

@knalli knalli closed this as completed Jul 17, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants