0% found this document useful (0 votes)
99 views32 pages

Globalizing and Localizing .NET Applications For AutoCAD

Globalizing and Localizing .NET Applications for AutoCAD

Uploaded by

John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views32 pages

Globalizing and Localizing .NET Applications For AutoCAD

Globalizing and Localizing .NET Applications for AutoCAD

Uploaded by

John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Globalizing and localizing .

NET Applications for AutoCAD®


James E. Johnson – Synergis Software

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

About the Speaker


James Johnson has worked with CAD products for more than 25 years in many positions, from
being a CAD drafter to writing automation applications. In his current position he is doing CAD
integration for adept document management system. In previous positions he used
RealDWG® to write custom automation to create AutoCAD software drawings of industrial
kitchen equipment, and he worked at Autodesk resellers in software development groups doing
custom applications for Inventor software and AutoCAD software. He has taught AutoCAD
software and AutoCAD Microsoft Visual Basic software classes while working for resellers, and
he was a CAD instructor at 2 different community colleges.

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®

Introduction: Globalization, Internationalization and Localization


Globalization:
The process involves designing and developing products, services, applications or document
content that supports localized interfaces and regional data for users of multiple cultures and
regions.

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®

Create an application that supports localized interfaces and regional


data for multiple cultures…
Start a new Application and Add required references…
Open Visual Studio and create a new project, for an AutoCAD® add in application create a
“Class Library” project.

This will start an


empty C# project
that we will
require adding
references and
changes to
become an
AutoCAD® addin.
n
io
at
ic
pl
Ap

 
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

Set “Copy Local” to false for all 3 references…

4
Globalizing and Localizing .NET Applications for AutoCAD®

Rename Class name and add Using statements…

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

       }  
}  

Add a new Windows form to the project and rename to frmHello…


Add Buttons, Labels or other controls and change display text properties.

Windows Form: frmHello

Label: lblMessage

Buttons:……………………………..btnOK….…btnCancel..….btnAddText

5
Globalizing and Localizing .NET Applications for AutoCAD®

Add additional code to the “frmHello” form …

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®

Add AutoCAD® commands to the “clsHello” class…


 

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 …

Load into AutoCAD® with the NETLOAD command…

7
Globalizing and Localizing .NET Applications for AutoCAD®

Localize the Application…

Localize AutoCAD® Command


The CommandMethod attribute is used for defining a method to be exposed in AutoCAD® as a
command. It needs a string value to use as a global command name (e.g, “helloWorld”). The
CommandMethod attribute can accept other values and it is one of these that will allow setting
up a localized name.

CommandMethod with only a global name:

[CommandMethod("helloWorld")]

CommandMethod with group name, global name, local command ID and a command flag:

[CommandMethod("HW",  "_helloWorld",  "helloID",  CommandFlags.Session)]

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.

public  class  clsHello  :  IExtensionApplication  


{  
       [CommandMethod("HW",  "_helloWorld",  "helloID",  CommandFlags.Session)]  
       public  void  helloWorld()  
       {  
               frmHello  hi  =  new  frmHello();          Command ID  
               Application.ShowModalDialog(hi);  
       }  
}  

The class name in the example is


“clsHello”, add a resource file with
that name…

Right-click on the project, in the


context menu, select “Add” then in
the sub-context menu select the
“New Item…” menu item.

This opens Add New Item


dialog.

8
Globalizing and Localizing .NET Applications for AutoCAD®

Scroll down and


select Resource file.

Change the name of


the resource file to
match the class that
contains the
commands, in this
example, change the
name to:
“clsHello.resx”

Click the “Add” button at bottom right


corner of the dialog to add the
resource file to the project.

Open the resource file for editing by double-


clicking on the file or right-click and select
open.

Then add the local name id (e.g. “helloID”)


specified in the CommandMethod with a
value that will be the local command in
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®

A resource file needs to be added for


each language with the culture name
added to the filename (e.g.
clsCommands.fr-FR.resx) that the
application targets…

Add the same ID with the


command translated for the
culture that is used in the files
name.

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.).

In this example “_helloWorld” is the global command name…

[CommandMethod("HW",  "_helloWorld",  "helloID",  CommandFlags.Session)]  

10
Globalizing and Localizing .NET Applications for AutoCAD®

Localize an applications user interface…


As noted above, strings, form controls and message boxes will change length when translated
for different cultures and need room to grow or be relocated.

