| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#include "chttrans-opencc.h" |
| 8 |
#include "chttrans.h" |
| 9 |
#include <exception> |
| 10 |
#include <fcitx-utils/standardpaths.h> |
| 11 |
#include <fcitx-utils/stringutils.h> |
| 12 |
#include <opencc.h> |
| 13 |
#include <string> |
| 14 |
|
| 15 |
using namespace fcitx; |
| 16 |
|
| 17 |
bool OpenCCBackend::loadOnce(const ChttransConfig &config) { |
| 18 |
updateConfig(config); |
| 19 |
return true; |
| 20 |
} |
| 21 |
|
| 22 |
std::string OpenCCBackend::locateProfile(const std::string &profile) { |
| 23 |
auto profilePath = |
| 24 |
openCCStandardPath().locate(StandardPathsType::PkgData, profile); |
| 25 |
return profilePath.empty() ? profile : profilePath.string(); |
| 26 |
} |
| 27 |
|
| 28 |
void OpenCCBackend::updateConfig(const ChttransConfig &config) { |
| 29 |
auto s2tProfile = *config.openCCS2TProfile; |
| 30 |
if (s2tProfile.empty() || s2tProfile == "default") { |
| 31 |
const std::string preferredS2TProfile = "s2tw.json"; |
| 32 |
// Means it's resolved. |
| 33 |
if (locateProfile(preferredS2TProfile) != preferredS2TProfile) { |
| 34 |
s2tProfile = preferredS2TProfile; |
| 35 |
} else { |
| 36 |
s2tProfile = OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD; |
| 37 |
} |
| 38 |
} |
| 39 |
auto s2tProfilePath = locateProfile(s2tProfile); |
| 40 |
FCITX_DEBUG() << "s2tProfilePath: " << s2tProfilePath; |
| 41 |
|
| 42 |
try { |
| 43 |
auto s2t = std::make_unique<opencc::SimpleConverter>(s2tProfilePath); |
| 44 |
s2t_ = std::move(s2t); |
| 45 |
} catch (const std::exception &e) { |
| 46 |
FCITX_WARN() << "exception when loading s2t profile: " << e.what(); |
| 47 |
} |
| 48 |
|
| 49 |
auto t2sProfile = *config.openCCT2SProfile; |
| 50 |
if (t2sProfile.empty() || t2sProfile == "default") { |
| 51 |
const std::string preferredT2SProfile = "tw2s.json"; |
| 52 |
// Means it's resolved. |
| 53 |
if (locateProfile(preferredT2SProfile) != preferredT2SProfile) { |
| 54 |
t2sProfile = preferredT2SProfile; |
| 55 |
} else { |
| 56 |
t2sProfile = OPENCC_DEFAULT_CONFIG_TRAD_TO_SIMP; |
| 57 |
} |
| 58 |
} |
| 59 |
auto t2sProfilePath = locateProfile(t2sProfile); |
| 60 |
FCITX_DEBUG() << "t2sProfilePath: " << t2sProfilePath; |
| 61 |
|
| 62 |
try { |
| 63 |
auto t2s = std::make_unique<opencc::SimpleConverter>(t2sProfilePath); |
| 64 |
t2s_ = std::move(t2s); |
| 65 |
} catch (const std::exception &e) { |
| 66 |
FCITX_WARN() << "exception when loading t2s profile: " << e.what(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
std::string OpenCCBackend::convertSimpToTrad(const std::string &str) { |
| 71 |
if (s2t_) { |
| 72 |
try { |
| 73 |
return s2t_->Convert(str); |
| 74 |
} catch (const std::exception &e) { |
| 75 |
} |
| 76 |
} |
| 77 |
return str; |
| 78 |
} |
| 79 |
|
| 80 |
std::string OpenCCBackend::convertTradToSimp(const std::string &str) { |
| 81 |
if (t2s_) { |
| 82 |
try { |
| 83 |
return t2s_->Convert(str); |
| 84 |
} catch (const std::exception &e) { |
| 85 |
} |
| 86 |
} |
| 87 |
return str; |
| 88 |
} |