Skip to content

Commit 1f24347

Browse files
committed
C++ i18n
1 parent 11571a6 commit 1f24347

File tree

14 files changed

+201
-74
lines changed

14 files changed

+201
-74
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
needs: [lint, data]
4545
runs-on: macos-15
4646
env:
47-
MOZC_SHA: c4a8c95865cb60a14e6c829a0a3893c3b3dac64c
47+
MOZC_SHA: 88b1dcae03a0a16192586dca3da157dad64f0e2f
4848
strategy:
4949
fail-fast: false
5050
matrix:

CMakeLists.txt

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,37 @@ option(HALLELUJAH "" ON)
7676
option(MOZC "" ON)
7777
option(RIME "" ON)
7878

79-
function(copy_to_keyboard keyboard cmd file relative_path)
80-
add_custom_command(TARGET "${keyboard}Extension"
79+
function (set_app_and_target bundle)
80+
if ("${bundle}" STREQUAL "app")
81+
set(app "${BUNDLE_NAME}.app" PARENT_SCOPE)
82+
set(target "${BUNDLE_NAME}" PARENT_SCOPE)
83+
else()
84+
set(app "${bundle}.appex" PARENT_SCOPE)
85+
set(target "${bundle}Extension" PARENT_SCOPE)
86+
endif()
87+
endfunction()
88+
89+
function(copy_to bundle cmd file relative_path)
90+
set_app_and_target(${bundle})
91+
add_custom_command(TARGET ${target}
8192
POST_BUILD COMMAND /bin/sh -c \"
8293
${CMAKE_COMMAND} -E ${cmd} ${file}
83-
${PROJECT_BINARY_DIR}/src/$<CONFIG>-${SDK_NAME}/${keyboard}.appex/${relative_path}
94+
${PROJECT_BINARY_DIR}/src/$<CONFIG>-${SDK_NAME}/${app}/${relative_path}
8495
\")
8596
endfunction()
8697

87-
function(copy_share_to_keyboard keyboard cmd relative_path)
88-
copy_to_keyboard(${keyboard} ${cmd} "${PREBUILDER_SHARE_DIR}/${relative_path}" "share/${relative_path}")
98+
function(copy_share_to bundle cmd relative_path)
99+
copy_to(${bundle} ${cmd} "${PREBUILDER_SHARE_DIR}/${relative_path}" "share/${relative_path}")
89100
endfunction()
90101

91-
function(copy_to_app cmd file relative_path)
92-
add_custom_command(TARGET ${BUNDLE_NAME}
93-
POST_BUILD COMMAND /bin/sh -c \"
94-
${CMAKE_COMMAND} -E ${cmd} ${file}
95-
${PROJECT_BINARY_DIR}/src/$<CONFIG>-${SDK_NAME}/${BUNDLE_NAME}.app/${relative_path}
96-
\")
102+
function(install_cxx_translations bundle po)
103+
set_app_and_target(${bundle})
104+
add_custom_command(TARGET ${target} POST_BUILD
105+
COMMAND ./scripts/install.sh "${app}" "${PROJECT_BINARY_DIR}/${po}"
106+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
107+
COMMENT "Installing C++ translations for ${bundle}"
108+
)
109+
copy_to(${bundle} copy_directory "${PROJECT_SOURCE_DIR}/build/po/${app}/usr/local/share/locale" "share/locale")
97110
endfunction()
98111

99112
set(Fcitx5Utils_DIR "${PROJECT_SOURCE_DIR}/cmake")

cmake/Fcitx5UtilsConfig.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ set(FCITX_INSTALL_CMAKECONFIG_DIR "${CMAKE_CURRENT_LIST_DIR}")
77

88
# mimic fcitx5/src/lib/fcitx-utils/Fcitx5UtilsConfig.cmake.in
99
include("${CMAKE_CURRENT_LIST_DIR}/Fcitx5Utils/Fcitx5Macros.cmake")
10+
11+
# Unify addons with fcitx5.
12+
set(FCITX_INSTALL_LOCALEDIR "/usr/local/share/locale")

common/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ target_include_directories(FcitxCommon PRIVATE
77
)
88
target_link_libraries(FcitxCommon Fcitx5::Core nlohmann_json)
99

10-
add_library(SwiftUtil util.swift)
10+
add_library(SwiftUtil util.swift locale.swift)
1111
set_target_properties(SwiftUtil PROPERTIES Swift_MODULE_NAME SwiftUtil)

common/common-public.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#include <string>
22

33
std::string getInputMethods();
4+
void setLocale(const char *locale);

common/common.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "common.h"
22
#include "../fcitx5/src/lib/fcitx/addonmanager.h"
33
#include "nativestreambuf.h"
4+
#include <fcitx-utils/i18n.h>
45
#include <filesystem>
5-
66
#include <thread>
77

88
namespace fs = std::filesystem;
@@ -43,10 +43,48 @@ void setupEnv(const char *bundlePath, const char *appGroupPath,
4343
setenv("FCITX_CONFIG_HOME", fcitx_config_home.c_str(), 1);
4444
}
4545

46+
// Collect all share/locale/*/LC_MESSAGES/*.mo
47+
std::set<std::string> getAllDomains(const fs::path &localedir) {
48+
std::set<std::string> ret;
49+
try {
50+
for (const auto &entry : fs::directory_iterator(localedir)) {
51+
if (!entry.is_directory()) {
52+
continue;
53+
}
54+
fs::path lcMessagesPath = entry.path() / "LC_MESSAGES";
55+
try {
56+
for (const auto &file :
57+
fs::directory_iterator(lcMessagesPath)) {
58+
if (file.path().extension() == ".mo") {
59+
ret.insert(file.path().stem());
60+
}
61+
}
62+
} catch (const std::exception &e) {
63+
// LC_MESSAGES not exist.
64+
}
65+
}
66+
} catch (const std::exception &e) {
67+
// localedir not exist.
68+
}
69+
return ret;
70+
}
71+
72+
// Must be executed before creating fcitx instance, i.e. loading addons, because
73+
// addons register compile-time domain path, and only 1st call of registerDomain
74+
// counts. The .mo files must exist.
75+
void setupI18N(const char *bundlePath) {
76+
fs::path bundle = bundlePath;
77+
fs::path localedir = bundle / "share" / "locale";
78+
for (const auto &domain : getAllDomains(localedir)) {
79+
fcitx::registerDomain(domain.c_str(), localedir);
80+
}
81+
}
82+
4683
void setupFcitx(const char *bundlePath, const char *appGroupPath,
4784
bool isMainApp) {
4885
setupLog();
4986
setupEnv(bundlePath, appGroupPath, isMainApp);
87+
setupI18N(bundlePath);
5088

5189
instance = std::make_unique<fcitx::Instance>(0, nullptr);
5290
instance->setInputMethodMode(fcitx::InputMethodMode::OnScreenKeyboard);
@@ -59,3 +97,12 @@ void setupFcitx(const char *bundlePath, const char *appGroupPath,
5997
dispatcher->attach(&instance->eventLoop());
6098
fcitx_thread = std::thread([] { instance->eventLoop().exec(); });
6199
}
100+
101+
void setLocale(const char *locale) {
102+
std::string val = locale;
103+
val += ":C";
104+
// For config items.
105+
setenv("LANGUAGE", val.c_str(), 1);
106+
// For addon names.
107+
setenv("FCITX_LOCALE", val.c_str(), 1);
108+
}

common/locale.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// See fcitx5-macos/src/locale.swift
2+
3+
import Foundation
4+
import SwiftUtil
5+
6+
// Return app locale or system locale if called from app.
7+
// Return system locale if called from keyboard.
8+
public func getLocale() -> String {
9+
let locale = Locale.current
10+
logger.info("System locale = \(locale.identifier)")
11+
12+
if let languageCode = locale.language.languageCode?.identifier {
13+
if languageCode == "zh" {
14+
if let scriptCode = locale.language.script?.identifier {
15+
if scriptCode == "Hans" {
16+
return "zh_CN"
17+
} else {
18+
return "zh_TW"
19+
}
20+
}
21+
if locale.region?.identifier == "SG" {
22+
return "zh_CN"
23+
} else {
24+
return "zh_TW"
25+
}
26+
}
27+
return languageCode
28+
}
29+
return "C"
30+
}

engines/fcitx5-chinese-addons

engines/fcitx5-mozc

keyboard/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ function(add_keyboard_extension keyboard language targets)
5252
ADDONS ${COMMON_TARGETS} ${targets}
5353
)
5454

55-
copy_to_keyboard(${keyboard} copy "${PROJECT_BINARY_DIR}/iosfrontend/iosfrontend.conf" "${ADDON_PREFIX}/iosfrontend.conf")
56-
copy_to_keyboard(${keyboard} copy "${PROJECT_BINARY_DIR}/iosnotifications/notifications.conf" "${ADDON_PREFIX}/notifications.conf")
57-
copy_to_keyboard(${keyboard} copy "${PROJECT_BINARY_DIR}/uipanel/uipanel.conf" "${ADDON_PREFIX}/uipanel.conf")
58-
copy_to_keyboard(${keyboard} copy_directory "${PROJECT_SOURCE_DIR}/fcitx5-keyboard-layouts/layout" "share/layout")
55+
copy_to(${keyboard} copy "${PROJECT_BINARY_DIR}/iosfrontend/iosfrontend.conf" "${ADDON_PREFIX}/iosfrontend.conf")
56+
copy_to(${keyboard} copy "${PROJECT_BINARY_DIR}/iosnotifications/notifications.conf" "${ADDON_PREFIX}/notifications.conf")
57+
copy_to(${keyboard} copy "${PROJECT_BINARY_DIR}/uipanel/uipanel.conf" "${ADDON_PREFIX}/uipanel.conf")
58+
copy_to(${keyboard} copy_directory "${PROJECT_SOURCE_DIR}/fcitx5-keyboard-layouts/layout" "share/layout")
5959
endfunction()

0 commit comments

Comments
 (0)