Build customizable Language Switcher Tag Helper with Bootstrap

Build customizable Language Switcher Tag Helper with Bootstrap

Mar 21, 2016     Viewed 31832 times 8 Comments
Posted in #TagHelpers  #Localization  #Bootstrap 

Previously I talked about localization in much details, today I will look to it from different angle. While the multilingual websites is common nowadays, there should be a way to let the user to switch between the languages to see the content in the language that he/she prefers.

Let's build a customizable language switcher with the help of tag helper and bootstrap.

In this article I will not dig in much details about tag helpers and bootstrap, so for those who don't know what the tag helper is, please read this link.

1. Preparing the assets

Our tag helper needs a set of icons to show the flags of the supported languages, there are some open source projects that provide a rich SVG country flags such as flag-icon-css but for simplicity I used some icons from this link. Of course we need bootstrap too, so you can get it from this link.

2. Configuring the dependencies

The main two dependencies that we need in our tag helper are  "Microsoft.AspNetCore.Mvc.TagHelpers"
"Microsoft.AspNetCore.Mvc.Localization"

3. Authoring LanguageSwitcherTagHelper

Now let's see some code

public enum DisplayMode
{
Image = 0,
ImageAndText = 1,
Text = 2
}

our tag helper contains three display modes:

  1. Image: which is display the flag of the supported languages
  2. ImageAndText: which is display the name and the flag of the supported languages
  3. Text: which is display the name of the supported languageson Insights SDK from your application. Many events such as user page views are automatically sent by the SDK.

    Set up Application Insights for ASP.NET Core Application

    To enable Application Insights to your ASP.NET Core application, simply right click your project and choose Add Application Insights Telemetry as the following:

    after that you can install the Application Insights SDK to use it locally. For more information about the set up process I encourge you to checkout this link.

    Explore your logs

    If you are using NLog, log4Net or System.Diagnostics.Trace for diagnostic tracing in your ASP.NET Core application, you can have send your logs to Azure Application Insights, where you can explore and search them. Your logs will be merged with the other telemetry coming from your application, so that you can identify the traces associated with servicing each user request, and correlate them with other events and exception reports.

    After running your application in debug mode you can see the Application Insights integration with Diagnostic Tools window

    also you can check the Application Insights dashboard which looking pretty good with the statistics information that let you drill in for further details about what is going on.

    Creating Application Insights Logger

    Microsoft already has a repository for Application Insights for ASP.NET Core, it would be nice if we have such tool integrated with ASP.NET Logging APIs, that is what I wanna explore in this section.

    First of all let create a simple class that represents the various settings for the Application Insights

    public class ApplicationInsightsSettings
    {
        public bool? DeveloperMode { get; set; }
    
        public string InstrumentationKey { get; set; }
    }

    Second we need to write the ApplicationInsightsLogger that implements ILogger interface.

    public class ApplicationInsightsLogger : ILogger
    {
        private readonly string _name;
        private readonly Func _filter;
        private readonly ApplicationInsightsSettings _settings;
        private readonly TelemetryClient _telemetryClient;
    
        public ApplicationInsightsLogger(string name)
            : this(name, null, new ApplicationInsightsSettings())
        {
        }
    
        public ApplicationInsightsLogger(string name, Func filter, ApplicationInsightsSettings settings)
        {
            _name = string.IsNullOrEmpty(name) ? nameof(ApplicationInsightsLogger) : name;
            _filter = filter;
            _settings = settings;
            _telemetryClient = new TelemetryClient();
    
            if (_settings.DeveloperMode.HasValue)
            {
                TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = _settings.DeveloperMode;
            }
    
            if(!_settings.DeveloperMode.Value)
            {
                if (string.IsNullOrWhiteSpace(_settings.InstrumentationKey))
                {
                    throw new ArgumentNullException(nameof(_settings.InstrumentationKey));
                }
    
                TelemetryConfiguration.Active.InstrumentationKey = _settings.InstrumentationKey;
                _te
        
        


    8 Comments

    login wells fargo (5/5/2018 1:51:52 AM)

    Additional fees will be paid on the city for concerts and also other events.
    Wells fargo banking login login wells fargo wells
    fargo login in for checking The board includes six appointed directors, representing the top OEMs by marketshare, and six elected directors,
    representing five aftermarket companies the other OEM.
    The Mille Lacs Band understands the type of finance and lending practices by U.

    Wells fargo identity theft protection login [url="https://loginto.us.com/"]wells fargo identity theft login[/url]
    wells fargo login During a similar period within the previous year, the company earned $0.

    payday loans online direct lender (5/11/2018 7:41:33 PM)

    That is really a 78 percent uptick from 2014, and a triple through to 2012, based on non-bank lending data from Experian. Martinez
    on Thursday rejected new requirements that lobbyists report
    expenses under $100 which can be spent on lawmakers along
    with other public officials. The people behind the payday and car-title loan industry cut a lot of campaign-contribution checks to Missouri lawmakers.
    payday loans online direct lender Yet, from the start,
    the CFPB's small-dollar loan rule was crafted pursuant to some pre-determined ideological agenda that trusted biased data,
    anecdotes, and closed-door dealings with so-called consumer groups who have long sought
    to reduce small-dollar lending. French wrote the Short-Term Loan Act will not prohibit payday advance
    businesses from lending money under the Mortgage Lending Act.
    He decided to move to the neighborhood and convinced the congregation to remain.

    paydayloansnear.me (5/12/2018 3:46:11 AM)

    For Arizona's working middle class, there is an unmet need for
    these types of loans, which are certainly not offered by traditional banks and lending institutions.

    Failure to do so on time may have a hugely damaging
    effect on your credit rating and can deprive you of peace of mind for the foreseeable future.
    While the industry says the loans provide low-income
    workers having a necessary financial service, government regulators and consumer advocates say some lenders deceptively charge exorbitantly high
    interest levels. payday loan near me (paydayloansnear.me) Mick
    Mulvaney, the head in the Consumer Financial Protection Bureau, took $31,700 in contributions from payday advance companies in 2016 which is
    now reviewing rules aimed at protecting consumers from harmful lenders.
    The quantity of hard-up Brits applying for payday cash advances to help cover
    their rent or mortgage has a lot more than doubled within two years, shocking
    new figures reveal. Trump, have said they wish to eliminate — indicated last year that it intended
    to crack down for the payday lending market.

    craigslist search all (5/13/2018 9:47:05 AM)

    If you happen to be out to produce a huge profit, you won't likely understand.
    Pay - Pal will not be integrated into Craigslist, so the purchaser must manually send
    the funds to owner. craigslist search all The online classified site Craigslist incorporates a section specifically designated for posting resumes and attracting prospective employers
    seeking professionals with specific skill sets.
    If the ads do not possess a personal email address contact information or cell phone number,
    send a communication message on the address close
    to "Reply to" at the very best left on the ad.

    payday loans online (5/13/2018 4:47:06 PM)

    Ruby added, We are relying around the moral argument that is unethical also
    it's simply wrong. How then will we solve the problem of rollover fee that
    take advantage of the indegent when they may be in dire straits.

    But payday lenders say they supply a needed service and that their satisfied clients are aware in the costs.
    payday loans online JUAN GONZÁLEZ: And one
    with the things which you mentioned if you ask me when we had arrived talking yesterday is how the bondholders also insisted for the appointment
    of your majority with the board in the new finance authority that can run the electric company being selected from people in the United
    States. With the state of hawaii refusing to do something,
    cities have stepped in. More people will probably
    be accessing most of these loans so this is definitely an important way being able to safeguard them in a proactive sense.

    gmail login email (8/8/2018 2:41:42 AM)

    The incoming emails remain handled by Gmail as before, but thanks to the magic of POP3 , I can readily read
    and respond to these messages inside Outlook without ever logging into Gmail.
    Gmail login account sign in gmail gmail sign in for email AI will be the observer, the
    understander; it is a cognitive agent developing
    a representational schema of each and every consumer. Although it really is most likely Outlook,
    the steps offer a similar experience for different email clients.
    Gmail sign in for email https://loginaid.org/ gmail login account If you're using a computer that supports at
    the very least Windows 7 or contemporary Windows platforms,
    then your personal machine already includes the hardware requirements to operate the Gmail software.

    comcast email login (8/29/2018 11:01:06 PM)

    In a regular system, you are doing your mail by using a
    standard e-mail program; you don't have to utilize
    a Web browser and don't must be on the Web. Uf email
    login network solutions email login 365 email login Meanwhile, Johnson accesses Gmail from the Windows Vista PC
    and downloads the messages to his computer, although he keeps copies of
    them on the Google servers. She holds a Bachelor of Arts in film and media production from your University at Buffalo, a Master of
    Fine Arts in visual effects from Academy of Art University as well as
    a Diploma in facebook marketing from ALISON.
    Bellsouth.net email login https://emaillogin.us.com/ icloud
    email login Iranians also have reported widespread service disruptions of txt messaging services, though mobile phones seemed to be operating normally Wednesday.

    assignment (9/19/2018 12:11:41 AM)

    Ad campaign planned for http://metropoliscomix.com/ | Deseret News Are you looking for the National Edition?


    Leave a Comment