ngx-translate / core

The internationalization (i18n) library for Angular
MIT License
4.5k stars 574 forks source link

Race condition when changing the languages #224

Open tueena opened 8 years ago

tueena commented 8 years ago

I'm submitting a ... (check one with "x")

[x ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior

ng2-translate changes the language, when a requested language JSON file arrives at the client, regardless of the value of the last call to use().

Expected/desired behavior

When a request for a language JSON file arrives at the client, ng2-translate should change the language only, if the arrived language data is for the language, that has been chosen last.

Reproduction of the problem

For example delay the delivery of the json files by some seconds. If you have an apache and php like this:

RewriteRule json$ delay.php [L]

delay.php:

<?php
sleep(5);
echo file_get_contents(__DIR__ . '/' . $_SERVER['REQUEST_URI']);
  1. Chose a language and wait until the json file is downloaded.
  2. Switch to another language and before this new json file has been downloaded:
  3. Switch back to the first language.

It should also be reproducible without any delay, if you use('en') in a component constructor and then in a click handler use('fr'); use('en');.

What is the expected behavior?

The last selected language should be visible. Any pending, outdated language selection should have no effect.

What is the motivation / use case for changing the behavior?

I'm setting the language in my app component. The languages are not reflected in the routes. But some routes contain a language code. The language is set by the specific components, that handle those routes requests. Maybe not the best approach, but changing the language according to the order of the use() calls would be the correct behavior anyways.

Please tell us about your environment:

ocombe commented 7 years ago

Hello, you're right, we should cancel the current observable when we call change lang again