std::messages<CharT>::get, std::messages<CharT>::do_get
來自 cppreference.com
定義於標頭檔案 <locale> |
||
public: string_type get( catalog cat, int set, int msgid, const string_type& dfault ) const; |
(1) | |
protected: virtual string_type do_get( catalog cat, int set, int msgid, const string_type& dfault ) const; |
(2) | |
1) 公有成員函式,呼叫最派生類的受保護虛成員函式
do_get
。2) 從開放的訊息目錄 cat 中,以實現定義的方式,使用值 set、msgid 和 dfault 獲取訊息。如果在目錄中未找到預期的訊息,則返回 dfault 的副本。
目錄 |
[編輯] 引數
cat | - | 從 open() 獲取且尚未傳遞給 close() 的訊息目錄識別符號 |
set | - | 實現定義的引數,POSIX 中的訊息集 |
msgid | - | 實現定義的引數,POSIX 中的訊息 ID |
dfault | - | 要在目錄中查詢的字串(如果目錄使用字串查詢),也是在失敗時返回的字串 |
[編輯] 返回值
來自目錄的訊息,如果未找到則返回 dfault 的副本。
[編輯] 注意
在 POSIX 系統上,此函式呼叫通常轉換為對 catgets()
的呼叫,並且引數 set、msgid 和 dfault 按原樣傳遞給 catgets()
。在 GNU libstdc++ 中,此函式忽略 set 和 msgid,只需在所需的語言環境中呼叫 GNU gettext(dfault)。
[編輯] 示例
以下示例演示了訊息檢索:在典型的 GNU/Linux 系統上,它從 /usr/share/locale/de/LC_MESSAGES/sed.mo
讀取。
執行此程式碼
#include <iostream> #include <locale> int main() { std::locale loc("de_DE.utf8"); std::cout.imbue(loc); auto& facet = std::use_facet<std::messages<char>>(loc); auto cat = facet.open("sed", loc); if (cat < 0) std::cout << "Could not open german \"sed\" message catalog\n"; else std::cout << "\"No match\" in German: " << facet.get(cat, 0, 0, "No match") << '\n' << "\"Memory exhausted\" in German: " << facet.get(cat, 0, 0, "Memory exhausted") << '\n'; facet.close(cat); }
可能的輸出
"No match" in German: Keine Übereinstimmung "Memory exhausted" in German: Speicher erschöpft