To add internationalization to our demo app we can use a third-party module called angular-translate
, you can download from https://github.com/angular-translate/bower-angular-translate/releases. I will add two text labels with language translations, English-Language, Arabic-Language and Urdu-Language.
A sample output of this demo will be like this:
With Arabic-Translator:
With Urdu-Translator:
With English-Translator:
First, we need to load the script in html page, so that AngularJS can find the module. Add following script tag in html page's head tag:
<script src="assets/libs/angular-translate.min.js"></script>
Next we have to add a dependency on the module
pascalprecht.translate
defined in angular-translate
. So, we go to our main app module and add
pascalprecht.translate
module in the dependencies list. It now looks like this:
var demoApp = angular.module('demoApp', ['pascalprecht.translate']);Now, we need to add
$translateProvider
dependency in the config function of our main module demoApp
. Then we add translations for the languages we required in our application. In this demo I am using English-Language, Arabic-Language and Urdu-Language. The following code shows how it's done:
demoApp.config(['$translateProvider', function ($translateProvider) { $translateProvider.translations('ar', { TITLE: 'تدويل تجريبي', DESCRIPTION: 'يمكننا استخدام وحدة خارجية، angular-translate ، إضافة إلى تدويل التطبيق التجريبي لدينا.', }); $translateProvider.translations('ur', { TITLE: 'بین الاقوامیت سازی ڈیمو', DESCRIPTION: 'ہم ڈیمو اپلی کیشن کے لئے عالمگیریت شامل کرنے کے لئے، angular-translate ، ایک تیسری پارٹی کے ماڈیول کا استعمال کر سکتے ہیں.' }); $translateProvider.translations('en', { TITLE: 'Internationalization Demo', DESCRIPTION: 'We can use a third-party module, angular-translate, to add internationalization to our demo app.' }); $translateProvider.preferredLanguage('en'); }]);
We use translateProvider.translations
to add the translations against a key. Here we added translations for English, Arabic and Urdu languages. If you want to support more languages you can keep adding more translations in the same way.
Finally, we set the preferred language to English by calling function$translateProvider.preferredLanguage('en')
.
Next, we'll add options in html page to let user switch between available languages, so we add a toggle button to html page using bootstrap. The following markup do this work:
<div class="btn-group btn-toggle"> <button class="btn btn-xs btn-default" ng-class="{ar:'active'}[languagePreference.currentLanguage]" ng-click="languagePreference.switchLanguage('ar')"> Arabic</button> <button class="btn btn-xs btn-default" ng-class="{de:'active'}[languagePreference.currentLanguage]" ng-click="languagePreference.switchLanguage('ur')"> Urdu</button> <button class="btn btn-xs btn-default" ng-class="{en:'active'}[languagePreference.currentLanguage]" ng-click="languagePreference.switchLanguage('en')"> English </button> </div>
The next thing we need to do is add a function languagePreference.switchLanguage()
to $rootScope, which we called in the above markup for toggle button's click event. This accepts a language key and sets the specified language a variable named
languagePreference.currentLanguage
, used in the above markup in ng-class
attribute to setup view for the
selected language by applying active class. For that we need to modify the run
block in our demoApp module as follows:
demoApp.run(['$rootScope', '$translate', function ($rootScope, $translate) { $rootScope.languagePreference = { currentLanguage: 'en' }; $rootScope.languagePreference.switchLanguage = function (key) { $translate.use(key); $rootScope.languagePreference.currentLanguage = key; } }]);
The function $translate.use(key)
takes a key and loads the translations written against it, that we defined in the config function.
Now comes the final thing, to use this translator in UI. We need to replace the static hardcoded strings with a call to translate
filter.
For example, regular text will go like this:
<h3>{{'TITLE'}}</h3> <p>{{'DESCRIPTION'}}</p>After adding translate filter, the final html will become like this:
<h3>{{'TITLE' | translate}}</h3> <p>{{'DESCRIPTION' | translate}}</p>
Thats it. We have setup a simple HTML page with two strings, i.e. TITLE and DESCRIPTION, that will be displayed in three different languages. Similarly you can add more language translations in the config function if you need. Also the same way you can add more HTML text with translate filter where required.
In recent days Angular plays vital role to loading your site content in a fastest way, so it’s a required skill for everyone, thanks for sharing this useful information to our vision keep blogging.
ReplyDeleteRegards,
Angularjs training in chennai|Angularjs training chennai
Hi there! For those who need to translate their application into multiple languages, I recommend evaluating a software localization tool like POEditor, because it can save time and improve the workflow.
ReplyDeleteI was looking for this certain information for a long time. Thank you and good luck.
ReplyDeleteangularjs online training
apache spark online training
informatica mdm online training
devops online training
aws online training
This is such a good post. One of the best posts that I\'ve read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training
this is the good post blockchain training in
ReplyDeletehyderabad
Excellent blog i got more useful information from this blog..
ReplyDeletePHP Training in Chennai
PHP Training in bangalore
php training in coimbatore
php Course in madurai
devops training in bangalore
dot net training institutes in bangalore
PHP Training Institute in Chennai
php training institute in bangalore
php training institute in coimbatore
Best php training institute in chennai
I have to agree with everything in this post. Thanks for useful sharing information.
ReplyDeleteHadoop Training in Chennai
Hadoop Training in Bangalore
Big Data Course in Coimbatore
Big data course in bangalore
Big Data Course in Chennai
Big Data Training in Bangalore
DOT NET Training in Bangalore
Python Training in Coimbatore
ReplyDeleteThanks for posting this.I got lots of information from your blog.
Software Testing Training in Chennai
Software Testing Training in Bangalore
Software Testing Training in Coimbatore
Software Testing Training in Madurai
Software Testing Training Institute in Chennai
Software Testing Course in Chennai
Testing Course in Chennai
Software Testing Training Institute in Bangalore
Selenium Course in Bangalore
This is most informative and also this post most user friendly and super navigation to all posts. Thank you so much for giving this information to me. AngularJS Course in Pune
ReplyDelete
ReplyDeleteGet inspired by your blog. Keep doing like this....
Selenium Training in Chennai
Selenium Training in Bangalore
Selenium Training in Coimbatore
Best Selenium Training in Bangalore
Selenium Training Institute in Bangalore
Selenium Classes in Bangalore
selenium training in marathahalli
Selenium training in Btm
Ielts coaching in bangalore
German classes in bangalore
Thanks for sharing your innovative ideas to our vision. I have read your blog and I gathered some new information through your blog. Your blog is really very informative and unique. Keep posting like this. Awaiting for your further update.If you are looking for Angular Applications In Australia, .Net Core Applications In New Zealand, IOT Applications In Singapore and .Net Applications In UAE information please visit our website Sidhyati Technology Solutions Pvt Ltd
ReplyDeleteGood Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteI am really happy with your blog because your article is very unique and powerful for new.
Best Training Institute for AWS in Pune
Robotic Process Automation Training in Pune
AngularJS Course in Pune
this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming.
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
spoken english classes in chennai | Communication training
the info is really great
ReplyDeleteoracle training in chennai
ReplyDeleteNice article and thanks for sharing with us. Its very informative
Machine Learning Training in Hyderabad
Nice blog...Very useful information is providing by ur blog..here is a way to find.
ReplyDeleteOffshore Yii Development in India
This comment has been removed by the author.
ReplyDeletePerde Modelleri
ReplyDeleteMOBİL ONAY
mobil odeme bozdurma
Nft nasıl alinir
ankara evden eve nakliyat
trafik sigortası
dedektör
websitesi kurmak
aşk romanları
smm panel
ReplyDeletesmm panel
İŞ İLANLARI
instagram takipçi satın al
hırdavatçı burada
beyazesyateknikservisi.com.tr
servis
jeton hilesi
özel ambulans
ReplyDeleteminecraft premium
yurtdışı kargo
lisans satın al
en son çıkan perde modelleri
nft nasıl alınır
en son çıkan perde modelleri
uc satın al
Hi, Great information you will provide thanks for sharing this useful information and content.
ReplyDeleteBest IAS Coaching in Delhi
Your hard work and dedication are truly commendable. You consistently give it your all. Buy google drive storage pricing india from google drive storage pricing india
ReplyDeleteExplore exciting career opportunities with leading healthcare providers across England. We are in search of skilled and compassionate nurses to deliver outstanding patient care. A valid nursing qualification and NMC registration are necessary. Responsibilities include patient care management, collaborating with interdisciplinary teams, and detailed health assessments. Candidates should possess excellent communication skills and flexibility to work various shifts. We offer competitive remuneration, a comprehensive benefits package, and career growth opportunities. Join a team dedicated to advancing healthcare standards and improving patient outcomes across diverse settings.
ReplyDeletehttps://www.dynamichealthstaff.com/nursing-jobs-in-england