Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F112703
l10n-provider.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
l10n-provider.cpp
View Options
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include
"l10n-provider.hpp"
#include
<optional>
#include
<QFile>
#include
<QJsonDocument>
#include
<QJsonObject>
#include
<QStringBuilder>
#include
<stdexcept>
#include
"kazv-path-config.hpp"
static
QString
l10nConfigFile
()
{
static
QString
file
=
KazvPathConfigDetail
::
pathJoin
(
kazvL10nDir
(),
QString
(
"config.json"
));
return
file
;
}
static
QString
dirForLocale
(
QString
locale
)
{
return
KazvPathConfigDetail
::
pathJoin
(
kazvL10nDir
(),
locale
);
}
static
QString
readWholeFile
(
QString
path
)
{
QFile
f
(
path
);
if
(
!
f
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Text
))
{
throw
std
::
runtime_error
(
"Cannot open "
+
path
.
toStdString
()
+
" for reading."
);
}
QTextStream
stream
(
&
f
);
return
stream
.
readAll
();
}
struct
L10nProviderPrivate
{
std
::
optional
<
QJsonObject
>
cachedLocaleToNameMap
;
QMap
<
QString
,
QString
>
cachedFtlData
;
void
cacheAvailableLocales
();
void
cacheFtlDataFor
(
QString
locale
);
};
void
L10nProviderPrivate::cacheAvailableLocales
()
{
if
(
cachedLocaleToNameMap
.
has_value
())
{
return
;
}
else
{
auto
fn
=
l10nConfigFile
();
auto
content
=
readWholeFile
(
fn
);
auto
j
=
QJsonDocument
::
fromJson
(
content
.
toUtf8
());
auto
availableLocales
=
j
[
"availableLocales"
].
toObject
();
cachedLocaleToNameMap
=
std
::
move
(
availableLocales
);
}
}
void
L10nProviderPrivate::cacheFtlDataFor
(
QString
locale
)
{
if
(
cachedFtlData
.
contains
(
locale
))
{
return
;
}
auto
dn
=
dirForLocale
(
locale
);
auto
files
=
QDir
(
dn
).
entryInfoList
(
QStringList
{
QStringLiteral
(
"*.ftl"
)},
QDir
::
NoFilter
,
QDir
::
Name
);
QString
s
;
const
QString
newline
=
"
\n
"
;
for
(
auto
f
:
files
)
{
auto
fn
=
f
.
absoluteFilePath
();
s
.
append
(
readWholeFile
(
fn
));
s
.
append
(
newline
);
}
cachedFtlData
[
locale
]
=
s
;
}
L10nProvider
::
L10nProvider
(
QObject
*
parent
)
:
QObject
(
parent
)
,
m_d
(
new
L10nProviderPrivate
)
{
}
L10nProvider
::~
L10nProvider
()
=
default
;
QStringList
L10nProvider::availableLocaleCodes
()
{
m_d
->
cacheAvailableLocales
();
return
m_d
->
cachedLocaleToNameMap
.
value
().
keys
();
}
QJsonObject
L10nProvider::availableLocaleToNameMap
()
{
m_d
->
cacheAvailableLocales
();
return
m_d
->
cachedLocaleToNameMap
.
value
();
}
QJsonObject
L10nProvider::getFtlData
(
QStringList
desiredLocales
)
{
QJsonObject
data
;
for
(
auto
locale
:
desiredLocales
)
{
m_d
->
cacheFtlDataFor
(
locale
);
data
[
locale
]
=
m_d
->
cachedFtlData
[
locale
];
}
return
data
;
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Sat, Nov 23, 10:18 PM (21 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39163
Default Alt Text
l10n-provider.cpp (2 KB)
Attached To
Mode
rK kazv
Attached
Detach File
Event Timeline
Log In to Comment