ASP.NET - Displaying web page in different language based on user location...  ASP.NET - Displaying web page in different language based on user location...

Asked By selva kumar
28-Apr-11 10:22 PM
 hi all...

My web page available in English, Japanese,French , 
and English is default in general..
How can i make my site display  in Japanese as default if page is open from Japan..

Thanks in advance.

Regards,  
Selva.
  James H replied to selva kumar
28-Apr-11 11:28 PM

Hi ,

     please try to  use global resource files to create a web application that can cope with multiple languages as well as localising a display to a user’s location.

Creating Resource Files

Create a new website using Visual Studio or Visual Web Developer, then create a new ASP.Net folder by right clicking on the project in Solution Explorer, followed by Add ASP.Net Folder, then App_GlobalResources. Next, right click on this new folder and Add New Item. Select Resource and call it 'GlobalLanguages.resx'. This will create the default language file and Visual Studio will then display the resource designer window where we can enter our data into. GlobalLanguages can be any name you wish, just substitute in your name for GlobalLanguages in the code below.

On the left column (name) you enter an identifier name for the string, for example "WelcomeMessage" and in the right hand column (value) enter the actual message, for example "Hello and Welcome to our Website". You can optionally add information into the comments column.

Save and close this file, then from Solution Explorer duplicate this file, giving it a name of GlobalLanguages.it.resx (it being the two letter country code for Italy/Italian). Replace "Hello and Welcome to our Website" with the Italian "Ciao e benvenuto al nostro Web site". Save and close this file.

Resources are strongly typed, meaning that you can access them with intellisence using the methods shown below. After you make changes to the resource files you will need to build the project for intellisense to pick up on the extra or modified values.

Displaying Resource Messages

Now all we need to do is output these messages on the screen. There are a few methods for this, so pick the best one for your needs.

Raw HTML in the ASPX

<a href="#"><%=Resources.GlobalLanguages.WelcomeMessage %></a>

Setting the Value of a Label in C# Code behind (preferred)

Label1.Text = Resources.GlobalLanguages.WelcomeMessage;

Reading the Value into a String object

string WelcomeMessage = Resources.GlobalLanguages.WelcomeMessage;

When you run the application you will see the English message written out on the screen.

Changing Resource Languages

You can change the resource file language that is displayed by changing the current resource provider culture. The constructor of the CultureInfo class takes the five character language code to use from the Messages.xx.resx filename. Shown below is the code to change to Italian.

Resources.GlobalLanguages.Culture = new CultureInfo("it");

For the purposes of this tutorial I have hard coded the language, but it can be loaded from a configuration file, query string, session variable or profile information or any other method you desire. If you specify a language to the CultureInfo that does not have a Messages.xx.resx file then the default Messages.resx will be used.

You can also use string placeholders in the resource file, so you could have a string "Welcome back {0}, you have {1} new messages" and use a string.format to enter usernames and numbers. You can see an example of this in action in the sample download below.

The example uses three languages – English, French and Italian with buttons to change the language. I am also outputting the date so I have changed the System.Threading.Thread.CurrentThread.CurrentCulture to match the language selected so that when the day is outputted the system automatically outputs the correct day for the language.

please refer the following link for more information

http://www.codeproject.com/Kb/aspnet/localizationByVivekTakur.aspx

http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx

http://http://www.c-sharpcorner.com/uploadfile/ankithakur/globalization_localization_in_dotnet_csharp07032006023510am/globalization_localization_in_dotnet_csharp.aspx

http://www.dotnetsizzler.com/post/2011/01/03/Globalization-and-Localization-with-ASPNET-AJAX.aspx

http://msdn.microsoft.com/en-us/library/c6zyy3s9%28v=vs.80%29.aspx

Hope this will help you

  Vickey F replied to selva kumar
28-Apr-11 11:36 PM

You can do this by implementing concept of localization and globalization.

Globalization is process of making an application automatically handle users from different countries and locales.

Localization is process of providing “localized resources” to make an application usable for different countries and locales.

In Localization Application author has to provide the resources like text in different languages for labels, buttons and other controls.

Example: In order to use your web page or site by international users it must be translated to multiple languages, FW would not transform the text automatically; you need to provide the translations.


Globalization in ASP.NET AJAX

In ASP.NET AJAX, script manager control is going to support Globalization by setting the “EnableScriptGlobalization = true” it automatically formats the dates and currencies based on culture.

You can set culture in following ways

· Set Programmatically

· Set at Page level

· Set in the Web.config file

If it is not set anywhere above then it can be determined from client side ACCEPT-LANGUAGE header which comes from the client browser.


Localization in ASP.NET AJAX

Localization is also relies on Script Manager control and uses the Culture UI setting and you need to set the “EnableScriptLocalization = true”

Use scripts element to identify the localized scripts by using the ResourceCultures attribute.

Script Manager will insert locale info into filename:

  1. 1. Open Visual Studio 2010 and create a new ASP.NET

 

  1. http://dotnetsizzler.com/image.axd?picture=image_10.png

 

2.  Drag the Script Manager control to default.aspx page and set the EnableScriptGlobalization attribute to “true”. By default the value is false

<asp:ScriptManager ID="ScriptManager1" 
      EnableScriptGlobalization="true" 
     runat="server">
</asp:ScriptManager>

 

3. Now add a hyperlink and write a JavaScript function to update the UI in default web page

<a id="updateLink" href="javascript:updateUI()">
UpdateUI</a>

 

 

<script type="text/javascript">
    function updateUI() {
    cultureDiv.innerHTML = Sys.CultureInfo.CurrentCulture.name;
    var cur = 15.65;
    currencyDiv.innerHTML = String.localeFormat("{0:c}", c);
    var d = new Date();
     TimeDiv.innerHTML = d.localeFormat("dddd dd MMM yyyy HH:mm:ss");
        }
</script>
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

4. Add some HTML content to page as shown below

<p/>
    Current Culture(client):
    <div id="cultureDiv" style="font-weight:bold"></div>
    <p/>
        Currency:
    <div id="currencyDiv" style="font-weight:bold"></div>
        <p/>
        Time:
    <div id="TimeDiv" style="font-weight:bold"></div>

 

5. Now run the application and click the updateUI link to see the following window. It shows the current culture, currency and time.

http://dotnetsizzler.com/image.axd?picture=image_11.png

6. In order to get the appropriate culture and currency that browser using then you need to set the following attribute in web.config file so that the values change based on the culture setting in the browser.

<globalization culture ="auto"/>

 

7. If you want to control the culture from server side, just mention the desired culture in web.config file

<globalization culture ="es-ES"/>

http://dotnetsizzler.com/image.axd?picture=image_12.png

8.  You can override the culture setting at page level by changing the culture attribute in page directive.

 <Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true"    CodeBehind="Default.aspx.cs" 
Inherits="GlobalizationSample._Default" 
    Culture="it-IT" %>

 

9. We can override these values by writing the following code in code behind

protected override void InitializeCulture()
 {
      this.Culture = "it-IT";
  }

 

Localization Example

1. Now add a javascript file named Localization.js to your scripts folder and write the following code in the script file

   1:  Sys.Application.add_load(SetLocalizedInfo);
    2:  function SetLocalizedInfo() {
  3:  $get("updateLink").innerHTML = Messages.UpdateLinkText;
   4:  }
  5:  Messages = {
   6:  "UpdateLinkText": "Update Eng UI"
 7:  };

The above script sets the link text to “Update Eng UI” while you call this in script manager

2. Similarly add the Localization.it-IT.js and Localization.sv-SE.js script files in solution as shown in below

http://dotnetsizzler.com/image.axd?picture=image_13.png

The script files will be download to the client based on UI culture that you set in your application

Refer the above script in page as follows

<asp:ScriptManager ID="ScriptManager1" EnableScriptGlobalization="true" 
  EnableScriptLocalization ="true" runat="server">
 <Scripts>
  <asp:ScriptReference  Path="~/Scripts/Localization.js"
   ResourceUICultures ="it-IT;sv-SE" /> 
  </Scripts>

 

3. Write the following code in code behind

protected void Page_Load(object sender, EventArgs e)
  {
    //seeting the current culture to UI culture
     Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
   }
 protected override void InitializeCulture()
  {
   this.Culture = "it-IT";
   }

Respective culture that you set will be loaded from scripts folder and ui elements will be changed accordingly.

for more detail follow this link-
http://www.dotnetsizzler.com/post/2011/01/03/Globalization-and-Localization-with-ASPNET-AJAX.aspx

  Riley K replied to selva kumar
29-Apr-11 12:07 AM
Create a root site and have that will take you to a page where u can choose country and language and create directory like below


site.com/ca/en/ (Canada English)
site.com/ca/fr/ (Canada French)
site.com/us/en/ (US English)
site.com/us/es/ (US Spanish)
or use the concept of Localization and Globalization

Refer these below links

http://www.eggheadcafe.com/community/aspnet/2/10066499/multilingual-web-application.aspx

http://

Hope this helps
  Ravi S replied to selva kumar
29-Apr-11 01:13 AM
Hi selva

use this java script....

Here fr and es are the language codes

var langcodes=["fr", "es"];

// Browser Language Redirect script
// copyright 3rd January 2006, Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (with the sole exception
// of the langcodes array entries) is used without any alteration

var langCode = navigator.language || navigator.systemLanguage;
var lang = langCode.toLowerCase();
 lang = lang.substr(0,2);
 var dest = window.location.href;
for (i=langcodes.length-1;i >= 0; i--)
{
if (lang==langcodes[i])
{
dest = dest.substr(0,dest.lastIndexOf('.')) + '-' + lang.substr(0,2) + dest.substr(dest.lastIndexOf('.'));
window.location.replace ?window.location.replace(dest) :window.location=dest;
}
}


save it as langdir.js.

Finally we link the script into the head of each of our pages where translated versions are available by adding the following code:

<script type="text/javascript" src="langdir.js">
</script>

          

refer the link...you get more information on this
http://javascript.about.com/library/bllang.htm

here is the link for language codes
http://javascript.about.com/gi/dynamic/offsite.htm?site=http://www.w3.org/WAI/ER/IG/ert/iso639.htm

hope it helps you...

hope it helps you...
  dipa ahuja replied to selva kumar
29-Apr-11 02:16 AM
One Way you can use the google Translator this is the code:


Untitled document
<div>
     <div id="google_translate_element">
     </div>
     <script>
       function googleTranslateElementInit() {
         new google.translate.TranslateElement({
           pageLanguage: 'en'
         }, 'google_translate_element');
       }
     </script>
     <script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
   </div>
   Hello World!
   
Create New Account
help
visual studio installation problem Actually, my OS is Windows Xp with service pack2.I added service pack3 to install visual studio2010.after that i tryed to installed, but am getting SETUP FAILED due to "Windows XP is not installed. [08 / 10 / 11, 14:26:00] VS70pgui: [2] DepCheck indicates Microsoft Visual F# 2.0 Runtime was not attempted to be installed. [08 / 10 / 11, 14:26:00] VS70pgui: [2] DepCheck indicates Microsoft Visual Studio Macro Tools was not attempted to be installed. [08 / 10 / 11, 14:26:00] VS70pgui attempted to be installed. [08 / 10 / 11, 14:26:01] VS70pgui: [2] DepCheck indicates Microsoft Visual Studio 2010 Professional - ENU was not attempted to be installed. [08 / 10 / 11, 14:26:01
how to debug visual webpart for sharepoint 2010 in visual studio 2010 i am unable to debug visual webpart for sharepoint 2010 in visual studio 2010. after attaching w3wp it is not debug mode. HI Designing the Visual Web
Get Started with SQLite and Visual Studio How to get started using SQLite with Designer Support in Visual Studio I've written here a number of times about SQLite and its features. The latest version of the ADO.NET SQLite Provider -by Robert Simpson - features complete Visual Studio integration with all versions of Visual Studio- 2005, 2008, and 2010. This means you can create a new SQLite database file from
at the steps needed to manually add new stored procedures to the database through the Visual Studio environment. Let s get started! Note: In the Wrapping Database Modifications within a Transaction tutorial within the scope of a transaction. Step 1: Adding Stored Procedures to the Northwind Database Visual Studio makes it easy to add new stored procedures to a database. Let s add a Figure 1 shows, selecting the Add New Stored Procedure option opens a script window in Visual Studio with the outline of the SQL script needed to create the stored procedure. It is clicking the Save icon will update the stored procedure with these changes. Figure 2 shows Visual Studio after the Products_SelectByCategoryID stored procedure has been saved. The Stored Procedure Products_SelectByCategoryID Has Been Added
want to write custom code for a button in share point designer for a contenet page not for a master page i.ee basing on the users(admin, reader, writer) i want make changes to the for diff users First approach: Most of the changes you’ll be making in content pages are going to be cosmetic, and will likely revolve around adding some additional content here and there and changing the layout of elements inside the page. Understand, however, that all of these SharePoint pages are built using ASP.NET technology. As such, these pages can be altered any way that you could normally alter an ASP.NET page, allowing you to do some really heavy renovations if you so choose. For example, take the login.aspx page from sharepoint templates. Here’s the Page directive found in login.aspx: <% @ Page Language = "C