Also, plan and adjust the user interface for languages that read left to right and right to left.

To localize a Windows form the


first step is to set the “Localizable”
property to true.

This creates a resource file in the


forms file collection when a
property or control is set and
updates with each edit.

Change the Forms “Language”


property to the target language
culture.

Any new edits to controls or


properties with the language set
will first create a resource file
and then save any changes to
that ne resource.

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.

Use culture specific settings (.NET CultureInfo class)…


The form is now localized and will display properly if loaded into a localized version of
AutoCAD® for any of the languages that have been targeted. If you do not have a localized
version installed you can set the culture to display for the desired culture.

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:

public  void  Initialize()  


{  
       CultureInfo  cult  =  new  CultureInfo("fr-­‐FR");  
       Thread.CurrentThread.CurrentUICulture  =  cult;  
}  
 
This will set the current threads UI (user interface) culture to the targeted culture. Build the
application, start a session of AutoCAD® and “Netload” the projects DLL. The localized
command will be the value entered in the “clsCommands” resource file for the targeted culture
(e.g. “SalutMonde” in the above example).

Note: The AutoCAD® sessions default culture can be acquired with the following code…

CultureInfo  cult  =  new  CultureInfo(SystemObjects.DynamicLinker.ProductLcid);  

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());  
}  

With a localized string the date format would not be correct…

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);  
}  

The result adjusts the date to DD/MM/YYYY instead of MM/DD/YYYY…

14
Globalizing and Localizing .NET Applications for AutoCAD®

Setup strings for Formatting…


Instead of using the “.Replace” method on the string, you should format the string using
“string.Format” which replaces each format item (e.g. “{0} {1}”) in the string with the value of the
related object's value.

Example:

string  firstName  =  "John";  


string  lastName  =  "Smith";  
string  todayDate  =  DateTime.Now.ToString("d",  Thread.CurrentThread.CurrentUICulture);  
         
string  multiSubst  =  "Hello  {0}  {1},  todays  date  is  {2}";  
string  substituted  =  string.Format(multiSubst,  firstName,  lastName,  todayDate);  

In the example form…


Change from: “Hello World todays date is MM/DD/YYYY.” To: Hello World todays date is {0}.

Then change code to…

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.

Standard Numeric Format…

These are used to return strings in commonly used formats.

An example of this would be for handling numbers for


currency…

double  val  =  100.00;  


CultureInfo  cult  =  new  CultureInfo("fr-­‐FR");  
string  currency  =  string.Format(cult,  "{0:C}",  val);  

Returns: 100,00 €

15
Globalizing and Localizing .NET Applications for AutoCAD®

Standard Numeric Format (continued)…


The following items need to be handled when working with numeric values. Hard coding the way
numbers and characters are displayed can never be assumed…

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.

Number Shapes can be different in various locales.

LOCATION DIGITS

English (United States) 0123456789

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.

Convert float to string…  


float  num  =  52.01f;  
string  strEng  =  num.ToString(CultureInfo.InvariantCulture.NumberFormat);                  
//Returns  52.01  
string  strFR  =  num.ToString(CultureInfo.GetCultureInfo("fr-­‐FR").NumberFormat);  
//Returns  52,01  
 
Convert string to float…  
float  numEng  =  float.Parse("52.01",  CultureInfo.InvariantCulture.NumberFormat);  
float  numFR  =  float.Parse("52,01",  CultureInfo.GetCultureInfo("fr-­‐FR").NumberFormat);

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

English from link… French from link…

Values need to be expressed for plural forms


(e.g. “0 circles”, “1 circle” or “2 circles”).

In the Main_Strings.resx and Main_Strings.fr-FR.resx add entries for the words or


phrases that require plurals with changing units…

int  count  =  12345;  


string  sCircle  =  string.Empty;  
string  sCount  =  count.ToString("N0",  CultureInfo.CurrentCulture);    
 
if  (count  ==  0)  
{  
       sCircle  =  string.Format(acadHelloWorld.Main_Strings.circleZero,  sCount);  
}  
else  if  (count  ==  1)  
{  
       sCircle  =  string.Format(acadHelloWorld.Main_Strings.circleOne,  sCount);  
}  
else  if  (count  >  1)  
{  
       sCircle  =  string.Format(acadHelloWorld.Main_Strings.circleOther,  sCount);  
}

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®

