2010-01-28 16:39:01 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2018-11-21 18:51:15 +01:00
|
|
|
* Copyright (c) 2010-2018, Tomasz Sowa
|
2010-01-28 16:39:01 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2010-01-28 16:39:01 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-23 15:15:30 +01:00
|
|
|
#ifndef headerfile_winix_templates_locale
|
|
|
|
#define headerfile_winix_templates_locale
|
2010-01-28 16:39:01 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2012-05-01 00:53:54 +02:00
|
|
|
#include "space/spaceparser.h"
|
2012-07-10 18:50:59 +02:00
|
|
|
#include "textstream/textstream.h"
|
2018-11-21 18:51:15 +01:00
|
|
|
#include "core/winixbase.h"
|
2019-01-07 04:32:17 +01:00
|
|
|
#include "convert/patternreplacer.h"
|
2018-11-21 18:51:15 +01:00
|
|
|
|
2010-11-21 01:19:17 +01:00
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
2010-01-28 16:39:01 +01:00
|
|
|
|
|
|
|
|
2018-11-21 18:51:15 +01:00
|
|
|
class Locale : public WinixBase
|
2010-01-28 16:39:01 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Locale();
|
|
|
|
|
2011-02-24 18:06:12 +01:00
|
|
|
// locale files
|
|
|
|
// those files will be reading from directories specified in Read() method
|
|
|
|
// default one item: en
|
|
|
|
void SetLocaleFiles(const std::vector<std::wstring> & files);
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
// preparing locale_indices table
|
|
|
|
// if you change this values you should reload all locale files
|
|
|
|
void SetLocaleMaxId(size_t max_id);
|
|
|
|
|
2012-06-28 01:21:43 +02:00
|
|
|
// 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;
|
2012-06-27 01:19:19 +02:00
|
|
|
|
2011-02-24 18:06:12 +01:00
|
|
|
// reading locales
|
|
|
|
// you should call SetLocaleFiles() beforehand
|
|
|
|
void Read(const char * dir, const char * dir_def = 0);
|
|
|
|
void Read(const std::string & dir, const std::string & dir_def);
|
|
|
|
void Read(const wchar_t * dir, const wchar_t * dir_def = 0);
|
|
|
|
void Read(const std::wstring & dir, const std::wstring & dir_def);
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
// returns true if a language with lang_id exists
|
|
|
|
bool HasLanguage(size_t lang_id);
|
|
|
|
|
2012-06-28 01:21:43 +02:00
|
|
|
// 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;
|
2011-02-24 18:06:12 +01:00
|
|
|
|
2012-07-10 18:50:59 +02:00
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
bool IsKey(const pt::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
2012-07-10 18:50:59 +02:00
|
|
|
bool try_default_too = true);
|
|
|
|
|
2012-06-28 01:21:43 +02:00
|
|
|
// 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;
|
2010-01-28 16:39:01 +01:00
|
|
|
|
2012-07-10 18:50:59 +02:00
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
bool IsKey(const pt::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
2012-07-10 18:50:59 +02:00
|
|
|
size_t lang_id, bool try_default_too = true);
|
|
|
|
|
2012-06-28 01:21:43 +02:00
|
|
|
// 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;
|
2011-04-16 10:42:22 +02:00
|
|
|
|
2012-07-10 18:50:59 +02:00
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
const std::wstring & Get(const pt::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
2012-07-10 18:50:59 +02:00
|
|
|
bool try_default_too = true);
|
|
|
|
|
2019-01-07 04:32:17 +01:00
|
|
|
template<typename ... Types>
|
|
|
|
const std::wstring & GetReplace(const wchar_t * key, Types ... types)
|
|
|
|
{
|
|
|
|
const std::wstring & pattern = Get(key, true);
|
|
|
|
pattern_replacer.ReplaceToString(pattern, pattern_value, types...);
|
|
|
|
return pattern_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename ... Types>
|
|
|
|
const std::wstring & GetReplaceForLang(const wchar_t * key, size_t lang_id, Types ... types)
|
|
|
|
{
|
|
|
|
const std::wstring & pattern = Get(key, lang_id, true);
|
|
|
|
pattern_replacer.ReplaceToString(pattern, pattern_value, types...);
|
|
|
|
return pattern_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ... Types>
|
|
|
|
const std::wstring & GetReplace(const std::wstring & key, Types ... types) const
|
|
|
|
{
|
|
|
|
const std::wstring & pattern = Get(key, true);
|
|
|
|
pattern_replacer.ReplaceToString(pattern, pattern_value, types...);
|
|
|
|
return pattern_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ... Types>
|
|
|
|
const std::wstring & GetReplaceForLang(const std::wstring & key, size_t lang_id, Types ... types) const
|
|
|
|
{
|
|
|
|
const std::wstring & pattern = Get(key, lang_id, true);
|
|
|
|
pattern_replacer.ReplaceToString(pattern, pattern_value, types...);
|
|
|
|
return pattern_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
// lists
|
2021-04-09 17:50:58 +02:00
|
|
|
bool GetList(const wchar_t * key, std::vector<std::wstring> & list, bool try_default_too = true);
|
|
|
|
bool GetList(const std::wstring & key, std::vector<std::wstring> & list, bool try_default_too = true) const;
|
|
|
|
bool GetList(const wchar_t * key, size_t lang_id, std::vector<std::wstring> & list, bool try_default_too = true);
|
|
|
|
bool GetList(const std::wstring & key, size_t lang_id, std::vector<std::wstring> & list, bool try_default_too = true) const;
|
2012-06-27 01:19:19 +02:00
|
|
|
|
|
|
|
// converting lang_id to an internal index
|
|
|
|
// returns an index from <0, Size()-1> or size_t(-1) if lang_id is incorrect
|
|
|
|
// it does it in O(1) time
|
|
|
|
size_t IdToIndex(size_t lang_id);
|
2010-01-28 16:39:01 +01:00
|
|
|
|
2011-02-24 18:06:12 +01:00
|
|
|
// returning how many locale files (languages) there are
|
2012-06-27 01:19:19 +02:00
|
|
|
// this is how many files were correctly parsed
|
|
|
|
// this is size of locale_tab table
|
2011-04-16 10:42:22 +02:00
|
|
|
size_t Size() const;
|
2011-02-24 18:06:12 +01:00
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
// accessing by an internal index
|
|
|
|
// internal index is from zero to Size()-1
|
2012-06-28 01:21:43 +02:00
|
|
|
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;
|
2021-04-09 17:50:58 +02:00
|
|
|
|
|
|
|
bool GetListByIndex(const wchar_t * key, size_t index, std::vector<std::wstring> & list, bool try_default_too = true);
|
|
|
|
bool GetListByIndex(const std::wstring & key, size_t index, std::vector<std::wstring> & list, bool try_default_too = true) const;
|
2012-06-27 01:19:19 +02:00
|
|
|
|
|
|
|
|
2011-08-30 00:23:54 +02:00
|
|
|
// url substitution characters
|
|
|
|
wchar_t UrlSubst(wchar_t c);
|
|
|
|
void UrlSubst(std::wstring & str);
|
|
|
|
|
|
|
|
// changing to small/capital letters
|
|
|
|
wchar_t ToSmall(wchar_t c);
|
|
|
|
void ToSmall(std::wstring & str);
|
|
|
|
wchar_t ToCapital(wchar_t c);
|
|
|
|
void ToCapital(std::wstring & str);
|
2010-01-28 16:39:01 +01:00
|
|
|
|
2012-01-21 21:51:13 +01:00
|
|
|
// comparing two characters/strings
|
|
|
|
// return a value less than zero if c1<c2, zero if c1==c2, value greater than 0 if c1>c2
|
|
|
|
int Compare(wchar_t c1, wchar_t c2);
|
|
|
|
int Compare(const std::wstring & str1, const std::wstring & str2);
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
|
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
private:
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
|
2011-08-30 00:23:54 +02:00
|
|
|
// struct to used for substitution
|
|
|
|
struct SubstItem
|
|
|
|
{
|
|
|
|
wchar_t from, to;
|
2012-01-21 21:51:13 +01:00
|
|
|
int index;
|
|
|
|
|
|
|
|
SubstItem() { from = to = 0; index = 0; }
|
2011-08-30 00:23:54 +02:00
|
|
|
bool operator<(const SubstItem & arg) const { return from < arg.from; }
|
|
|
|
};
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
void AddLocale(const char * file);
|
|
|
|
void ReadFile(const char * dir, const char * dir_def, const char * file);
|
|
|
|
bool ReadFile(const char * dir, const char * file);
|
2010-11-21 01:19:17 +01:00
|
|
|
void ReadSubstTable(const char * dir, const char * dir_def);
|
|
|
|
bool ReadSubstTable(const char * dir);
|
2011-08-30 00:23:54 +02:00
|
|
|
void CreateSubstVector(std::vector<SubstItem> & vect, const std::wstring & tab1, const std::wstring & tab2);
|
2012-01-21 21:51:13 +01:00
|
|
|
void CreateSubstSortVector(std::vector<SubstItem> & vect, std::vector<std::wstring> & tab);
|
|
|
|
size_t SubstFindIndex(const std::vector<SubstItem> & vect, wchar_t val);
|
2011-08-30 00:23:54 +02:00
|
|
|
wchar_t SubstFind(const std::vector<SubstItem> & vect, wchar_t val);
|
2012-06-27 01:19:19 +02:00
|
|
|
const std::wstring * GetKeyInLanguage(const std::wstring & key, size_t lang_id) const;
|
2021-04-09 17:50:58 +02:00
|
|
|
bool GetListInLanguage(const std::wstring & key, size_t lang_id, std::vector<std::wstring> & list) const;
|
|
|
|
bool IsListInLanguage(const std::wstring & key, size_t lang_id) const;
|
2012-06-27 01:19:19 +02:00
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
|
2011-02-24 18:06:12 +01:00
|
|
|
// locale files
|
|
|
|
std::vector<std::wstring> locale_files;
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
// a map from <0, config->locale_max_id> returning an index to
|
|
|
|
// locale_tab table (you should check whether the index is correct)
|
|
|
|
// with this we have O(1) time to find the specified locale
|
|
|
|
// and locale_tab doesn't have to be so large
|
|
|
|
std::vector<size_t> locale_indices;
|
2010-11-21 01:19:17 +01:00
|
|
|
|
2011-04-16 10:42:22 +02:00
|
|
|
// messages vector<via language>
|
2021-05-20 20:59:12 +02:00
|
|
|
std::vector<pt::Space> locale_tab;
|
2012-06-27 01:19:19 +02:00
|
|
|
|
|
|
|
// default locale index (index to locale_indexes)
|
|
|
|
size_t default_lang;
|
|
|
|
|
|
|
|
// current locale index (index to locale_indexes)
|
|
|
|
size_t current_lang;
|
2011-04-16 10:42:22 +02:00
|
|
|
|
2021-05-20 20:59:12 +02:00
|
|
|
pt::WPatternReplacer pattern_replacer;
|
2019-01-07 04:32:17 +01:00
|
|
|
|
|
|
|
|
2011-08-30 00:23:54 +02:00
|
|
|
// vectors of characters substitution (sort by 'from')
|
2012-06-27 01:19:19 +02:00
|
|
|
std::vector<SubstItem> subst_url; // for url substitution
|
2011-08-30 00:23:54 +02:00
|
|
|
std::vector<SubstItem> subst_smalllet; // changing from small to capital
|
|
|
|
std::vector<SubstItem> subst_capitallet; // changing from capital to small
|
2012-01-21 21:51:13 +01:00
|
|
|
std::vector<SubstItem> subst_sort; // local characters for comparison
|
2010-11-21 01:19:17 +01:00
|
|
|
|
|
|
|
|
2021-05-20 20:59:12 +02:00
|
|
|
pt::Space temp_space;
|
|
|
|
pt::SpaceParser loc_parser;
|
2011-02-24 18:06:12 +01:00
|
|
|
std::string locale_filea;
|
2010-01-28 16:39:01 +01:00
|
|
|
std::string file_name;
|
2011-02-24 18:06:12 +01:00
|
|
|
std::wstring key_str;
|
|
|
|
const std::wstring empty; // used when returning a non existing key from loc_tab (or in LangToFileName)
|
2010-11-21 01:19:17 +01:00
|
|
|
std::string adir1, adir2;
|
2019-01-07 04:32:17 +01:00
|
|
|
std::wstring pattern_value;
|
2010-01-28 16:39:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-07-10 18:50:59 +02:00
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
bool Locale::IsKey(const pt::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
2012-07-10 18:50:59 +02:00
|
|
|
bool try_default_too)
|
|
|
|
{
|
2022-04-25 22:05:34 +02:00
|
|
|
key.to_str(key_str);
|
2012-07-10 18:50:59 +02:00
|
|
|
return IsKey(key_str, try_default_too);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
bool Locale::IsKey(const pt::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
2012-07-10 18:50:59 +02:00
|
|
|
size_t lang_id, bool try_default_too)
|
|
|
|
{
|
2022-04-25 22:05:34 +02:00
|
|
|
key.to_str(key_str);
|
2012-07-10 18:50:59 +02:00
|
|
|
return IsKey(key_str, lang_id, try_default_too);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
2021-05-20 20:59:12 +02:00
|
|
|
const std::wstring & Locale::Get(const pt::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
2012-07-10 18:50:59 +02:00
|
|
|
bool try_default_too)
|
|
|
|
{
|
2022-04-25 22:05:34 +02:00
|
|
|
key.to_str(key_str);
|
2012-07-10 18:50:59 +02:00
|
|
|
return Get(key_str, try_default_too);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
} // namespace Winix
|
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
#endif
|