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

Fallback language not working when local storage is used #690

Closed
phw opened this issue Aug 26, 2014 · 2 comments
Closed

Fallback language not working when local storage is used #690

phw opened this issue Aug 26, 2014 · 2 comments
Milestone

Comments

@phw
Copy link

phw commented Aug 26, 2014

The fallback language is not working when useLocalStorage() is being used. That is the fallback language does work if one uses the filter, but not when using the directive or the $translate service directly.

This is how I set up the translations in my app.js:

angular.module('myApp', [
  'ngRoute',
  'pascalprecht.translate',
  'ngCookies',
  'myApp.filters',
  'myApp.services',
  'myApp.directives',
  'myApp.controllers'
]).
config(['$routeProvider', '$translateProvider', function($routeProvider, $translateProvider) {
  $routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
  $routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
  $routeProvider.otherwise({redirectTo: '/view1'});

  $translateProvider
    .translations('en', {
      'TEXT1': 'EN TEXT1',
      'TEXT2': 'EN TEXT2'
    })
    .translations('de', {
      'TEXT1': 'DE TEXT1'
    })
    .fallbackLanguage('en')
    .useLocalStorage() // If you comment this out everything works as expected
    .preferredLanguage('de')
    ;
}]);

Expected result is that for TEXT1 the German translation is used, but for TEXT2 it will fallback to the English translation. But the actual result is

<p>This is the partial for view 1.</p>

<h1>Display TEXT1 (should use requested language DE)</h1>
<div>{{ 'TEXT1' | translate }}</div> <!-- Ok, shows 'DE TEXT1' -->
<div translate="TEXT1"></div> <!-- Ok, shows 'DE TEXT1' -->

<h1>Display TEXT2 (should use fallback language EN)</h1>
<div>{{ 'TEXT2' | translate }}</div> <!-- Ok, shows 'DE TEXT2' -->
<div translate="TEXT2"></div> <!-- Shows the string identifier instead of the fallback -->

Using $translate in a controller will behave just like the translatedirective and will not show the fallback for TEXT2. If you disable the local storage the fallback works reliable.

@tspaeth
Copy link
Member

tspaeth commented Aug 26, 2014

Thanks for the nice example 👍 - makes it easy for us to find the problem.

And this one is indeed a bug somehow.
The reason is the code in

https://github.com/angular-translate/angular-translate/blob/master/src/service/translate.js#L764-L772

A quick fix (as I'm still thinking about the right fix) is to comment out

fallbackIndex = (index ...

Afterwards, directive and $translate should call your fallback translation as well.

@knalli
Copy link
Member

knalli commented Sep 17, 2014

landed as ac2f35c

@knalli knalli closed this as completed Sep 17, 2014
@knalli knalli added this to the 2.4.0 milestone Sep 17, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants