Web Development by liaoqinmei

VIEWS: 5 PAGES: 23

									ASP.NET Web Development 2

     10. Globalization and deployment
I18n, L10n, G11n
   Internationalization (I18n)
       Making your application able to support a range of
        languages and locales
   Localization (L10n)
       Making your application support a specific
        language/locale
   Globalization (G11n)
       The combination of Internationalization and
        Localization

                 ASP.NET Web Development 2      10. Globalization and deployment
                                                                               2
Languages and locales
   Language
       For example, English
       ISO code "en"
   Locale
       For example, GB
       en-GB, en-US




                ASP.NET Web Development 2   10. Globalization and deployment
                                                                           3
Cultures in .NET
   Property of a thread
   Thread.CurrentThread.CurrentCulture
       Used in formatting dates, currency, etc
   Thread.CurrentThread.CurrentUICulture
       Used to select resources in resource files
       ResourceManager class
   Both are instances of CultureInfo class
   See .NET Web Dev 2

                 ASP.NET Web Development 2        10. Globalization and deployment
                                                                                 4
 Getting user preference from
 browser
    HTTP request header Accept-Langauge
        List of preferred languages in order
        Configured in browser options
    Can get in server code as
     Request.UserLanguages
        Use to set current culture
                  Thread.CurrentThread.CurrentUICulture =
                          new CultureInfo(this.Request.UserLanguages[0]);
        Alternatively use setting in Web.Config (or Page
         directive) to set cultures automatically
<globalization enableClientBasedCulture="true" uiCulture="auto" culture="auto" />
                      ASP.NET Web Development 2                   10. Globalization and deployment
                                                                                                 5
Getting user preference from
browser
   Alternatively, use your own HTTP cookie
       Can read or write from client or server code
       Can allow user to select language from page rather
        than by configuring browser
   Writing and reading cookies with jQuery
       Need cookies plug-in
          https://github.com/carhartl/jquery-cookie
        // write
        $.cookie('language', lang);

        // read
        var culture = $.cookie('language');
                      ASP.NET Web Development 2       10. Globalization and deployment
                                                                                     6
  Getting user preference from
  browser
     Alternatively, use a value in the URL
     Extract from RouteData
      routes.MapRoute(
         "Localization", // Route name
         "{lang}/{controller}/{action}/{id}", // URL with parameters
         new { controller = "Home", action = "Index",
            id = UrlParameter.Optional } // Parameter defaults
      );


var lang = filterContext.RouteData.Values["lang"].ToString();
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(lang);



                        ASP.NET Web Development 2                      10. Globalization and deployment
                                                                                                      7
Localizing a web form
   Tools menu > Generate Local Resources
   Creates resx file which contains keys taken
    from all localizable controls in web form
       e.g. Default.aspx.resx
       Gives each control a meta:resourcekey attribute
   Need to manually create resx file for each
    culture
       e.g. Default.aspx.de.resx
   ResourceManager will locate resource file by
    name based on current UI culture
                 ASP.NET Web Development 2     10. Globalization and deployment
                                                                              8
Localizing MVC - resources
   Can create resource files and access these in
    view



       Resources.resx



                  Resources.es.resx




                ASP.NET Web Development 2   10. Globalization and deployment
                                                                           9
Localising MVC - validation
   Attributes can take their values from resource
    files
   Localizes validation messages displayed in
    view




              ASP.NET Web Development 2   10. Globalization and deployment
                                                                        10
Localizing MVC - views
   Instead of resource files, create separate
    views for each culture
   Allows localization to be more
     than just text
   Can have different layouts for
    different cultures
   More difficult to maintain than
    using resources


              ASP.NET Web Development 2   10. Globalization and deployment
                                                                        11
Locating localized views
   Role of IViewEngine is to find view files and
    create IView instances
       CreateView and CreatePartialView methods
   Can derive a custom view engine class from
    RazorViewEngine
   Modifies search path according to culture
    before searching for view
   Gets culture from any of the sources listed
    previously
                ASP.NET Web Development 2   10. Globalization and deployment
                                                                          12
