Learn MVC : Using Angular Dynamic Localization

Introduction

This article demonstrates how to use Agular dynamic localization in MVC using Angular i18n package in Visual Studio 2017. 

Localization

Localization is a very important part of region based applications. It customizes your application for given culture and locale. So, this localization can change dynamically using Angular Dynamic Locale Module.

By following the below steps, you can use Angular file upload in MVC.

  • Create MVC Project
  • Configure Angular i18n & Dynamic Locale
  • Work in Angular i18n & Dynamic Locale

Create MVC Project

Open Visual Studio 2017.

Angular

Go to New menu > click New & project. Now, it will open New Project window.

Angular

You can select ASP.NET Web Application on Framework 4.5. Enter the name of project in “Solution name” textbox, then click OK button.

Angular

One more window should be appearing. Select MVC Template in this popup & click OK button. Now, start cropping the image.

Configure Angular i18n & Dynamic Locale

You can download the plug-in from

Or you can install from npm.  If you try npm, don’t forget to install Node JS. Then, use the following commands.

npm install angular-i18n

npm install angular-dynamic-locale

Open the _Layout.cshtml and must refer the .js file from downloaded folder to this View page.

  1. <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />  
  2.  <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  3.  <script src="~/Scripts/bootstrap.min.js"></script>  
  4.  <script src="~/PlugIn/angular/angular.min.js"></script>  
  5.  <script src="~/PlugIn/angular-dynamic-locale/dist/tmhDynamicLocale.js"></script>  
  6.  <script src="~/PlugIn/angular-ui-router/release/angular-ui-router.js"></script>  

Link your Angular configurable file.

  1. <script src="~/App/App.module.js"></script>  
  2.         <script src="~/App/App.config.js"></script>  
  3.         <script src="~/App/LocaleController.js"></script> 

Angular Module

You will need to include the module as a dependency on your application.

  1. var Locale = angular.module(Locale, ['ui.router''tmh.dynamicLocale']);  

If you have any doubt in configuration, visit the following articles -

Work in Angular i18n & Dynamic Locale

“Locale” is my Angular module name. so, I have added the “ng-app”.

  1. <div class="container body-content" ng-app=" Locale">  
  2.         @RenderBody()  
  3. </div>   

Html Code

  1. <div ng-controller="LocalizationController" class="container container-md">  
  2.         <div class="row">  
  3.             <div class="col-md-5">  
  4.                 Select a locale  
  5.                 <select ng-model="model.selectedLocale"  
  6.                         ng-options="key as value for (key, value) in availableLocales"  
  7.                         ng-change="changeLocale(model.selectedLocale)" class="form-control"></select>  
  8.             </div>  
  9.   
  10.             <div class="col-md-7">  
  11.                   
  12.                 <table class="table table-responsive table-bordered">  
  13.                     <tr>  
  14.                         <td>  
  15.                             Selected locale id:  
  16.                         </td>  
  17.                         <td>  
  18.                             {{model.selectedLocale}}  
  19.                         </td>  
  20.                     </tr><tr>  
  21.     <td>  
  22.         Current locale id:  
  23.     </td>  
  24.     <td>  
  25.         {{$locale.id}}  
  26.     </td>  
  27. </tr>  
  28.                     <tr>  
  29.                         <td>  
  30.                             A big number:  
  31.                         </td>  
  32.                         <td>  
  33.                             {{1234567890 | number}}  
  34.                         </td>  
  35.                     </tr>  
  36.                     <tr>  
  37.                         <td>  
  38.                             The Epoch was on:  
  39.                         </td>  
  40.                         <td>  
  41.                             {{0 | date}}  
  42.                         </td>  
  43.                     </tr>  
  44.                     <tr>  
  45.                         <td>  
  46.                             One million:  
  47.                         </td>  
  48.                         <td>  
  49.                             {{1000000 | currency}}  
  50.                         </td>  
  51.   
  52.                     </tr>  
  53.   
  54.                 </table>  
  55.             </div>  
  56.         </div>  
  57.         </div>  

Angular Config

This module expects for the Angular locales to present at  'angular-i18n/angular-locale_{{locale}}.js'. If the locales are at another URL, this can be changed at tmhDynamicLocaleProvider using localeLocationPattern (string).

  1.         .module('Locale')  
  2.         .config(localeConfig);  
  3. function localeConfig(tmhDynamicLocaleProvider) {  
  4.         tmhDynamicLocaleProvider.localeLocationPattern('Plugin/angular-i18n/angular-locale_{{locale}}.js');  
  5.     }  

Angular Controller

Angular initiates the locale in dropdown and set to the locale id as “en” Angular.

  1.     .module('Locale')  
  2.     .controller('LocalizationController', LocalizationController);  
  3. function LocalizationController($rootScope, tmhDynamicLocale, $locale) {  
  4.   $rootScope.availableLocales = {  
  5.     'en''English',  
  6.     'es''Spanish',  
  7.     'de''German',  
  8.     'fr''French',  
  9.     'ar''Arabic',  
  10.     'ja''Japanese',  
  11.     'ko''Korean',  
  12.     'zh''Chinese'};  
  13.     
  14.   $rootScope.model = {selectedLocale: 'en'};   
  15.   $rootScope.$locale = $locale;   
  16.   $rootScope.changeLocale = tmhDynamicLocale.set;   
  17. }  

When using, tmhDynamicLocale.set will return a promise that will be resolved when the locale is loaded and will resolve to the new locale

Click F5 button and Run the application. Now, it will open the browser where you can see the result.

Output 1

Angular

I have loaded default locale as English.

Output 2

Angular

I have changed locale as Chinese, so the data is changed to Chinese.

Conclusion

In this article, we have seen dynamic localization using Angular i18n. If you have any queries, please tell me through the comments section. 

Happy Coding!

Up Next
    Ebook Download
    View all
    Learn
    View all