Internationalization support in software development tooling is vital to enabling efficient globalization. If there is a remote possibility that you could collaborate in the future 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.
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.
To check a model for characters that cannot be represented in the locale setting of your current MATLAB session, use the Simulink® Model Advisor check Check model for foreign characters.
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®)
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.
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
(or model
_capi.c.cpp
)
and model
_capi.h
The code generator replaces characters that are not represented in the character set encoding for a model with XML escape sequences. Escape sequence replacements occur for block, signal, and Stateflow object names that appear in Comments in code generation template (CGT) files.
By default, code generation template files do not contain character set encoding information. The operating system reads the files, using its current encoding, regardless of the encoding that you use to write the file. You can enable escape sequence replacements by adding the following token at the top of the template file:
<encodingIn = "encoding">
Replace encoding
with a string that names a standard
character encoding scheme, such as UTF-8
,
ISO-8859–1
, or windows-1251
.
This example shows content from the file rtwdemo_unicode.cgt in a MATLAB session for windows-1251
. The example uses the
encodingIn
token to set the encoding to UTF–8
,
which is the correct value for code generation.
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.
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:
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 check Check model for foreign characters.
1. Open the Model Advisor. In the Modeling tab, click Model Advisor. Select Model Advisor. Or, in the Command Window, type:
modeladvisor('rtwdemo_unicode')
Updating Model Advisor cache... Model Advisor cache updated. For new customizations, to update the cache, use the Advisor.Manager.refresh_customizations method.
2. Expand By Product.
3. Expand Simulink.
4. Select Check model for foreign characters
5. Click Run 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 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('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 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');