0

I'm just begining with angularJS and I want add the option of translation to my application.

I tried this fonction with one variable for my first test with 2 button which refer to 2 differents languages, the local language is ENGLISH and the second is FRENSH and it work but the probleme is after refreshing my application, this variable turn to the local language. how can I resolve this problem . this is my code on angularJS.

     'use strict';
     var app = angular.module('app', ['pascalprecht.translate']);
    app.config(function($translateProvider) {
     $translateProvider.fallbackLanguage('en');
     $translateProvider .translations('en', {
        msg : 'Hello',
      })
      $translateProvider.translations('fr', {
        msg : 'bonjour'
      });
      $translateProvider.preferredLanguage('en');
    });
    app.controller('Ctrl', function($translate, $scope) {
      $scope.changeLanguage = function (langKey) {
        $translate.use(langKey);
      };
    });

Thank you

Fatim
  • 155
  • 3
  • 3
  • 13
  • You can use cookies in AngularJS to maintain the values. See the reference answer - http://stackoverflow.com/questions/35480998/array-like-static-possible-in-angular-js/35481890#35481890 – Sumit Deshpande Apr 11 '16 at 15:12

2 Answers2

0

You will probably want to use some kind of a localStorage service to save the selected language.
You'll have to decide which kind of persistence (by session? by user?) you want to attain, to choose the best model of persistence to use...

MarcoS
  • 17,323
  • 24
  • 96
  • 174
0

You should use a way to persist data during life time of your aplication.

for example you can use cookies.

But if you want to do it angular way you can use $localStorage or $sessionStorage from this repo

there are other repositories as well.

After that when user refresh the page you can check for language information saved in your session($sessionStorage) or browser($localStorage) and display the right language.

as @MarcoS said, if you want to show another language for specific users you should store and get language information from database.

goodluck !

Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69