现在的位置: 首页 > 综合 > 正文

Telerik RadGridView汉化

2018年02月12日 ⁄ 综合 ⁄ 共 10551字 ⁄ 字号 评论关闭
Telerik UI for Silverlight
Localization
See Also Send
Feedback

When you limit your product's availability to only one language, you limit your potential customer base to a fraction of the world population. If you want your application to reach a global audience, cost-effective
localization of your product is one of the best and most economical ways to reach more customers.

Tip
Localization is the translation of application resources into localized versions for the specific cultures that the application supports.

The built-in localization mechanism in Silverlight allows you to localize any string resource used by a standard
RadControl. Once translated you might use your resources in your Silverlight project without changing anything.

Common_Localization_070

The purpose of this tutorial is to show you how to localize any resource string used by a
RadControl.

Tip
All examples in this tutorial are demonstrated in the context of the Telerik
RadGridView
control. However, the techniques and principles used for the localization of the string resources are valid for all the other Telerik
Silverlight       controls.

This topic contains the following sections:

Collapse imageWhat is LocalizationManager?

The Telerik.Windows.Controls.LocalizationManager allows you to easily localize any of the Telerik controls.

To apply
custom localization
to your controls just instantiate your custom LocalizationManager and set it to the static property
LocalizationManager.Manager, before the creation of the UI.

CopyC#
LocalizationManager.Manager = new CustomLocalizationManager();

CopyVB.NET
LocalizationManager.Manager = New CustomLocalizationManager()
Note

Note that you have to set the localization manager before the creation of the UI, otherwise some parts might remain not-localized.

Collapse imageResource Keys

Some of the controls are complex user interface controls (e.g. RadGridView,
RadScheduleView) and their strings for localization are numerous. In order to be able to distinguish these resources, an unique identifier called
resource key, is assigned to each localizable string.

On the picture below you can see some resource keys and the strings they are associated with.

Common_Localization_060
Tip
For a full list of Resource Keys, check out the Localization topic for the specific control.

Collapse imageLocalization Using Built-in Resources

          The built-in localization mechanism in  Silverlight provides the possibility to set the used Telerik Silverlight controls in one of the following supported languages:

  •               English            
  •               German              
  •                 Spanish              
  •                 French              
  •                 Italian              
  •                 Dutch              
  •                 Turkish              

          They are located in separate files (each for every language). You can find them in the corresponding folders together with the other binaries in your local installation.        

Note

            Not all the controls support each of those languages.          

          Note that, the resource folders are supposed to be placed along with the binaries you have referenced.        

Common Localization 050

Firstly, you must defined your prefered language in the <Supported Cultures> tag, like so:

CopyXAML
<SupportedCultures>en;nl</SupportedCultures>

The next step for defining the language settings of the application is changing the
Current Culture of the application.            

CopyC#
private void Application_Startup(object sender, StartupEventArgs e)
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("es");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");

            this.RootVisual = new MainPage();
        }

CopyVB.NET
Private Sub Application_Startup(sender As Object, e As StartupEventArgs)
    Thread.CurrentThread.CurrentCulture = New CultureInfo("es")
    Thread.CurrentThread.CurrentUICulture = New CultureInfo("es")

    Me.RootVisual = New MainPage()
End Sub

            You could check            
this help article
            , where you can find detailed explanation about how to achieve this.          

          If you want to translate your RadControl in another language, you should use the          

Custom Localization Manager.

Collapse imageLocalization Using ResourceManager

          You can base your localization on the standard resource files provided by the .NET framework.           For that purpose you will have to create a separate
.ResX file           for each one of the languages that your application will support.        

Imagine that you want to translate your control         (for example:
RadGridView
control)         into English, German and Dutch.         For that purpose you will have to add three new resource files to your project:        

  • GridViewResources.resx - this resource file will store the
    English(default)
                 resources for the grid control. Set the
    AccessModifier
    property to Public.          
  • GridViewResources.de.resx - this resource file will store the
    German             resources for the grid control. Set the
    AccessModifier
    property to No code generation.          
  • GridViewResources.nl.resx - this resource file will store the
    Dutch             resources for the grid control. Set the
    AccessModifier
    property to No code generation.          
Common Localization 030

Now, having the needed files, it's time to illustrate the idea and localize only the text for the group panel.         For that purpose you need to create a single resource string in each one of the three resource files and translate it to the appropriate
language.      

Note

            Note that the name of the resource string should be the same as the resource key for the string that you are localizing. The resource key for the group panel is
GridViewGroupPanelText.          

Tip
          For a full list of Resource Keys, check out the
Localization
topic for the specific control.        

          The snapshot below shows the content of the GridViewResources.de.resx file. The resource name of the other two files should be the same. The
Value column will contain the translation for the appropriate language.        

Common Localization 040

          The last step is to instantiate the LocalizationManager class and set its
ResourceManager to the resources that have been just created (you can do this in the default constructor of the Application class)        

CopyC#
LocalizationManager.Manager = new LocalizationManager()
{
   ResourceManager = GridViewResources.ResourceManager
};