Create resource files for localization string values and resources…


Resources like strings, images or object data are stored in resource files with the extension
“.resx” to make them available to your application.

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.

Add Main Resource file…

Right-click on the project, in the


context menu, select Add then
in the sub-context menu select
the “New Item…” menu item.

This opens Add New Item


dialog.

18
Globalizing and Localizing .NET Applications for AutoCAD®

Scroll down and select


Resource file.

Change the name of the


resource file to match the
class that contains the
commands, in this
example, change the
name to:

“Main_Strings.resx”

Click the “Add” button at bottom


right corner of the dialog to add
the resource file to the project.

Open the resource file for editing by double-clicking on the file or right-click and select open.

Search the code and


add an entry in the
string table for each
text string in the
Application. Keep ID
names simple but
easy to understand
the associated values
usage.

19
Globalizing and Localizing .NET Applications for AutoCAD®

Set the culture of the resource file…


In the “csHello.cs” class file add code to set the culture of the resource file in the “clsHello” class
to set the culture when the application is initialized.

public  void  Initialize()  


{  
       //  Test  Culture  
       //CultureInfo  cult  =  new  CultureInfo("fr-­‐FR");  //French  (France)  
 
       //  Get  current  AutoCAD  session  culture  
       CultureInfo  cult  =  new  CultureInfo(SystemObjects.DynamicLinker.ProductLcid);  
 
       //Set  User  Interface  Culture  
       Thread.CurrentThread.CurrentUICulture  =  cult;  
       Thread.CurrentThread.CurrentCulture  =  cult;  
 
       //  Set  culture  of  resource  file  
       acadHelloWorld.Main_Strings.Culture  =  cult;        
}

Apply localized text strings into code…


In the resource file (“Main_Strings.resx”) verify that the Access Modifier is set to “Internal” or
“Public”. This sets the resource file “Custom Tool” property to “ResxFileCodeGenerator” or
“PublicResxFileCodeGenerator” which creates a designer and each ID name added to the
Strings table is displayed in “Intellisense”.

public: Access is not restricted.

Internal: Access is limited to the current assembly.

20
Globalizing and Localizing .NET Applications for AutoCAD®

Apply localized text strings into code (continued)…

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.

Existing code sample:

   MText  text  =  new  MText();  


   text.Contents  =  "Hello  World  !!";  

Replace code with using the resource strongly typed Name:                                                        

   MText  text  =  new  MText();  


   text.Contents  =  acadHelloWorld.Main_Strings.helloWorld;  
 
Or using the resource files resource manager “GetString” method:

   MText  text  =  new  MText();  


   text.Contents  =  acadHelloWorld.Main_Strings.ResourceManager.GetString("helloWorld");  

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”.

public  frmHello()   Set strings for the form and controls


{  
       InitializeComponent();   from the “Main_Strings.resx”
  resource file.
       this.Text  =  acadHelloWorld.Main_Strings.frmTitle;  
       this.btnCancel.Text  =  acadHelloWorld.Main_Strings.btnCanceltext;  
       this.btnOK.Text  =  acadHelloWorld.Main_Strings.btnOKtext;  
       this.btnCircle.Text  =  acadHelloWorld.Main_Strings.btnCircletext;  
 
       string  todayDate  =  DateTime.Now.ToString("d",  Thread.CurrentThread.CurrentUICulture);  
       this.lblMessage.Text  =    
               string.Format(acadHelloWorld.Main_Strings.lblMessText,  todayDate);  
 
}

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®

Creating Resource files for target Cultures…


The resource file “Main_Strings.resx” was created and the Strings table was populated with the
strings in the application that need to be translated. To have the strings show the translated
values when the application is ran, the project needs to have resource files created for each
targeted culture (e.g. Main_Strings.fr-FR.resx) with the same string names entered into the
Strings table of the “Main_Strings.resx” files Strings table with translated values.

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.

This opens the “Add


Existing Item” dialog.

Select the new


resource file and
select the “Add”
button.

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®

For testing and visual


sizing on line
translation utilities can
be used, but someone
that can do a “proper”
translation should
translate the final
product.

Note: A resource file needs to be created for each culture the application will target.

Setting the Culture…


Verify that the proper culture is set in the applications initialization class (“csHello.cs”) file in the
class that implements the “IExtensionApplication” interface (“clsHello”).

