Showing posts with label language. Show all posts
Showing posts with label language. Show all posts

Wednesday, October 23, 2019

How to Add Different Languages in Asp.net

Adding Different Languages in Asp.net

In this article , We will Learn about How languages are in websites in Asp.net


# Language Code for Different Languages

  1. English ------- en-us (us)
  2. French ------- fr-be (Belgium)
  3. Germen ------- de-at (Austria)

# How to find other languages with its code

Goto Internet Explorer > Tools > Internet Option > Languages > Add

# We have to make txt file for languages

*Number of languages = number of txt file

Base name=abc   culture name=en    region name=us
abc.en-us.txt
abc.fr-be.txt
abc.de-at.txt

# After making txt files, convert them to resource file

abc.en-us.resources
abc.fr-be.resources
abc.de-at.resources

# Resources file = compressed  (common bitmap, icon, images, common string)

# Open Visual Studio and Make new Project

*Website >  Add new Item > Text file > abc.en-us.txt
a= Welcome
b= Hello
c= English
d= Hi
key =a          value = Welcome

Key is used to access and it should be in English.

*Website >  Add new Item > Text file > abc.fr-be.txt
a= Bienvenu
b= Bonjour
c= French
d= salut

*Website >  Add new Item > Text file > abc.de-at.txt
a= Herzlich willkommen
b= Hallo
c= Germen
d= Hallo

*Open Developer command prompt for VS 2017 and run as admin


Resgen – utility to convert txt to resource file
*Add new webform > then drop downlist
DropdownList> Tasklist> Edit Item> add > Text= English > Value = en-us
add > Text= French > Value = fr-be
add > Text= Germen> Value = de-at
*Enable autopostback
*Add 4 Label


#Code File

using System.Globalization;
using System.Resources;
using System.Threading;
Globalization – culture specific classes
Resources – to read/write information from resource file
Threading – multi-threading concept

#Global Application Class

-         one project one file
-         to manage application and session level events
#Website > Add new Item > Global Application Class
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Globalization"%>
<%@ Import Namespace="System.Resources"%>
<%@ Import Namespace="System.Threading" %>

void Application_Start(object sender, EventArgs e)
-         fire at first and one time only when project runs
void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["rm"] = ResourceManager.CreateFileBasedResourceManager("abc", Server.MapPath("."), null);
    }
Rm= Global Variable (can be used in whole project)
. = current path
Null = current resource setting

# Make new method to fire the event everytime when request is send to server

void Application_BeginRequest(object sender, EventArgs e)
    {
        try
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo(Request.UserLanguages[0].ToString());
        }

        catch
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
        }
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

    }

Application_BeginRequest- Number of times is request send to server , the event should be fired same number of time
try, catch – Structure error handling (error handling)
on error goto – Unstructured error handling
try’s error will come in catch
Thread.CurrentThread- Current instance of the browser
CurrentCulture- to change the format not the culture
CultureInfo- it is a class of System.Globalization
CurrentUICulture- just to change the culture not the format
Format- it is date , time format and others etc

# Code File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Resources;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
    ResourceManager rm;
    protected void Page_Load(object sender, EventArgs e)
    {
        rm = (ResourceManager)(Application["rm"]);
        if (Page.IsPostBack==false)
        {
            Label1.Text = rm.GetString("a").ToString();
            Label2.Text = rm.GetString("b").ToString();
            Label3.Text = rm.GetString("c").ToString();
            Label4.Text = rm.GetString("d").ToString();
        }
    }

    private void Set_Cul(String d)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(d);
        Label1.Text = rm.GetString("a").ToString();
        Label2.Text = rm.GetString("b").ToString();
        Label3.Text = rm.GetString("c").ToString();
        Label4.Text = rm.GetString("d").ToString();
    }
}

String d- to pass culture code

# Design

*Double click DropDownList
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Set_Cul(DropDownList1.SelectedValue);
    }