rewritten: public interface in TemplatesFunctions::Locale

added a default parameter bool try_default_too = true to some methods: Get(), IsKey()
           added more methods for accessing by an internal index



git-svn-id: svn://ttmath.org/publicrep/winix/trunk@854 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2012-06-27 23:21:43 +00:00
parent b8ff5d4cfc
commit 403cca5aad
4 changed files with 221 additions and 96 deletions

View File

@ -308,27 +308,27 @@ size_t Locale::GetDefLang() const
}
bool Locale::IsKey(const wchar_t * key)
bool Locale::IsKey(const wchar_t * key, bool try_default_too)
{
key_str = key;
return IsKey(key_str);
return IsKey(key_str, try_default_too);
}
bool Locale::IsKey(const wchar_t * key, size_t lang_id)
bool Locale::IsKey(const std::wstring & key, bool try_default_too) const
{
return IsKey(key, current_lang, try_default_too);
}
bool Locale::IsKey(const wchar_t * key, size_t lang_id, bool try_default_too)
{
key_str = key;
return IsKey(key_str, lang_id);
return IsKey(key_str, lang_id, try_default_too);
}
bool Locale::IsKey(const std::wstring & key) const
{
return IsKey(key, current_lang);
}
const std::wstring * Locale::GetKeyInLanguage(const std::wstring & key, size_t lang_id) const
{
if( lang_id < locale_indices.size() )
@ -343,63 +343,50 @@ return 0;
}
bool Locale::IsKey(const std::wstring & key, size_t lang_id) const
bool Locale::IsKey(const std::wstring & key, size_t lang_id, bool try_default_too) const
{
const std::wstring * value = GetKeyInLanguage(key, lang_id);
if( value )
if( GetKeyInLanguage(key, lang_id) != 0 )
return true;
if( lang_id == default_lang )
if( !try_default_too || lang_id == default_lang )
return false;
return GetKeyInLanguage(key, default_lang) != 0;
}
bool Locale::IsKeyLang(const wchar_t * key, size_t lang)
const std::wstring & Locale::Get(const wchar_t * key, bool try_default_too)
{
key_str = key;
return IsKeyLang(key_str, lang);
return Get(key_str, try_default_too);
}
bool Locale::IsKeyLang(const std::wstring & key, size_t lang_id) const
const std::wstring & Locale::Get(const std::wstring & key, bool try_default_too) const
{
return GetKeyInLanguage(key, lang_id) != 0;
return Get(key, current_lang, try_default_too);
}
const std::wstring & Locale::Get(const wchar_t * key)
const std::wstring & Locale::Get(const wchar_t * key, size_t lang_id, bool try_default_too)
{
key_str = key;
return Get(key_str);
}
const std::wstring & Locale::Get(const wchar_t * key, size_t lang_id)
{
key_str = key;
return Get(key_str, lang_id);
return Get(key_str, lang_id, try_default_too);
}
const std::wstring & Locale::Get(const std::wstring & key) const
{
return Get(key, current_lang);
}
const std::wstring & Locale::Get(const std::wstring & key, size_t lang_id) const
const std::wstring & Locale::Get(const std::wstring & key, size_t lang_id, bool try_default_too) const
{
const std::wstring * value = GetKeyInLanguage(key, lang_id);
if( value )
return *value;
if( lang_id == default_lang )
if( !try_default_too || lang_id == default_lang )
return empty;
value = GetKeyInLanguage(key, default_lang);
@ -412,22 +399,60 @@ return empty;
bool Locale::IsKeyLangList(const wchar_t * key, size_t lang_id)
bool Locale::IsList(const wchar_t * key, bool try_default_too)
{
key_str = key;
return IsKeyLangList(key_str, lang_id);
return IsList(key_str, try_default_too);
}
bool Locale::IsKeyLangList(const std::wstring & key, size_t lang_id) const
bool Locale::IsList(const std::wstring & key, bool try_default_too) const
{
return GetListInLanguage(key, lang_id) != 0;
return IsList(key, current_lang, try_default_too);
}
const std::vector<std::wstring> & Locale::GetList(const std::wstring & key) const
bool Locale::IsList(const wchar_t * key, size_t lang_id, bool try_default_too)
{
return GetList(key, current_lang);
key_str = key;
return IsList(key_str, lang_id, try_default_too);
}
bool Locale::IsList(const std::wstring & key, size_t lang_id, bool try_default_too) const
{
if( GetListInLanguage(key, lang_id) != 0 )
return true;
if( !try_default_too || lang_id == default_lang )
return false;
return GetListInLanguage(key, default_lang) != 0;
}
const std::vector<std::wstring> & Locale::GetList(const wchar_t * key, bool try_default_too)
{
key_str = key;
return GetList(key_str, try_default_too);
}
const std::vector<std::wstring> & Locale::GetList(const std::wstring & key, bool try_default_too) const
{
return GetList(key, current_lang, try_default_too);
}
const std::vector<std::wstring> & Locale::GetList(const wchar_t * key, size_t lang_id, bool try_default_too)
{
key_str = key;
return GetList(key_str, lang_id, try_default_too);
}
@ -451,14 +476,15 @@ return 0;
}
const std::vector<std::wstring> & Locale::GetList(const std::wstring & key, size_t lang_id) const
const std::vector<std::wstring> & Locale::GetList(const std::wstring & key,
size_t lang_id, bool try_default_too) const
{
const std::vector<std::wstring> * list = GetListInLanguage(key, lang_id);
if( list )
return *list;
if( lang_id == default_lang )
if( !try_default_too || lang_id == default_lang )
return empty_list;
list = GetListInLanguage(key, default_lang);
@ -490,14 +516,44 @@ size_t Locale::Size() const
}
const std::wstring & Locale::GetByIndex(const wchar_t * key, size_t index)
bool Locale::IsKeyByIndex(const wchar_t * key, size_t index, bool try_default_too)
{
key_str = key;
return GetByIndex(key_str, index);
return IsKeyByIndex(key_str, index, try_default_too);
}
const std::wstring & Locale::GetByIndex(const std::wstring & key, size_t index) const
bool Locale::IsKeyByIndex(const std::wstring & key, size_t index, bool try_default_too) const
{
if( index < locale_tab.size() )
{
if( locale_tab[index].GetValue(key) != 0 )
return true;
}
if( try_default_too )
{
if( GetKeyInLanguage(key, default_lang) != 0 )
return true;
}
return false;
}
const std::wstring & Locale::GetByIndex(const wchar_t * key, size_t index, bool try_default_too)
{
key_str = key;
return GetByIndex(key_str, index, try_default_too);
}
const std::wstring & Locale::GetByIndex(const std::wstring & key, size_t index,
bool try_default_too) const
{
if( index < locale_tab.size() )
{
@ -507,12 +563,76 @@ const std::wstring & Locale::GetByIndex(const std::wstring & key, size_t index)
return *value;
}
if( try_default_too )
{
const std::wstring * value = GetKeyInLanguage(key, default_lang);
if( value )
return *value;
}
return empty;
}
bool Locale::IsListByIndex(const wchar_t * key, size_t index, bool try_default_too)
{
key_str = key;
return IsListByIndex(key_str, index, try_default_too);
}
bool Locale::IsListByIndex(const std::wstring & key, size_t index, bool try_default_too) const
{
if( index < locale_tab.size() )
{
PT::Space::Table::const_iterator i = locale_tab[index].table.find(key);
if( i != locale_tab[index].table.end() )
return true;
}
if( try_default_too )
return GetListInLanguage(key, default_lang) != 0;
return false;
}
const std::vector<std::wstring> & Locale::GetListByIndex(const wchar_t * key,
size_t index, bool try_default_too)
{
key_str = key;
return GetListByIndex(key_str, index, try_default_too);
}
const std::vector<std::wstring> & Locale::GetListByIndex(const std::wstring & key,
size_t index, bool try_default_too) const
{
if( index < locale_tab.size() )
{
PT::Space::Table::const_iterator i = locale_tab[index].table.find(key);
if( i != locale_tab[index].table.end() )
return i->second;
}
if( try_default_too )
{
const std::vector<std::wstring> * value = GetListInLanguage(key, default_lang);
if( value )
return *value;
}
return empty_list;
}