The culture can be overridden…


CultureInfo  cult  =  new  CultureInfo("fr-­‐FR");  //French  (France)  

Or set to the AutoCAD® default product…


CultureInfo  cult  =  new  CultureInfo(SystemObjects.DynamicLinker.ProductLcid);

Set this in the initialization class of the project…

public  void  Initialize()  


{   Get the current AutoCAD®
       //  Test  Culture  
       //CultureInfo  cult  =  new  CultureInfo("fr-­‐FR");  
sessions culture.
 
       //  Get  current  AutoCAD  session  culture  
       CultureInfo  cult  =  new  CultureInfo(SystemObjects.DynamicLinker.ProductLcid);  
 
       //Set  User  Interface  Culture  
       Thread.CurrentThread.CurrentUICulture  =  cult;  
 
       //  Set  culture  of  resource  file  
       acadHelloWorld.Main_Strings.Culture  =  cult;  
}  

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 …

Load into AutoCAD® with the NETLOAD command…

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…

Contents of “fr-FR” folder…

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 “C:\Program Files\Autodesk\ApplicationPlugins” create a folder to contain the application with


the extension “.bundle”…
“C:\Program Files\Autodesk\ApplicationPlugins\acadHelloWorld.bundle”

In that folder create a contents folder…


“C:\Program Files\Autodesk\ApplicationPlugins\acadHelloWorld.bundle\Contents”

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®

Additional Command (using plurals code)…


The CommandMethod attribute is used to add commands. Add the following to the “csHello.cs”
class to add a new command that uses the plurl handling to count the circles in the drawing…

[CommandMethod("HW",  "_circleCount",  "circleCountID",  CommandFlags.Session)]  


public  void  circleCount()  
{  
     int  count  =  0;  
     Database  db  =  Application.DocumentManager.MdiActiveDocument.Database;  
     using  (Transaction  tr  =  db.TransactionManager.StartTransaction())  
     {  
           BlockTable  bt  =  (BlockTable)tr.GetObject(db.BlockTableId,  OpenMode.ForRead);  
           BlockTableRecord  btr  =  (BlockTableRecord)tr  
                     .GetObject(bt[BlockTableRecord.ModelSpace],  OpenMode.ForRead);  
 
           List<Circle>  dbCirc  =  btr.Cast<ObjectId>()  
             .Where<ObjectId>(oid  =>  (oid.ObjectClass.DxfName  ==  "CIRCLE"))  
             .Select<ObjectId,  Circle>(oid  =>  (Circle)(oid.GetObject(OpenMode.ForRead,  false)))  
             .ToList();  
 
           count  =  dbCirc.Count();   Use sample code from “Handling
     }   Plurals…” above to set alert string…
 
     string  sCircle  =  string.Empty;  
     string  sCount  =  count.ToString("N0",  CultureInfo.CurrentCulture);  
 
     if  (count  ==  0)  
     {  
             sCircle  =  string.Format(acadHelloWorld.Main_Strings.circleZero,  sCount);  
     }  
     else  if  (count  ==  1)  
     {  
             sCircle  =  string.Format(acadHelloWorld.Main_Strings.circleOne,  sCount);  
     }  
     else  if  (count  >  1)  
     {  
             sCircle  =  string.Format(acadHelloWorld.Main_Strings.circleOther,  sCount);  
     }  
 
     Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(sCircle);  
}

Add Command ID with local commands to the class resource files…

26
Globalizing and Localizing .NET Applications for AutoCAD®

Add Control and Manage Images…


Adding additional controls after localization of a form requires verifying that the new control is
properly sized and located for each targeted language. Images can be managed for localization
with resource files.

To add images to the resource file open the


Main_Strings.resx file for edit and select
images.

Then select ”Add Resource” and select “Add


Existing File”.

Opens “Add
Existing file to
Resources”
dialog.

Browse to the file (e.g. America.jpg) and select


open…
Rename the image to a common name (e.g.
FlagImage)…

Repeat the above using the


Main_Strings.fr-FR.resx file and add an
image for a French flag (e.g. France.jpg)
and rename to the same image name
(e.g. FlagImage).

27
Globalizing and Localizing .NET Applications for AutoCAD®

Add a “pictureBox” control to the “frmHello”


form.

Set the “pictureBox” Image property to the


“FlagImage” in the Main_Strings.resx file.

Running in AutoCAD®

