PowerApps 中的 Language 函式Language function in PowerApps

傳回目前使用者慣用語言的語言標記。Returns the language tag of the current user.

說明Description

Language 函式會傳回目前使用者的語言、指令碼和區域做為語言標記。The Language function returns the language, script, and region of the current user as a language tag.

使用語言資訊自訂您在地區設定的應用程式。Use the language information to tailor your app across locales. 例如,如果您要建立用於義大利和法國的應用程式,您可以使用 Language 自動顯示義大利文和法文字串給您在這些不同位置的使用者。For example, if you are creating an app that will be used in Italy and France, you can use Language to automatically display Italian and French strings to your users in those different locations.

語言標記Language tags

語言標記可為三種格式之一︰A language tag can be in one of three formats:

傳回值Return value 說明Description
"lg‑RE""lg‑RE" lg 是語言的兩個字元縮寫,而 RE 是區域的兩個字元縮寫。lg is the two character abbreviation for the language and RE is the two character abbreviation for the region. 這是最常見的傳回類型。This is the most common return type. 例如,英國會傳回 "en-GB"。For example, "en-GB" is returned for Great Britain.
"lg""lg" lg 是語言的兩個字元縮寫。lg is the two character abbreviation for the language. 這是當 PowerApps 具有語言的相關資訊,但並沒有特定區域的資訊時所使用的格式。This is the format used when PowerApps has information about the language, but does not have information for the specific region.
"lg‑scrp‑RE""lg‑scrp‑RE" lg 是語言的兩個字元縮寫,scrp 是指令碼的四個字元縮寫,而 RE 是區域的兩個字元縮寫。lg is the two character abbreviation for the language, scrp is the four character abbreviation for the script, and RE is the two character abbreviation for the region.

PowerApps 使用 IETF BCP-47 語言標記格式。PowerApps uses the IETF BCP-47 language tag format.

若要查看支援語言標記清單,請在公式列或進階檢視中輸入 Value( "1", ),然後捲動針對第二個引數建議的地區設定清單。To see the list of supported language tags, type Value( "1", ) in the formula bar or advanced view and scroll through the list of locales suggested for the second argument.

TextValue 函式也會使用語言標記。The Text and Value functions also use language tags. 以全球關注的方式使用這些函式來回轉譯文字字串。Use these functions for translating to and from text strings in a globally aware manner. 將語言標記傳遞至這些函式和區域不會造成差異時,您可以只使用標記的語言部分。When passing a language tag to these functions and the region would not make a difference, you can use just the language portion of the tag.

語法Syntax

Language()Language()

範例Examples

使用者的地區設定User's locale

假設主機作業系統和/或瀏覽器會使用位置的預設地區設定。It is assumed that the host operating system and/or browser are using the default locale for the location.

公式Formula 位置Location 傳回值Return Value
Language()Language() 里斯本,葡萄牙Lisbon, Portugal "pt-PT""pt-PT"
Language()Language() 里約熱內盧,巴西Rio de Janeiro, Brazil "pt-BR""pt-BR"
Language()Language() 亞特蘭大,美國Atlanta, USA "en-US""en-US"
Language()Language() 曼徹斯特,英國Manchester, Great Britain "en-GB""en-GB"
Language()Language() 巴黎,法國Paris, France "fr-FR""fr-FR"
Language()Language() 羅索,多米尼克Roseau, Dominica "en""en"
Language()Language() 貝爾格勒,塞爾維亞Belgrade, Serbia "sr-cyrl-RS" 或 "sr-latn-RS",依使用者的系統設定而定。"sr-cyrl-RS" or "sr-latn-RS", depending on the user's system settings

當地語系化資料表Localization table

