allow to use multiple locale directories

remove config options:
- locale_dir
- locale_dir_default

add a config option:
- locale_dirs - a list of directories with locale files (translations)

We start reading locale from the first directory and files with the same name
in each directory are concatenated.
This commit is contained in:
2026-06-21 23:36:29 +02:00
parent d35854691c
commit b82a36142c
5 changed files with 101 additions and 107 deletions
+71 -81
View File
@@ -4,8 +4,8 @@
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2010-2021, Tomasz Sowa
/*
* Copyright (c) 2010-2026, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -49,13 +49,18 @@ Locale::Locale()
}
void Locale::SetDirectories(const std::vector<std::wstring> & dirs)
{
directories = dirs;
}
void Locale::SetLocaleFiles(const std::vector<std::wstring> & files)
{
locale_files = files;
}
void Locale::SetLocaleMaxId(size_t max_id)
{
if( max_id > 1000 )
@@ -72,8 +77,6 @@ void Locale::SetLocaleMaxId(size_t max_id)
}
bool Locale::HasLanguage(size_t lang_id)
{
if( lang_id < locale_indices.size() )
@@ -83,34 +86,38 @@ return false;
}
void Locale::ReadFile(const char * dir, const char * dir_def, const char * file)
void Locale::ReadFile(const std::wstring & file)
{
bool read = false;
temp_space.clear();
if( dir_def && ReadFile(dir_def, file) )
read = true;
if( dir && ReadFile(dir, file) )
read = true;
for(const std::wstring & dir : directories)
{
if( ReadFile(dir, file) )
read = true;
}
if( read )
{
AddLocale(file);
}
else
{
log << log1 << "Locale: can't open locale's file: " << file << logend;
}
temp_space.clear();
}
bool Locale::ReadFile(const char * dir, const char * file)
bool Locale::ReadFile(const std::wstring & dir, const std::wstring & file)
{
bool read = false;
bool read = false;
file_name = dir;
pt::wide_to_utf8(dir, file_name);
file_name += '/';
file_name += file;
pt::wide_to_utf8(file, file_name, false);
pt::SpaceParser::Status status = loc_parser.parse_space_file(file_name, temp_space, false);
@@ -125,23 +132,24 @@ bool read = false;
log << log1 << "Locale: syntax error in: " << file_name << " in line: " << loc_parser.get_last_parsed_line() << logend;
}
return read;
file_name.clear();
return read;
}
void Locale::AddLocale(const char * file)
void Locale::AddLocale(const std::wstring & file)
{
std::wstring * id_str = temp_space.get_wstr(L"winix_locale_id");
if( !id_str )
{
log << log1 << "Locale: winix_locale_id field should be defined in locale file: "
<< file << " (skipping)" << logend;
log << log1 << "Locale: winix_locale_id field should be defined in a locale file: "
<< file << " (skipping this file)" << logend;
return;
}
size_t id = (size_t)Tol(*id_str);
size_t id = (size_t)pt::to_ul(*id_str);
if( id >= locale_indices.size() )
{
@@ -155,32 +163,35 @@ void Locale::AddLocale(const char * file)
void Locale::ReadSubstTable(const char * dir, const char * dir_def)
void Locale::ReadSubstTable()
{
bool read = false;
bool read = false;
temp_space.clear();
subst_url.clear();
subst_smalllet.clear();
subst_capitallet.clear();
subst_sort.clear();
if( dir_def && ReadSubstTable(dir_def) )
read = true;
if( dir && ReadSubstTable(dir) )
read = true;
for(const std::wstring & dir : directories)
{
if( ReadSubstTable(dir) )
read = true;
}
if( !read )
log << log1 << "Locale: can't open file for characters substitution" << logend;
{
log << log1 << "Locale: can't open a file for characters substitution" << logend;
}
temp_space.clear();
}
bool Locale::ReadSubstTable(const char * dir)
bool Locale::ReadSubstTable(const std::wstring & dir)
{
bool read = false;
bool read = false;
file_name = dir;
pt::wide_to_utf8(dir, file_name);
file_name += '/';
file_name += "substitute";
@@ -199,7 +210,8 @@ bool read = false;
log << log3 << "Locale: read characters substitution tables from: " << file_name << logend;
}
return read;
file_name.clear();
return read;
}
@@ -254,58 +266,36 @@ void Locale::CreateSubstSortVector(std::vector<SubstItem> & vect, std::vector<st
}
void Locale::Read(const char * dir, const char * dir_def)
void Locale::ReadLocales()
{
for(size_t & index : locale_indices)
index = size_t(-1);
for(const std::wstring & file : locale_files)
{
ReadFile(file);
}
ReadSubstTable();
}
void Locale::Clear()
{
directories.clear();
locale_files.clear();
locale_indices.clear();
locale_tab.clear();
default_lang = 0;
current_lang = 0;
for(size_t i=0 ; i<locale_indices.size() ; ++i)
locale_indices[i] = size_t(-1);
for(size_t i=0 ; i<locale_files.size() ; ++i)
{
pt::wide_to_utf8(locale_files[i], locale_filea);
ReadFile(dir, dir_def, locale_filea.c_str());
}
ReadSubstTable(dir, dir_def);
subst_url.clear();
subst_smalllet.clear();
subst_capitallet.clear();
subst_sort.clear();
}
void Locale::Read(const std::string & dir, const std::string & dir_def)
{
if( dir_def.empty() )
Read(dir.c_str());
else
Read(dir.c_str(), dir_def.c_str());
}
void Locale::Read(const wchar_t * dir, const wchar_t * dir_def)
{
pt::wide_to_utf8(dir, adir1);
if( !dir_def )
{
Read(adir1.c_str());
}
else
{
pt::wide_to_utf8(dir_def, adir2);
Read(adir1.c_str(), adir2.c_str());
}
}
void Locale::Read(const std::wstring & dir, const std::wstring & dir_def)
{
if( dir_def.empty() )
Read(dir.c_str());
else
Read(dir.c_str(), dir_def.c_str());
}
void Locale::SetCurLang(size_t lang_id)
{