The images are loaded from the resource file


using the designer resource manager and
culture information.

28
Globalizing and Localizing .NET Applications for AutoCAD®

Use Resgen.exe to convert “.txt” file to “.resx”…


The Resource File Generator (Resgen.exe) is a conversion utility that does the following…
http://msdn.microsoft.com/en-us/library/ccec7sz1(v=vs.110).aspx

• Converts “.txt” or “.restext” files to “.resources” or “.resx” files.


• Converts “.resources” files to text or “.resx” files.
• Converts “.resx” files to text or “.resources” files.
• Extracts the string resources from an assembly into a “.resx” file.
• Creates a strongly typed class that provides access to individual named resources.

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.

Text file in form <Name> = <Value> …

Run “Resgen.exe” from command window


to generate the “.resx” file.

Resgen <resource.txt> <resource.resx>

Resource file created from running


resgen.exe can be added to the Visual
Studio project.

After receiving the “.txt” file from the


translation service, resgen.exe can be
used to convert the data to a “.resx”
file.

29
Globalizing and Localizing .NET Applications for AutoCAD®

Use AL.exe to Create Language Resources Assembly…


The Assembly Linker generates an assembly from one or more resource files.
http://msdn.microsoft.com/en-us/library/c405shex(v=vs.110).aspx

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”).

This could also be built using the Assembly Linker (Al.exe).

Run “Resgen.exe” to create a “.resources” file…

Creates the resource.resources file from the resource.resx file.

resgen resource.resx Creates: resource.resources


resgen resource.fr-FR.resx Creates: resource.fr-FR.resources

Run the Assembly Linker (Al.exe to generate an embedded resource assembly…

Creates the file ALResource.resources.dll for an application named ALResource.dll.

Al /t:lib /embed:resource.resources /culture:en-US /out:ALResource.resouces.dll


Al /t:lib /embed:resource.fr-FR.resources /culture:fr-FR /out:ALResource.fr-FR.resouces.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

The “System.Globalization” namespace is used to make an application culture aware. The


primary class in the “System.Globalization” namespace is the “CultureInfo” class.

CultureInfo  cult  =  new  CultureInfo("fr-­‐FR");  //French  (France)  

The “System.Threading.Thread.CurrentCulture” is used to get or set a specific culture for the


applications current thread.

The “System.Threading.Thread.CurrentUICulture” is used to get or set the current culture used


by the Resource Manager to look up culture-specific resources.
 
 
public  void  Initialize()  
{  
       //  Test  Culture  
       //CultureInfo  cult  =  new  CultureInfo("fr-­‐FR");  //French  (France)  
 
       //  Get  current  AutoCAD  session  culture  
       CultureInfo  cult  =  new  CultureInfo(SystemObjects.DynamicLinker.ProductLcid);  
 
       //Set  User  Interface  Culture  
       Thread.CurrentThread.CurrentUICulture  =  cult;  
       Thread.CurrentThread.CurrentCulture  =  cult;  
 
       //  Set  culture  of  resource  file  
       acadHelloWorld.Main_Strings.Culture  =  cult;  
}  

• Invariant Culture is a culture-independent (invariant) culture. Use Invariant Culture when


comparing strings, or for displaying and comparing dates, the default is “en-US” culture.

• 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…

Command Definition (.NET)


http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-F77E8FE0-8034-4704-93BD-
F717608F8223

Registering AutoCAD commands with localized names using .NET


http://through-the-interface.typepad.com/through_the_interface/2009/06/registering-autocad-
commands-with-localized-names-using-net.html

Autoloader and localization


http://adndevblog.typepad.com/autocad/2013/05/autoloader-and-localization.html

Autoloader Example for invoking a Startup Command in AutoCAD


http://adndevblog.typepad.com/autocad/2013/04/autoloader-example-for-invoking-a-startup-
command-in-autocad.html

Creating Resource Files for Desktop Apps


http://msdn.microsoft.com/en-us/library/xbx3z216(v=vs.110).aspx

Plurals & Units


http://www.unicode.org/cldr/charts/dev/supplemental/language_plural_rules.html

.NET - Localization using Resource file


http://www.codeproject.com/Articles/5447/NET-Localization-using-Resource-file

Twelve Commandments Of Software Localization


http://www.smashingmagazine.com/2012/07/18/12-commandments-software-localization/

32

You might also like