名稱空間
變體
操作

std::messages<CharT>::open, std::messages<CharT>::do_open

來自 cppreference.com
< cpp‎ | 本地化‎ | messages
 
 
 
 
 
定義於標頭檔案 <locale>
public:
catalog open( const std::string& name, const std::locale& loc ) const;
(1)
protected:
virtual catalog do_open( const std::string& name, const std::locale& loc ) const;
(2)
1) 公有成員函式,呼叫最派生類的保護虛成員函式 do_open
2) 獲取型別為 catalog 的值(繼承自 std::messages_base),該值可傳遞給 get() 以從由 name 命名的訊息目錄中檢索訊息。該值在傳遞給 close() 之前可用。

目錄

[編輯] 引數

name - 要開啟的訊息目錄的名稱
loc - 提供從目錄中讀取訊息可能需要的額外面(facet)的區域設定物件,例如 std::codecvt 以執行寬字元/多位元組轉換

[編輯] 返回值

型別為 catalog 的非負值,可用於 get()close()。如果無法開啟目錄,則返回負值。

[編輯] 注意

在 POSIX 系統上,此函式呼叫通常轉換為對 catopen() 的呼叫。在 GNU libstdc++ 中,它呼叫 textdomain

實際的目錄位置是實現定義的:例如,在德語區域設定中,對於目錄 "sed"(隨 Unix 實用程式 'sed' 安裝的訊息目錄),此函式呼叫開啟的檔案可能是 /usr/lib/nls/msg/de_DE/sed.cat/usr/lib/locale/de_DE/LC_MESSAGES/sed.cat/usr/share/locale/de/LC_MESSAGES/sed.mo

[編輯] 示例

以下示例演示了訊息檢索:在典型的 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

[編輯] 參閱