Usage
Internationalization (i18n) is an essential part of developing Smart MiniApp or Panel MiniApp. It helps applications adapt to users from different languages and cultures, improving user experience and market reach. Therefore, we have built an I18N module into the Panel SDK. This module extends the functionalities of the built-in I18n object to meet the multilingual needs of Panel MiniApp.
Getting Started
First, ensure that you have installed @ray-js/panel-sdk. You can install it using the following command:
Then ensure the project's multilingual file structure is as shown below. You can refer to the public-sdm template:
Define Multilingual Strings
Local code's multilingual strings are generally only used for quickly viewing the multilingual entries involved in the project and for fallback in case of exceptions, such as when online multilingual strings are empty. Additionally, local code cannot fully support multiple regions for other languages like zh_CN, en_GB, etc. Therefore, it is recommended not to define multilingual strings other than Chinese and English in local code.
Edit the src/i18n/strings.{js,ts} file.
Instantiate Multilingual Strings
Instantiate and export in the src/i18n/index.{js,ts} file.
Use Multilingual Strings
You can call it in any scenario where multilingual strings are needed. For example, in a component:
Platform Configuration for Multilingual Strings
Configure MiniApp Multilingual Strings
MiniApp multilingual strings need to be added on the MiniApp Developer Platform. Select the corresponding MiniApp and click on the Multilingual Management sidebar.
Configure Product Multilingual Strings
Product multilingual strings only apply to Panel MiniApp, and the configuration priority of product multilingual strings is higher than that of Panel MiniApp. See Q: What is the multilingual logic of Panel MiniApp?
Product multilingual strings need to be added on the Tuya Developer Platform - Product Multilingual. Select the corresponding product and click on the corresponding tab to configure the multilingual strings.
Advanced Development
Multilingual Specifications
Multilingual API
Get Multilingual String
Name
getLang
Description
Gets the multilingual string corresponding to the key value. If it does not exist in the current language, it will look up in en. If it does not exist either, it returns defaultValue or ${key}.
Request Parameters
Return Parameters
Request Example
Return Example
Get Datapoint Multilingual String
Name
getDpLang
Description
Gets the multilingual string corresponding to the DP code. If it does not exist in the current language, it will look up in en. If it does not exist either, it returns dp_${code}_${value}.
Compared with getLang, getDpLang is mainly used to standardize the acquisition of multilingual strings related to datapoints, generally only used in Panel MiniApp.
For example: getLang('dp_switch_on') is equivalent to getDpLang('switch'.toLowerCase(), true);
Request Parameters
Return Parameters
Request Example
Return Example
Get Datapoint Name
Name
getDpName
Description
Gets the name corresponding to the DP point.
For the sake of DP naming conventions, the passed DP code will be converted to lowercase letters, such as Switch will be converted to switch, generally only used in Panel MiniApp.
Request Parameters
Return Parameters
Request Example
Return Example
Get Fault Datapoint Multilingual String
Name
getFaultLang
Description
Gets the multilingual strings corresponding to fault-type datapoints (Bitmap type). This method is used to handle multilingual display of device fault information, supporting bit-wise parsing of fault codes and returning corresponding multilingual descriptions. Compared with the deprecated getFaultStrings, getFaultLang provides a more modern parameter structure and more flexible configuration options.
getFaultLang is specifically designed for processing Bitmap type fault datapoints, obtaining corresponding fault descriptions by parsing each bit of the fault value. The multilingual key follows the dp_${faultCode}_${label} naming convention. This API is supported starting from @ray-js/panel-sdk version 1.14.0.
Request Parameters
Return Parameters
Request Example
Return Example
Replace Multilingual Template Variables
Name
formatValue
Description
The formatValue method is used to get the template string of the specified key from the language package and replace the passed values in the placeholder positions of the template string.
Request Parameters
Return Parameters
Request Example
Return Example
Notes
Please ensure that numbers like 0-9 are not used as keys for multilingual configuration, as the underlying implementation is based on I18n.t. For details, refer to Multilingual Placeholders.
Multilingual Language Switching Test
The language environment of the MiniApp depends on the current running environment, i.e., Tuya MiniApp IDE or App Real Machine. Currently, dynamic language switching is not supported. If you need to perform language switching tests, you can do so by the following methods:
- Switch the language on the
App Real Machine. - Switch the language in the
Tuya MiniApp IDE, as shown below.
FAQs
For more multilingual FAQs, see Help Center - Multilingual
Q: Why does the multilingual string defined in my local code not take effect?
A: The multilingual string defined locally has a lower priority than the multilingual string uploaded on the platform. If the multilingual string is uploaded on the platform, the locally defined multilingual string will not take effect.
Q: Why does the multilingual string I modified on the MiniApp platform not take effect?
A1: If it is a Smart MiniApp, if you confirm that the multilingual key used in the business code is consistent with the multilingual key modified on the platform, and you have cleared the app cache and re-entered the MiniApp, if it still does not take effect, please contact Tuya technical support.
A2: If it is a Panel MiniApp, follow the steps below to troubleshoot:
- Check whether the Panel MiniApp associated with the device's corresponding product is currently running in the
Tuya MiniApp IDEor on the App real machine.
Note 1: The multilingual strings of the Panel MiniApp during runtime are based on the product associated with the current device, which means the multilingual strings of the current product associated with the Panel MiniApp are fetched. Therefore, if the multilingual strings of other MiniApp are modified, they will not take effect. For more details, see Q: What is the multilingual logic of Panel MiniApp?
- Check whether the product associated with the device currently running in the
Tuya MiniApp IDEor on the App real machine has the same multilingual key configured and has written different multilingual values.
Note 2: The configuration priority of product multilingual strings is higher than that of Panel MiniApp multilingual strings. For more details, see Q: What is the multilingual logic of Panel MiniApp?
- If you confirm that the above two steps are correct, and you have cleared the app cache and re-entered the MiniApp, if it still does not take effect, please contact Tuya technical support.
Q: What is the multilingual logic of Panel MiniApp?
A: Since Panel MiniApp can be associated with multiple products at the same time, and different products may require different multilingual configurations, the multilingual design logic of Panel MiniApp is as follows:
In other words, the priority of product multilingual configuration is the highest, followed by Panel MiniApp multilingual configuration, and finally local multilingual configuration.
Q: Why does multilingual upload fail?
A: You can troubleshoot according to the following steps:
- Check whether the uploaded multilingual file meets the platform's requirements for EXCEL or JSON format and is smaller than 1M.
- Check whether the uploaded multilingual file content contains scenarios where Chinese is written under English, as shown below:
- Check whether the uploaded multilingual file content has mismatched keys between Chinese and English, as shown below:
- Check whether the uploaded multilingual file content contains duplicate multilingual keys, as shown below (commonly occurring in EXCEL):