View File

@ -31,6 +31,15 @@ public:
// if you change this values you should reload all locale files
void SetLocaleMaxId(size_t max_id);
// setting/getting current language
// default: 0
void SetCurLang(size_t lang_id);
size_t GetCurLang() const;
// which language is used instead of if there is no a key in an other language
// default: 0
void SetDefLang(size_t lang_id);
size_t GetDefLang() const;
// reading locales
// you should call SetLocaleFiles() beforehand
@ -42,46 +51,35 @@ public:
// returns true if a language with lang_id exists
bool HasLanguage(size_t lang_id);
// checking whether there is a 'key' in the current language (or in 'lang' language)
bool IsKey(const wchar_t * key);
bool IsKey(const wchar_t * key, size_t lang_id);
bool IsKey(const std::wstring & key) const;
bool IsKey(const std::wstring & key, size_t lang_id) const;
// checking whether there is a 'key' in the current language
// or in the default language if try_default_too is true
bool IsKey(const wchar_t * key, bool try_default_too = true);
bool IsKey(const std::wstring & key, bool try_default_too = true) const;
// checking whether there is a 'key' in the lang language
// (default language is not checked)
bool IsKeyLang(const wchar_t * key, size_t lang_id);
bool IsKeyLang(const std::wstring & key, size_t lang_id) const;
// returning specific 'key'
const std::wstring & Get(const wchar_t * key);
const std::wstring & Get(const wchar_t * key, size_t lang_id);
const std::wstring & Get(const std::wstring & key) const;
const std::wstring & Get(const std::wstring & key, size_t lang_id) const;
// checking whether there is a 'key' in the lang_id language
// or in the default language if try_default_too is true
bool IsKey(const wchar_t * key, size_t lang_id, bool try_default_too = true);
bool IsKey(const std::wstring & key, size_t lang_id, bool try_default_too = true) const;
// returning a value for a specific key
const std::wstring & Get(const wchar_t * key, bool try_default_too = true);
const std::wstring & Get(const wchar_t * key, size_t lang_id, bool try_default_too = true);
const std::wstring & Get(const std::wstring & key, bool try_default_too = true) const;
const std::wstring & Get(const std::wstring & key, size_t lang_id, bool try_default_too = true) const;
// lists
bool IsKeyLangList(const wchar_t * key, size_t lang_id);
bool IsKeyLangList(const std::wstring & key, size_t lang_id) const;
const std::vector<std::wstring> & GetList(const std::wstring & key) const;
const std::vector<std::wstring> & GetList(const std::wstring & key, size_t lang_id) const;
// setting/getting current language
// default: 0
void SetCurLang(size_t lang_id);
size_t GetCurLang() const;
// which language is used instead of if there is no a key in an other language
// default: 0
void SetDefLang(size_t lang_id);
size_t GetDefLang() const;
// current limitation:
// we are looking only in 'space.table' so lists with only one item are not found
// (SplitSingle(true) was called to the space struct)
bool IsList(const wchar_t * key, bool try_default_too = true);
bool IsList(const std::wstring & key, bool try_default_too = true) const;
bool IsList(const wchar_t * key, size_t lang_id, bool try_default_too = true);
bool IsList(const std::wstring & key, size_t lang_id, bool try_default_too = true) const;
const std::vector<std::wstring> & GetList(const wchar_t * key, bool try_default_too = true);
const std::vector<std::wstring> & GetList(const std::wstring & key, bool try_default_too = true) const;
const std::vector<std::wstring> & GetList(const wchar_t * key, size_t lang_id, bool try_default_too = true);
const std::vector<std::wstring> & GetList(const std::wstring & key, size_t lang_id, bool try_default_too = true) const;
// converting lang_id to an internal index
// returns an index from <0, Size()-1> or size_t(-1) if lang_id is incorrect
@ -95,9 +93,21 @@ public:
// accessing by an internal index
// internal index is from zero to Size()-1
const std::wstring & GetByIndex(const wchar_t * key, size_t index);
const std::wstring & GetByIndex(const std::wstring & key, size_t index) const;
bool IsKeyByIndex(const wchar_t * key, size_t index, bool try_default_too = true);
bool IsKeyByIndex(const std::wstring & key, size_t index, bool try_default_too = true) const;
const std::wstring & GetByIndex(const wchar_t * key, size_t index, bool try_default_too = true);
const std::wstring & GetByIndex(const std::wstring & key, size_t index, bool try_default_too = true) const;
// lists by an internal index
// current limitation:
// we are looking only in 'space.table' so lists with only one item are not found
// (SplitSingle(true) was called to the space struct)
bool IsListByIndex(const wchar_t * key, size_t index, bool try_default_too = true);
bool IsListByIndex(const std::wstring & key, size_t index, bool try_default_too = true) const;
const std::vector<std::wstring> & GetListByIndex(const wchar_t * key, size_t index,
bool try_default_too = true);
const std::vector<std::wstring> & GetListByIndex(const std::wstring & key, size_t index,
bool try_default_too = true) const;
// it sets whether we should parse locale files as utf-8 files

