2010-01-28 16:39:01 +01:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2010-01-28 16:39:01 +01:00
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2012-01-12 03:24:08 +01:00
|
|
|
* Copyright (c) 2010-2012, Tomasz Sowa
|
2010-01-28 16:39:01 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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"
|
2010-11-21 01:19:17 +01:00
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Locale
|
|
|
|
{
|
|
|
|
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>
|
|
|
|
bool IsKey(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
|
|
|
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>
|
|
|
|
bool IsKey(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
|
|
|
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>
|
|
|
|
const std::wstring & Get(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
|
|
|
bool try_default_too = true);
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
// lists
|
2012-06-28 01:21:43 +02:00
|
|
|
// 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;
|
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;
|
|
|
|
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;
|
2012-06-27 01:19:19 +02:00
|
|
|
|
|
|
|
|
2011-02-24 18:06:12 +01:00
|
|
|
// it sets whether we should parse locale files as utf-8 files
|
|
|
|
// default: false
|
2010-11-21 01:19:17 +01:00
|
|
|
void UTF8(bool utf);
|
|
|
|
|
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;
|
|
|
|
const std::vector<std::wstring> * GetListInLanguage(const std::wstring & key, size_t lang_id) const;
|
|
|
|
|
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>
|
2012-06-27 01:19:19 +02:00
|
|
|
std::vector<PT::Space> locale_tab;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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
|
|
|
|
|
|
|
|
2012-06-27 01:19:19 +02:00
|
|
|
PT::Space temp_space;
|
2012-05-01 00:53:54 +02:00
|
|
|
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)
|
2011-04-16 10:42:22 +02:00
|
|
|
const std::vector<std::wstring> empty_list; // the same as above
|
2010-11-21 01:19:17 +01:00
|
|
|
std::string adir1, adir2;
|
|
|
|
bool input_as_utf8;
|
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>
|
|
|
|
bool Locale::IsKey(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
|
|
|
bool try_default_too)
|
|
|
|
{
|
|
|
|
key.to_string(key_str);
|
|
|
|
return IsKey(key_str, try_default_too);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
bool Locale::IsKey(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
|
|
|
size_t lang_id, bool try_default_too)
|
|
|
|
{
|
|
|
|
key.to_string(key_str);
|
|
|
|
return IsKey(key_str, lang_id, try_default_too);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename char_type, size_t stack_size, size_t heap_block_size>
|
|
|
|
const std::wstring & Locale::Get(const PT::TextStreamBase<char_type, stack_size, heap_block_size> & key,
|
|
|
|
bool try_default_too)
|
|
|
|
{
|
|
|
|
key.to_string(key_str);
|
|
|
|
return Get(key_str, try_default_too);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-28 16:39:01 +01:00
|
|
|
#endif
|