CopyVB.NET
LocalizationManager.Manager = New LocalizationManager()
LocalizationManager.Manager.ResourceManager = GridViewResources.ResourceManager
Note

                If you rely on culture settings to load the right resources automatically, you have to write some code inside your application's project file.                For example, if you have to support English and Dutch languages, you can store the
localized strings in                 Resources.resx and
Resources.nl.resx
files. For the Resources.resx                file you can set
ResXFileCodeGenerator to Internal or Public
                and for others - to No code generation. Then, open the project file in a text-mode and insert the code below into the                
<PropertyGroup> section. In this way you notify the framework about the supported cultures.              

CopyXAML
<SupportedCultures>en;nl</SupportedCultures>

Collapse imageLocalization Using Custom Localization Manager

            The other way to localize your RadControl is to create a class that derives from the
LocalizationManager          object and to override its method
GetStringOverride()
.           The logic is pretty simple, you just have to create a switch statement and return the correct translation for each resource key,          as it is shown below:        

CopyC#
public class CustomLocalizationManager : LocalizationManager
{
  public override string GetStringOverride( string key )
  {
      switch( key )
      {
          case "GridViewAlwaysVisibleNewRow":
                    return "点击这里添加新项";
                case "GridViewClearFilter":
                    return "清除筛选";
                case "GridViewFilter":
                    return "筛选";
                case "GridViewFilterAnd":
                    return "并且";
                case "GridViewFilterContains":
                    return "包含";
                case "GridViewFilterDoesNotContain":
                    return "不包含于";
                case "GridViewFilterEndsWith":
                    return "结尾等于";
                case "GridViewFilterIsContainedIn":
                    return "包含在";
                case "GridViewFilterIsEqualTo":
                    return "等于";
                case "GridViewFilterIsGreaterThan":
                    return "大于";
                case "GridViewFilterIsGreaterThanOrEqualTo":
                    return "大于等于";
                case "GridViewFilterIsLessThan":
                    return "小于";
                case "GridViewFilterIsLessThanOrEqualTo":
                    return "小于等于";
                case "GridViewFilterIsNotContainedIn":
                    return "不包含在";
                case "GridViewFilterIsNotEqualTo":
                    return "不等于";
                case "GridViewFilterMatchCase":
                    return "区分大小写";
                case "GridViewFilterOr":
                    return "或";
                case "GridViewFilterSelectAll":
                    return "全选";
                case "GridViewFilterShowRowsWithValueThat":
                    return "显示在行中的值";
                case "GridViewFilterStartsWith":
                    return "开头等于";
                case "GridViewGroupPanelText":
                    return "拖拽一列放在这里进行分组";
                case "GridViewGroupPanelTopText":
                    return "组";
                case "GridViewGroupPanelTopTextGrouped":
                    return "分组于";
      }
      return base.GetStringOverride( key );
  }

CopyVB.NET
Public Class CustomLocalizationManager
 Inherits LocalizationManager
 Public Overrides Function GetStringOverride(key As String) As String
  Select Case key
   Case "GridViewGroupPanelText"
    Return "Zum gruppieren ziehen Sie den Spaltenkopf in diesen Bereich."
   '---------------------- RadGridView Filter Dropdown items texts:
   Case "GridViewClearFilter"
    Return "Filter löschen"
   Case "GridViewFilterShowRowsWithValueThat"
    Return "Anzeigen der Werte mit Bedingung:"
   Case "GridViewFilterSelectAll"
    Return "Alles anzeigen"
   Case "GridViewFilterContains"
    Return "Enthält"
   Case "GridViewFilterEndsWith"
    Return "Endet mit"
   Case "GridViewFilterIsContainedIn"
    Return "Enthalten in"
   Case "GridViewFilterIsEqualTo"
    Return "Gleich"
   Case "GridViewFilterIsGreaterThan"
    Return "Grösser als "
   Case "GridViewFilterIsGreaterThanOrEqualTo"
    Return "Grösser oder gleich"
   Case "GridViewFilterIsLessThan"
    Return "Kleiner als"
   Case "GridViewFilterIsLessThanOrEqualTo"
    Return "Kleiner oder gleich"
   Case "GridViewFilterIsNotEqualTo"
    Return "Ungleich"
   Case "GridViewFilterStartsWith"
    Return "Beginnt mit"
   Case "GridViewFilterAnd"
    Return "Und"
   Case "GridViewFilter"
    Return "Filter"
  End Select
  Return MyBase.GetStringOverride(key)
 End Function
End Class

          Of course, if you don't want to hard-code your translation inside your source code, you can always use resource files:        

CopyC#
public override string GetStringOverride( string key )
{
   switch( key )
   {
       //----------------------
       case "GridViewClearFilter":
           return GridViewResources.GridViewClearFilter;
       //----------------------
   }
   return base.GetStringOverride( key );
}

CopyVB.NET
Public Overloads Overrides Function GetStringOverride(ByVal key As String) As String
    Select Case key
        '----------------------
        Case "GridViewClearFilter"
            Return GridViewResources.GridViewClearFilter
        '----------------------
    End Select
    Return MyBase.GetStringOverride(key)
End Function

Collapse imageSee Also

抱歉!评论已关闭.