Globalizing and Localizing .NET Applications For AutoCAD
Globalizing and Localizing .NET Applications For AutoCAD
SD5201 In the multiple cultures of the business world of today it has become more important for both
published applications and in house applications to be easily used by anyone. Over the past year one of my main
tasks has been to localize our projects for multiple cultures. Globalization is designing and developing applications for
users in multiple cultures and Localization is the process of customizing data and resources for specific cultures for
applications. In this class, we talk about and demonstrate building an AutoCAD® plugin that is localized for multiple
cultures. We will create resource files adding strings that are displayed in the application, have commands that are
culture aware and setup the user interface including dialogs that handle different cultures. Although this class will be
targeted for AutoCAD® applications most of what will be covered applies to any .NET development.
Learning Objectives
At the end of this class, you will be able to:
• Create an application that supports localized interfaces and regional data for multiple cultures.
• Use culture specific settings (in .NET, the CultureInfo class represents a particular culture or region)
• Create resource files for localization string values and other resources
• Localize an applications user interface and Windows Forms
Email: [email protected]
Globalizing and Localizing .NET Applications for AutoCAD®
Table of Contents
Globalizing and localizing .NET Applications for AutoCAD® ............................................................... 1
Learning Objectives............................................................................................................................... 1
About the Speaker ................................................................................................................................. 1
Introduction: Globalization, Internationalization and Localization ....................................................... 3
Globalization: ......................................................................................................................................... 3
Internationalization: ............................................................................................................................... 3
Localization: ........................................................................................................................................... 3
Getting Started… ................................................................................................................................... 3
Localization guidelines … ..................................................................................................................... 3
Create an application that supports localized interfaces and regional data for multiple cultures… 4
Start a new Application and Add required references….................................................................... 4
Rename Class name and add Using statements… ............................................................................ 5
Add a new Windows form to the project and rename to frmHello…................................................. 5
Add additional code to the “frmHello” form … ................................................................................... 6
Add AutoCAD® commands to the “clsHello” class… ....................................................................... 7
Localize the Application… .................................................................................................................... 8
Localize an applications user interface… ............................................................................................. 11
Edit the form and controls with the “Language” property set to the target culture. .................... 12
Use culture specific settings (.NET CultureInfo class)… ..................................................................... 13
Composed Strings… ........................................................................................................................... 14
Setup strings for Formatting… ........................................................................................................... 15
Standard Numeric Format… ............................................................................................................... 15
Converting Numbers… ........................................................................................................................ 16
Handling Plurals… ............................................................................................................................... 16
Create resource files for localization string values and resources… ................................................ 18
Add Main Resource file….................................................................................................................... 18
Set the culture of the resource file… ................................................................................................. 20
Apply localized text strings into code… ............................................................................................ 20
Creating Resource files for target Cultures… ................................................................................... 22
Setting the Culture… ........................................................................................................................... 23
Appendices… ........................................................................................................................................... 25
Application Deployment… .................................................................................................................. 25
Additional Command (using plurals code)… .................................................................................... 26
Add Control and Manage Images… ................................................................................................... 27
Use Resgen.exe to convert “.txt” file to “.resx”… ............................................................................ 29
Use AL.exe to Create Language Resources Assembly… ................................................................ 30
CultureInfo Class….............................................................................................................................. 31
Summary… ............................................................................................................................................... 32
Links… ...................................................................................................................................................... 32
2
Globalizing and Localizing .NET Applications for AutoCAD®
Internationalization:
The process of planning and implementing products, services, applications or document content
that enables easy localization for target audiences that varies in culture, region, or language.
Localization:
The process of adapting a product, service, application or document content to a particular
language, culture, and desired appearance.
Getting Started…
Existing or new applications by default may target a single culture or region, typically English,
but can be designed and written so that the application can be extended to users of multiple
cultures and regions. The requirements of a “world-ready” application should be considered at
the beginning of the project.
Before starting, you should do a review of which cultures and regions will be supported and
identify any code requiring special handling for strings, date formats, numeric values and parts
of the user interface that will need to be handled for localization.
Localization guidelines …
• Creating strings at run time from concatenated strings should be avoided. Composed
strings are difficult to localize, because they often assume a grammatical order in the
applications original language that would not apply to other localized languages.
• Use Resource (“.resx”) files for strings and images. If images contain text then it would
be best to have translated versions in a resource file. String examples are any titles,
labels, logging or error messages and any other text that a user could see.
• Strings will change length when translated for different cultures and need room to grow.
In forms, controls and message boxes the sizes need to be adjusted for the new
translation. Also, plan and adjust user interface for languages that read left to right and
right to left.
• When sorting strings the results with different languages may need to be considered.
• In addition to language translation of string values, details like currency formats, date
formats, time zones, gender roles, local holidays and local color preferences are
examples of topics that must all be considered. A successfully localized application
would appear to have been developed within the local culture and region.
3
Globalizing and Localizing .NET Applications for AutoCAD®
e
To make the
at
application work
re
as an
C
AutoCAD®
addin there are
three references
added to the
project:
AcMgd.dll
AcCoreMgd.dll
AcDbMgd.dll
4
Globalizing and Localizing .NET Applications for AutoCAD®
Added
Change the class like below:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Autodesk.AutoCAD.Runtime;
using
Autodesk.AutoCAD.ApplicationServices;
using
Autodesk.AutoCAD.DatabaseServices;
using
Autodesk.AutoCAD.Geometry;
using
Autodesk.AutoCAD.EditorInput;
[assembly:
ExtensionApplication(typeof(acadHelloWorld.clsHello))]
namespace
acadHelloWorld
n
{
io
public
class
clsHello
:
IExtensionApplication
at
{
ic
public
void
Initialize()
pl
{
//throw
new
NotImplementedException();
Ap
}
public
void
Terminate()
e
at
{
re
//throw
new
NotImplementedException();
}
C
}
}
Label: lblMessage
Buttons:……………………………..btnOK….…btnCancel..….btnAddText
5
Globalizing and Localizing .NET Applications for AutoCAD®
n
Add message text code to update for date:
io
at
public
frmHello()
ic
{
pl
InitializeComponent();
Ap
this.lblMessage.Text
=
"Hello
World
todays
date
is
"
+
DateTime.Now.ToShortDateString()
+
".";
e
}
at
re
Add button click event to add text to drawing:
C
private
void
btnAddText_Click(object
sender,
EventArgs
e)
{
Database
db
=
HostApplicationServices.WorkingDatabase;
using
(Autodesk.AutoCAD.ApplicationServices.Application
.DocumentManager.MdiActiveDocument.LockDocument())
{
using
(Transaction
trans
=
db.TransactionManager.StartTransaction())
{
try
{
MText
text
=
new
MText();
text.Contents
=
"Hello
World
!!";
BlockTable
bt
=
(BlockTable)trans
.GetObject(db.BlockTableId,
OpenMode.ForRead);
BlockTableRecord
btr
=
(BlockTableRecord)trans
.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);
btr.AppendEntity(text);
trans.AddNewlyCreatedDBObject(text,
true);
trans.Commit();
}
catch
{
Autodesk.AutoCAD.ApplicationServices.Application
.ShowAlertDialog("Error
in
Hello
World!");
}
finally
{
trans.Dispose();
}
}
}
}
6
Globalizing and Localizing .NET Applications for AutoCAD®
n
[assembly:
ExtensionApplication(typeof(acadHelloWorld.clsHello))]
io
[assembly:
CommandClass(typeof(acadHelloWorld.clsHello))]
at
namespace
acadHelloWorld
ic
{
pl
public
class
clsHello
:
IExtensionApplication
Ap
{
[CommandMethod("helloWorld")]
public
void
helloWorld()
e
{
at
frmHello
hi
=
new
frmHello();
re
Application.ShowModalDialog(hi);
C
}
public
void
Initialize()
{
//throw
new
NotImplementedException();
}
public
void
Terminate()
{
//throw
new
NotImplementedException();
}
}
}
This completes creating a project that can be built and loaded into AutoCAD® with a single
command of “HelloWorld”.
Build …
7
Globalizing and Localizing .NET Applications for AutoCAD®
[CommandMethod("helloWorld")]
CommandMethod with group name, global name, local command ID and a command flag:
The next addition is to add a resource file for each language to hold each command ID and its
value. The resource file for the commands must have the same name as the class containing
the command.
8
Globalizing and Localizing .NET Applications for AutoCAD®
In this example the ID is “helloID” and the value is “helloWorld”, which will be the local command
for the Default culture.
Add an ID and Value for each command defined with a CommandMethod in the class.
9
Globalizing and Localizing .NET Applications for AutoCAD®
In each of these resource files set the Access Modifier to “No code generation”.
Adding the local name ID for each command defined in the class to the resource files will make
that the local command for the Localized version of AutoCAD® that the application is loaded. It
is good practice to name the global name with a “_” to make your application follow the way
standard global AutoCAD® commands are defined (e.g. _Zoom, _Erase etc.).
10
Globalizing and Localizing .NET Applications for AutoCAD®
Also, plan and adjust the user interface for languages that read left to right and right to left.
11
Globalizing and Localizing .NET Applications for AutoCAD®
Edit the form and controls with the “Language” property set to the target culture.
Someone that is fluent in the language, culture and region would be best at doing the translating
of text values. Since the developer doing the application may not know the target languages
they can use online translators to get basic translation for sizing and appearance until the final
translation is available.
Example Microsoft® Bing Translator: Hello World <> Salut tout le monde
Change the “Text” property of the form to this temporary translation value. Continue editing Text
Values and resizing/relocating controls to for the target culture.
When Finished the forms appearance should represent the target culture:
Change the forms “Language” property back to “(Default)” and the form will reset to that target…
12
Globalizing and Localizing .NET Applications for AutoCAD®
The values for sizing and properties of the form and its controls are stored in the resource files.
The forms designer controls how those values are applied to the form based on the current
culture.
These settings are all handled automatically when controls are edited, so no manual editing is
required in the forms resource files.
Note: Setting the forms “Language” property and editing the form and its controls needs
to be done for each language, culture or region that the application will target.
In the class file “csHello.cs” add the following using statements at the top of the file with other
using statements:
using
System.Globalization;
using
System.Threading;
Then in the class “csHello” in the “Initialize” method set the user interface culture to a targeted
culture with the following code:
Note: The AutoCAD® sessions default culture can be acquired with the following code…
13
Globalizing and Localizing .NET Applications for AutoCAD®
Composed Strings…
Creating strings at run time from concatenated strings should be avoided.
In the sample project the form “frmHello” has a label named “lblMessage” with text in the
“(Default)” culture set to “Hello World todays date is MM/DD/YYYY.” This would be a composed
string created at runtime replacing the “MM/DD/YYYY” part of the string with the current date
and needs to be handled to display the date properly for all targeted cultures.
Override the string after the form after the form initializes:
public
frmHello()
{
InitializeComponent();
this.lblMessage.Text
=
this.lblMessage.Text.Replace("MM/DD/YYYY",
DateTime.Now.ToShortDateString());
}
Add the Using Statements to the top of the forms class file…
using
System.Threading;
using
System.Globalization;
Change the code to adjust the date for the targeted culture…
public
frmHello()
{
InitializeComponent();
string
todayDate
=
DateTime.Now.ToString("d",
Thread.CurrentThread.CurrentUICulture);
this.lblMessage.Text
=
this.lblMessage.Text.Replace("XX/XX/XXXX",todayDate);
}
14
Globalizing and Localizing .NET Applications for AutoCAD®
Example:
public
frmHello()
{
InitializeComponent();
string
todayDate
=
DateTime.Now.ToString("d",
Thread.CurrentThread.CurrentUICulture);
this.lblMessage.Text
=
string.Format(this.lblMessage.Text,
todayDate);
}
The “string.Format” method will replace the “{0}” in the string with the value of “todayDate”, do
not forget to update the translated strings to contain “{0}” instead of “MM/DD/YYYY” with the
forms “Language” property set to the targeted cultures.
Returns: 100,00 €
15
Globalizing and Localizing .NET Applications for AutoCAD®
Thousands Separator character, in the U.S. it is a comma (,) in Germany it is a period (.)
Decimal separator character, in the U.S. it is a period (.) in Germany it is a comma (.).
Displaying Negative Numbers, a negative sign can be at the beginning or end and can be
represented with parenthesis.
LOCATION DIGITS
Arabic-Indic ٠۰ ١۱ ٢۲ ٣۳ ٤ ٥ ٦ ٧۷ ٨۸ ٩۹
Japanese 〇 一 二 三 四 五 六 七 八 九十
Grouping of digits is the different number of digits contained between each separator.
Percent sign (%) Placement can be written several ways: 100%, 100 % and %100.
Converting Numbers…
This example shows how to convert float to string and string to float. Invariant culture is
internally associated with the English language and it is culture-insensitive.
Handling Plurals…
Some languages have different rules for how plurals need to be handled. This could require
custom handling when targeting a language with multiple plural rules.
16
Globalizing and Localizing .NET Applications for AutoCAD®
Languages handle plurals of nouns different, some languages have multiple forms.
http://www.unicode.org/cldr/charts/dev/supplemental/language_plural_rules.html
Custom code and string entries in the resource file would be required for varying plural
rules for languages with different range, ordinal and cardinal rules.
17
Globalizing and Localizing .NET Applications for AutoCAD®
Previously in the sample project the Windows Form (“frmHello”) had the “Localizable” property
set to True, when that was done the form designer created and managed resource files for the
sizing, Text and other display properties of the form for each language selected.
In the “clsHello” class the AutoCAD CommandMethod was used to add a command and
matching resource files (clsHello.resx and clsHello.fr-FR.resx) were created to manage the
locale command ID for each target language.
As mentioned previously, it is typical to have a vendor or someone other than the developers of
the application do the language translation of the strings used in the application. Typically either
the resource file (.resx) or a “.txt” file containing the string ID names and corresponding values
is supplied for translating.
The next step is to create a main application resource file to handle strings, images or other
object data for the application, add strings to the strings table, and then add the code to use
those strings.
18
Globalizing and Localizing .NET Applications for AutoCAD®
“Main_Strings.resx”
Open the resource file for editing by double-clicking on the file or right-click and select open.
19
Globalizing and Localizing .NET Applications for AutoCAD®
20
Globalizing and Localizing .NET Applications for AutoCAD®
In the code wherever you have strings that are either hard-coded or composed, they need to be
replaced with code that either manages the localization of the string or is pulled from a resource
file.
Make these changes to all strings in the code that need localization. Do not change strings in
designer class files these will be managed by resource managers or overridden after
initialization.
To override the initialization text in the form set the form and control properties that need
localization in the form file constructor after “InitialzeCompnent”.
The “InitializeComponent” method uses the form designer and the forms resource files to resize,
relocate and set properties that were previously set when designing and localizing the form.
21
Globalizing and Localizing .NET Applications for AutoCAD®
This can be accomplished by creating a new resource file named for the targeted culture (e.g.
Main_Strings.fr-FR.resx). Then entering the strings and values into the String table. Or the
existing file can be copied, renamed and then added to the project and update the values for the
strings in the Strings table.
After copying the “Main_Strings.resx” file and renaming the copy to “Main_Strings.fr-FR.resx” in
a Windows explorer window, go to the application and add the new file to the project.
Note: Do not copy, rename and add the “Main_Strings.Designer.cs” file, it is not required for the
targeted culture resource.
Right-click on the
project, in the context
menu, select “Add”
then in the sub-
context menu select
the “Existing Item…”
menu item.
The “Main_Strings.fr-FR.resx” file will now be added to the project. Double-click the file and edit
the values in the string table for the targeted culture.
22
Globalizing and Localizing .NET Applications for AutoCAD®
Note: A resource file needs to be created for each culture the application will target.
23
Globalizing and Localizing .NET Applications for AutoCAD®
The project strings are now localized and will display the translated strings of the targeted
culture.
Build …
When the application is compiled it creates a resources DLL for the targeted culture in a sub-
folder with the culture name (e.g. fr-FR). In this example the folder “fr-FR” contains a file named
“acadHelloWorld.resources.dll”, which contains the resources from the project for that target
culture.
Running in AutoCAD®
Bing translation….
24
Globalizing and Localizing .NET Applications for AutoCAD®
Appendices…
Application Deployment…
When the application is built, the main DLL will be created and a folder for each culture targeted
will be created with a resource DLL…
To have AutoCAD® demand load the application requires deployment to a location that the
AutoCAD® autoloader looks for applications and create required “PackageContents.xml” file.
In the Contents folder copy the application DLL and any localization folders (e.g. “fr-FR”) with
their contents.
In the “.bundle” folder create a file named “PackageContents.xml”, add loading instructions and
application information to the “PackageContents.xml” file…
This example defines the commands to load on invocation defining the commands as…
<Command Global="helloWorld" LocalEnu="helloWorld" LocalFra="SalutMonde"/>
25
Globalizing and Localizing .NET Applications for AutoCAD®
26
Globalizing and Localizing .NET Applications for AutoCAD®
Opens “Add
Existing file to
Resources”
dialog.
27
Globalizing and Localizing .NET Applications for AutoCAD®
Running in AutoCAD®
28
Globalizing and Localizing .NET Applications for AutoCAD®
The “Resgen.exe” application can be used to convert a “.txt” file to a “.resx” file. This can be
useful when sending translation Names/Values to a translation service.
29
Globalizing and Localizing .NET Applications for AutoCAD®
When building the Visual Studio project with the default resource files (e.g. Main_Strings.resx)
and language targeted resource file (e.g. Main_Strings.fr-FR.resx) the folder for the language is
created (e.g. “fr-FR”) containing the resource assembly (e.g. “acadHelloWorld.resources.dll”).
The resource assembly created would then be placed into the language sub-folder in the same
location as the application assembly.
30
Globalizing and Localizing .NET Applications for AutoCAD®
CultureInfo Class…
http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo(v=vs.110).aspx
• Neutral Culture is a language specific culture without regard to the country specific date-time
and currency formats. Use a neutral culture to determine which language specific
resource files the application uses. Specify a neutral culture with two lower case
characters identifying the language (e.g. “en” for English, “fr” for French).
• Specific Culture is the language and country specific culture, which determines the language
to use for the application and also the date-time and currency format to use. Specify the
specific culture with two lower case characters identifying the language and two upper
case characters identifying the country separated by hyphen (e.g. “en-US” for English
(US), “fr-FR” for French (France)).
31
Globalizing and Localizing .NET Applications for AutoCAD®
Summary…
Some of the key notes are that with .NET localization resource (“.resx”) are used for strings,
images and other data. It is important to handle the localization of Windows forms so that they
display in the targeted culture properly.
Always avoid concatenating strings, because it is much better to handle strings with proper
formatting for each culture.
When localizing an application, use online translators for approximation of string lengths and
appearance, not recommended for finished application.
Plurals and numbers need to be properly handled to avoid grammar errors in the targeted
languages.
Links…
32