Main Content

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, see Internationalization or see the operating system documentation.

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 files model_capi.c (or .cpp) and model_capi.h

Generate and Review Code with Mixed Languages and Mixed Locales

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

Before using this example, see Internationalization and Code Generation or Internationalization and Code Generation (Embedded Coder).

The rtwdemo_unicode model configuration uses the Embedded Coder (R) ert.tlc system target file. To see internationalization and localization support with Simulink Coder®, configure the model to use the grt.tlc system 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 model rtwdemo_unicode.

Open example model rtwdemo_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);
%

Open the Embedded Coder App

In the Apps tab, select Embedded Coder.

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:

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 the Code Generation > Template pane. The model is configured to use the code generation template file rtwdemo_unicode.cgt. That file adds comments to the top of generated code files. For the code generator to apply escape sequence replacements for the .cgt file, enable replacements by specifying:

<encodingIn = "encoding-name">

3. Open the file /toolbox/rtw/rtwdemos/rtwdemo_unicode.cgt.

edit rtwdemo_unicode.cgt

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

<encodingIn = "UTF-8">

5. Close the file /toolbox/rtw/rtwdemos/rtwdemo_unicode.cgt.

Generated File Customization Template

To use file customization templates with unicode characters 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 the Code Generation > Template pane. The model is configured to use the code customization file example_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 #define and #include statements.

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:

%% %assign ERTCustomFileTest = TLC_TRUE%

5. Close the file /toolbox/rtw/rtwdemos/example_file_process.tlc.

Generate C Code and a Report

Generate C code and a code generation report.

evalc('slbuild(''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 model rtwdemo_unicode is not open, in the Command Window, type:

coder.report.open('rtwdemo_unicode')

2. Review the generated code in rtwdemo_unicode.c and rtwdemo_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' <S2>:23). The report view changes to show the corresponding code in rtwdemo_unicode.c.

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

6. In the model window, right-click that chart. From the context menu, select C/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. Set model configuratoin parameter Language to C++. Or, in the Command Window, type:

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

3. Set model configuration parameter Code interface packaging to C++ 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. See Review the Generated Code.

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

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

Related Topics