Documentation

Internationalization and Code Generation

Internationalization support in software development tooling is vital for enabling efficient globalization. If there is any possibility of future collaboration with others across locales, consider internationalization from project inception. Internationalization can prevent rework or having to develop a new model design. The relevant requirement concerns locale settings.

Locale Settings

On a computer, a locale setting defines the language (character set encoding) for the user interface and the display formats for information such as time, date, and currency. The encoding dictates the number of characters that a locale can render. For example, the US-ASCII coded character set (codeset) defines 128 characters. A Unicode®codeset, such as UTF-8, defines more than 1,100,000 characters.

For code generation, the locale setting determines the character set encoding of generated file content. To avoid garbled text or incorrectly displayed characters, the locale setting for your MATLAB®session must be compatible with the setting for your compiler and operating system. For information on finding and changing the operating system setting, seeInternationalization(MATLAB) or see the operating system documentation.

To check a model for characters that cannot be represented in the locale setting of your current MATLAB session, use the Simulink®Model Advisor checkCheck model for foreign characters(Simulink).

Prepare to Generate Code for Mixed Languages and Locales

To prepare to generate code for a model, identify:

  • The operating system locale.

  • The locale of the MATLAB session.

  • Code generation requirements for:

    • Target Language Compiler files

    • Code generation template files that include comments (requires Embedded Coder®)

Character Set Limitations

Target language compiler files support user default encoding only. To produce international, custom generated code that is portable, use the 7-bit ASCII character set.

XML Escape Sequence Replacements

The code generator replaces characters that are not represented in the character set encoding of a model with XML escape sequences. Escape sequence replacements occur for block, signal, and Stateflow®object names that appear in:

  • Generated code comments

  • Code generation reports

  • Block paths logged to MAT-files

  • Block paths logged to C API filesmodel_capi.c(or.cpp) andmodel_capi.h

Generate and Review Code with Mixed Languages and Mixed Locales

This example shows how to use the code generator to generate and review code for use in mixed languages and mixed locales.

Before using this example, seeInternationalization and Code GenerationorInternationalization and Code Generation(Embedded Coder).

Thertwdemo_unicodemodel configuration uses the Embedded Coder (R)ert.tlcsystem target file. To see internationalization and localization support with Simulink Coder®, configure the model to use thegrt.tlcsystem target file. The example indicates the support that is specific to Embedded Coder® (for example, code generation templates).

The model configuration specifies files and settings that control how the code generator handles localization for:

  • C and C++ API interfaces

  • Code generation template (CGT) files (requires Embedded Coder®)

  • Target Language Compiler (TLC) files that apply code customizations (requires Embedded Coder®)

Open the example modelrtwdemo_unicode.

Labels in the model appear in multiple languages (Arabic, Chinese, English, German, and Japanese) and various Unicode symbols.

model ='rtwdemo_unicode';open_system(model);

Verify Locale Settings

Verify that the locale setting for your MATLAB® software is compatible with your compiler. See the documentation for your operating system or the following MATLAB documentation:

Verify Model for Use of Foreign Characters

to verify the model for characters that the code generator cannot represent in the model's current character set encoding, use the Simulink® Model Advisor checkCheck model for foreign characters.

1.Open the Model Advisor in Simulink®. SelectAnalysis>Model Advisor>Model Advisor. Or, in the Command Window, type:

modeladvisor('rtwdemo_unicode')
Loading Model Advisor cache… Warning: Cannot load an object of class 'SDRSLHDLWAPlugin': Its class cannot be found. Warning: Cannot load an object of class 'SDRSLHDLWAPlugin': Its class cannot be found. Model Advisor cache loaded. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method. Updating Model Advisor cache… Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.

2.ExpandBy Product.

3.Expand金宝app.

4.SelectCheck model for foreign characters

5.ClickRun This Check.

6.Review the results. Several warnings appear. Verify that the characters in the model can be represented in the current character set encoding.

7.Close the Model Advisor.

Code Generation Template Files

To use a code generation template file with unicode characters when generating code, complete these steps (requires Embedded Coder®). Otherwise, go to the next section.

1.Open the Configuration Parameters dialog box.

2.Navigate to theCode Generation > Templatepane. The model is configured to use the code generation template filertwdemo_unicode.cgt. That file adds comments to the top of generated code files. For the code generator to apply escape sequence replacements for the.cgtfile, enable replacements by specifying:

3.Open the file/工具箱/ rtw / rtwdemos / rtwdemo_unicode.cgt.

edit rtwdemo_unicode.cgt

4.Find the line of code that enables escape sequence replacements for the character set encodingUTF-8.

5.Close the file/工具箱/ rtw / rtwdemos / rtwdemo_unicode.cgt.

Generated File Customization Template

使用unicode c文件定制模板haracters when generating code, complete these steps (requires Embedded Coder®). Otherwise, go to the next section.

You can specify customizations to generated code files by using TLC code. TLC files support user default encoding only. To produce international custom generated code that is portable, use the 7-bit ASCII character set.

1.Open the Configuration Parameters dialog box.

2.Navigate to theCode Generation > Templatepane. The model is configured to use the code customization fileexample_file_process.tlc. That file customizes the generated code just before the code generator writes the code files. For example, the file adds a C source file, corresponding include file, and#defineand#includestatements.

3.Open the file/toolbox/rtw/rtwdemos/example_file_process.tlc.

edit example_file_process.tlc

4.Before generating code, uncomment the following line of code:

% % %分配ERTCustomFileTest = TLC_TRUE%

5.Close the file/工具箱/ rtw / rtwdemos / rtwdemo_unicode.cgt.

Generate C Code

Generate C code and a code generation report.

evalc('rtwbuild(''rtwdemo_unicode'')');

Review the Generated Code

For characters that are not in the current MATLAB® character set encoding, the code generator uses escape sequence replacements to render characters correctly in the code generation report.

1.If the code generation report for modelrtwdemo_unicodeis not open, in the Command Window, type:

coder.report.open('rtwdemo_unicode')

2.Review the generated code inrtwdemo_unicode.candrtwdemo_unicode.h. Names of model elements appear in code comments as replacement names in the local language.

3.Open the Traceability Report. The report maintains traceability information, even when the name contains characters that are not represented in the current encoding. Names of model elements appear in the report as replacement names in the local language.

4.Scroll down to and click the code location link for the first Chart (State 'Selection' :23). The report view changes to show the corresponding code inrtwdemo_unicode.c.

5.In the code comment, click the:23link. The model window shows the chart in a new tab.

6.In the model window, right-click that chart. SelectC/C++ Code > Navigate to C/C++ Code. The report view changes to show the named constant section of code for that chart.

7.Close the code generation report, Model Advisor, and model. In the Command Window, type:

coder.report.close(); bdclose('all');

Generate C++ Code

Generate C++ code and a code generation report.

1.Open the model.

model ='rtwdemo_unicode';open_system(model);

2.ChangeConfiguration Parameters>Code Generation>LanguagetoC++. Or, in the Command Window, type:

set_param('rtwdemo_unicode','TargetLang','C++');

3.ChangeConfiguration Parameters>Code Generation>Interface>Code interface packagingtoC++ class. Or, in the Command Window, type:

set_param('rtwdemo_unicode','CodeInterfacePackaging','C++ class');

4.Generate C++ code and a code generation report.

evalc('rtwrebuild(''rtwdemo_unicode'')');

5.To see internationalization and localization support, review the generated code. SeeReview the Generated Code.

6.Close the code generation report and model. In the Command Window, type:

coder.report.close(); bdclose('all');

Related Topics

Was this topic helpful?