Localizing client code
   Use jQuery Globalization plug-in
         https://github.com/jquery/globalize
   Originally contributed by Microsoft
   If cookie used to identify preferrer culture it
    can be read by both client and server code
     var culture = $.cookie('language');
     var today = Globalize.format(new Date(), "D", culture);
     $('#displaydate').html(today);




                    ASP.NET Web Development 2                  10. Globalization and deployment
                                                                                             13
Examples
   http://www.hanselman.com/blog/GlobalizationInternation
    alizationAndLocalizationInASPNETMVC3JavaScriptAnd
    JQueryPart1.aspx

   http://afana.me/post/aspnet-mvc-
    internationalization.aspx

   http://geekswithblogs.net/shaunxu/archive/2010/05/06/lo
    calization-in-asp.net-mvc-ndash-3-days-investigation-1-
    day.aspx


                ASP.NET Web Development 2        10. Globalization and deployment
                                                                               14
Preparing for deployment
   Compile views at build time rather than
    compiling dynamically
   Edit csproj file
    <PropertyGroup>
     …
     <MvcBuildViews>true</MvcBuildViews>
     …
    </PropertyGroup>




                   ASP.NET Web Development 2   10. Globalization and deployment
                                                                             15
Preparing for deployment
   Do not compile in debug mode, otherwise
       Some code optimizations are omitted to allow
        line-by-line debugging
       Request timeouts are disabled
       The way content, e.g. scripts, is cached by
        browser is limited
    <compilation debug=“false” targetFramework=“4.0”>

   Can disable debug=true on server using
    machine.config
    <deployment retail="true"/>

                    ASP.NET Web Development 2           10. Globalization and deployment
                                                                                      16
Bin deployment
   Deploy to any IIS server which has ASP.NET
    4 installed but not MVC 3
   Add Deployable Dependencies option in
    Solution Explorer menu
   Copies MVC 3 and other required assemblies
    to bin folder




             ASP.NET Web Development 2   10. Globalization and deployment
                                                                       17
What to deploy
   Do deploy
       Compiled assemblies
       Config files, inc global.asax
       Uncompiled views
       Static files, e.g. images, CSS, js
   Don’t deploy
       .cs files, inc global.asax.cs
       Project and solution files
       \obj folder
       Anything specific to source control
                  ASP.NET Web Development 2   10. Globalization and deployment
                                                                            18
Web.config transformations
    Specific configuration settings for debug and
     release builds
    Web.Debug.config and
    Web.Release.config contain transfoms
   Can also have transforms for custom build
    configs
       Create solution config in Configuration Manager
       Right-click on Web.config and select Add Config
        Transforms

                 ASP.NET Web Development 2    10. Globalization and deployment
                                                                            19
Transformation structure
   File contains Configuration element
   Replicate hierarchy of Web.config nodes we
    wish to transform
   Each transformation defines:
       Target element
       Type of transformation
       Any addition values required
   Transforms apply only when using
    Deployment Packages/One-click Publish
                 ASP.NET Web Development 2   10. Globalization and deployment
                                                                           20
Transformation types
   Insert
   InsertBefore
   InsertAfter
   Remove
   RemoveAll
   RemoveAttributes
   SetAttributes
   Replace

             ASP.NET Web Development 2   10. Globalization and deployment
                                                                       21
  Example
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
                                                           Transform – sets specific
                                                           attribute value for matching
 <connectionStrings>                                       elements
  <add xdt:Transform="SetAttributes(connectionString)"
    connectionString="MyConnection"
    xdt:Locator="Match(name, providerName)"
    name="SportsStoreEntities"
    providerName="System.Data.EntityClient"/>
                             Locator – allows us to be specific about which elements
 </connectionStrings>
                             we want to transform. This will apply only to elements
                             with path configuration/connectionstrings/add which
</configuration>
                             have name=“SportStoreEntities” and
                             providername=“System.Data.EntityClient”
                        ASP.NET Web Development 2                     10. Globalization and deployment
                                                                                                    22
Deployment packages
   Project > Package/Publish Settings
       Package/Publish Web and Package/Publish
        SQL tabs
   Project > Build Deployment Package
   Creates a zip file which can be imported into
    IIS using IIS manager
   Can use One-Click to deploy without creating
    file if dev machine can connect to server
       Solution menu > Publish

                ASP.NET Web Development 2   10. Globalization and deployment
                                                                          23

								
To top