diff --git a/templates/locale.cpp b/templates/locale.cpp index a64685f..83580fa 100755 --- a/templates/locale.cpp +++ b/templates/locale.cpp @@ -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 & 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 & Locale::GetList(const wchar_t * key, bool try_default_too) +{ + key_str = key; + return GetList(key_str, try_default_too); +} + + +const std::vector & Locale::GetList(const std::wstring & key, bool try_default_too) const +{ + return GetList(key, current_lang, try_default_too); +} + + +const std::vector & 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 & Locale::GetList(const std::wstring & key, size_t lang_id) const +const std::vector & Locale::GetList(const std::wstring & key, + size_t lang_id, bool try_default_too) const { const std::vector * 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 & 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 & 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 * value = GetListInLanguage(key, default_lang); + + if( value ) + return *value; + } + +return empty_list; +} + + + diff --git a/templates/locale.h b/templates/locale.h index 21ac75c..569257c 100755 --- a/templates/locale.h +++ b/templates/locale.h @@ -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 & GetList(const std::wstring & key) const; - const std::vector & 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 & GetList(const wchar_t * key, bool try_default_too = true); + const std::vector & GetList(const std::wstring & key, bool try_default_too = true) const; + const std::vector & GetList(const wchar_t * key, size_t lang_id, bool try_default_too = true); + const std::vector & 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 & GetListByIndex(const wchar_t * key, size_t index, + bool try_default_too = true); + const std::vector & 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 diff --git a/templates/templates.cpp b/templates/templates.cpp index 5b96b68..9888bf2 100755 --- a/templates/templates.cpp +++ b/templates/templates.cpp @@ -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