View File

@ -686,8 +686,6 @@ using namespace TemplatesFunctions;
// !! IMPROVE ME
// we need such a html filter for each language (orphans are different in each language)
void Templates::SetHtmlFilter()
{
using namespace TemplatesFunctions;
@ -704,14 +702,11 @@ using namespace TemplatesFunctions;
for(size_t i=0 ; i<locale.Size() ; ++i)
{
if( locale.IsKeyLang(L"html_lang_attr_value", i) &&
locale.IsKeyLangList(L"language_orphans", i) )
if( locale.IsKeyByIndex(L"html_lang_attr_value", i, false) &&
locale.IsListByIndex(L"language_orphans", i, false) )
{
// !! FIX ME
// this is not the proper index
// we should use locale.GetByIndex() instead
html_filter.AssignOrphans(locale.Get(L"html_lang_attr_value", i),
locale.GetList(L"language_orphans", i));
html_filter.AssignOrphans(locale.GetByIndex(L"html_lang_attr_value", i, false),
locale.GetListByIndex(L"language_orphans", i, false));
}
}
}

View File

@ -397,14 +397,14 @@ void winix_locale_tab(Info & i)
void winix_locale_tab_id(Info & i)
{
if( locale_id < locale.Size() )
i.out << Toi(locale.GetByIndex(L"winix_locale_id", locale_id));
i.out << Toi(locale.GetByIndex(L"winix_locale_id", locale_id, false));
}
void winix_locale_tab_name(Info & i)
{
if( locale_id < locale.Size() )
i.out << locale.GetByIndex(L"locale_name", locale_id);
i.out << locale.GetByIndex(L"locale_name", locale_id, false);
}