當地語系化的簡單方法是建立作者定義的 Excel 試算表 TextID 對應到使用者語言的翻譯文字。A simple approach to localization is to create an Excel spreadsheet mapping an author defined TextID to a translated text for the user's language. 雖然您可以將集合或任何其他資料來源用於此資料表,我們選擇 Excel 是因為它可輕鬆在應用程式外部由轉譯程式編輯和管理。Although you could use a collection or any other data source for this table, we chose Excel because it is easy to edit and manage outside of the app by translators.

  1. 在 Excel 中建立下列資料表︰Create the following table in Excel:

    如果找不到指定語言的特定文字字串,語言資料行的項目為空白會做為預設值。The entry with blank for the Language column will be used as the default if there is no specific text string found for a given language. 此項目必須出現在指定 TextID 之後的所有其他項目。This entry must appear after all other entries for a given TextID.

    針對本文目的,我們只需要查看語言的地區設定而不需要查看區域。For our purposes, we only need to look at the language of the locale and not the region. 如果區域考量很重要,我們會在上列資料表中包含完整的語言標記值。If regional considerations were important, we could have included the full language tag value in the table above.

  2. 使用 [插入] 功能區,資料表命令,將此設為適當的 Excel 資料表。Use the Insert ribbon, Table command, to make this into a proper Excel table. 根據預設,它將會命名為 Table1,但您可以使用 [資料表工具/設計]功能區和最左邊的 [資料表名稱︰]文字方塊將它任意命名。By default, it will be named Table1 but you can name it whatever you like with the Table Tools/Design ribbon and the Table Name: text box on the far left hand side.
  3. 將 Excel 檔案儲存至本機檔案系統。Save the Excel file to your local file system.
  4. 在 PowerApps 中,按一下或點選 [資料來源] 索引標籤,然後按一下或點選 [新增資料來源]。In PowerApps, in the right-hand pane, click or tap the Data Sources tab, and then click or tap Add data source.
  5. 按一下或點選 [對應用程式新增靜態資料],並按一下或點選所儲存的 Excel 檔案,然後按一下或點選 [開啟]。Click or tap Add static data to your app, click or tap the Excel file that you saved, and then click or tap Open.
  6. 選取您所建立的資料表,然後按一下或點選 [連線]。Select the table that you created, and then click or tap Connect.

在您的應用程式中,在您之前使用過文字 "Hello" 之處,改為使用此公式︰In your app, wherever you would have used the text "Hello" before, use this formula instead:

  • LookUp( Table1, TextID = "Hello" && (LanguageTag = Left( Language(), 2 ) || IsBlank( LanguageTag ))).LocalizedTextLookUp( Table1, TextID = "Hello" && (LanguageTag = Left( Language(), 2 ) || IsBlank( LanguageTag ))).LocalizedText

此公式會查詢使用者之語言的適當 LocalizedText 值,如果找不到,則會切換回預設的空白版本。This formula will lookup the appropriate LocalizedText value for the language of the user, and if that is not found, will fall back on the default blank version.

請注意,其他語言的翻譯字串可能會比您所用語言的字串長得多。Be aware that translated strings in other languages could be significantly longer than they are in your language. 在許多情況下,於使用者介面中顯示字串的標籤和其他元素必須更寬才能容納得下。In many cases, the labels and other elements that display the strings in your user interface will need to be wider to accommodate.

轉譯服務Translation service

您可以使用轉譯服務 (例如 Microsoft Translator 服務) 隨需轉譯文字︰You can translate text on demand using a translation service, such as the Microsoft Translator service:

  1. 在 PowerApps 中,按一下或點選 [資料來源] 索引標籤,然後按一下或點選 [新增資料來源]。In PowerApps, in the right-hand pane, click or tap the Data Sources tab, and then click or tap Add data source.
  2. 按一下或點選 Microsoft TranslatorClick or tap Microsoft Translator.

在您的應用程式中,在您之前使用過文字 "Hello" 之處,改為使用此公式︰In your app, wherever you would have used the text "Hello" before, use this formula instead:

  • MicrosoftTranslator.Translate( "Hello", Language() )MicrosoftTranslator.Translate( "Hello", Language() )

Microsoft Translator 服務會使用 Language 函式所傳回的相同語言標記。The Microsoft Translator service uses the same language tags that the Language function returns.

相較於上一個範例利用文字字串的預先轉譯資料表,這種方法有一些缺點︰This approach comes with some drawbacks when compared to the previous example which utilized a pre-translated table of text strings:

  • 轉譯需要時間才能完成,需要透過網路呼叫服務。The translation will take time to complete, requiring a call to a service across the network. 這會導致在您的應用程式中查看轉譯文字的延隔時間。This will result in a lag to see the translated text in your app.
  • 轉譯會很機械化,且可能不如您所預期,或應用程式內情況的最佳選擇。The translation will be mechanical and may not be what you anticipate or be the best choice for the situation within your app.