1

We are doing I18N recently, and we have a menu to switch the language between language en, jp, and fr. Once we switch the language, the url become like below, there is a querystring append in the end of the url.

http://localhost:3030/loadTests?lang=en

Here is the question, because we don't append the lang parameter in all existing links and hrefs, if we click some link on the website, the url would be like:

http://localhost:3030/loadTests/1

the querystring lang=en didn't append in the end of the url, and we want the lang=en always append in the end of the url, how can i achieve it?

Hope i describe my question clearly.

Thanks in advance!

huan feng
  • 7,307
  • 2
  • 32
  • 56

1 Answers1

7

You can use Angular JS http Interceptors to do so.

    var app = angular.module('myApp', []);

    app.config(function ($httpProvider) {
         $httpProvider.interceptors.push(function ($q) {
             return {
                 'request': function (config) {
                     config.url = config.url + '?lang=en';
                     return config;
                 }

             }
         });
     });

Edit : In order to fetch the current query string parameter from the url, you can follow this answer. --> How can I get query string values in JavaScript?

Then in the code you can write like this :

config.url = config.url + '?lang=' + getParameterByName('lang');
Community
  • 1
  • 1
Manish Kr. Shukla
  • 4,447
  • 1
  • 20
  • 35
  • Instead of always using en, it needs to use what is currently in the querystring. – Wayne Ellery Dec 30 '14 at 07:03
  • what i need to intercept is the url in state.go(), config.url will catch all the http request url, rather than the url show in the browser. – huan feng Dec 30 '14 at 07:39
  • 1
    e.g. if the browser url is:http://localhost:3030/loadTests?lang=en, the real http request in the backend like http://.../en.json, http://.../xxx.html,http://.../xxx.png these kind of request can be capture in the config.url, but 'http://localhost:3030/loadTests?lang=en' is the ui-router state url, this cannot be captured by your interceptor, can you help on this? – huan feng Dec 30 '14 at 07:42
  • Are you making these requests through Angular ?? is yes, then this interceptor will intercept those requests – Manish Kr. Shukla Dec 30 '14 at 07:53