mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-18 21:45:31 +09:00
fix(i18n): point static catalog translation at weblate (#2619)
This commit is contained in:
1 parent
3e32414849
commit
98cceae59d
3 files changed
+349
-20
No files matched your search
@@ -17,6 +17,10 @@ use crate::config::{
|
||||
EnvOverlay, SOURCE_LOCALE, default_app_dir, env_value, is_auto_i18n_unchanged_comment,
|
||||
locales_dir, positive_float_env, positive_int_env, trim_trailing_slash,
|
||||
};
|
||||
use crate::json_catalog::{
|
||||
StaticJsonCatalogConfig, read_static_json_entries, rebuild_static_json_allow_replacing,
|
||||
reset_static_json_translations,
|
||||
};
|
||||
use crate::llm::{
|
||||
LocalizationClient, LocalizationResult, base_options, clean_translation_response,
|
||||
};
|
||||
@@ -66,6 +70,7 @@ pub enum CatalogLayout {
|
||||
NestedMessages,
|
||||
FlatPo,
|
||||
StaticTs(StaticTsCatalogConfig),
|
||||
StaticJson(StaticJsonCatalogConfig),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
|
||||
@@ -128,17 +133,17 @@ impl RuntimeConfig {
|
||||
.join("errors")
|
||||
.join("src")
|
||||
.join("i18n")
|
||||
.join("weblate")
|
||||
.join("locales"),
|
||||
CatalogLayout::StaticTs(StaticTsCatalogConfig {
|
||||
CatalogLayout::StaticJson(StaticJsonCatalogConfig {
|
||||
kind: StaticTsCatalogKind::SimpleMessages,
|
||||
source_path: repo_root
|
||||
.join("packages")
|
||||
.join("errors")
|
||||
.join("src")
|
||||
.join("i18n")
|
||||
.join("ErrorI18nMessages.ts"),
|
||||
source_export: "ERROR_I18N_MESSAGES".to_string(),
|
||||
locale_function: "defineErrorI18nLocaleMessages".to_string(),
|
||||
.join("weblate")
|
||||
.join("messages.json"),
|
||||
}),
|
||||
),
|
||||
CatalogName::ApiContent => (
|
||||
@@ -147,17 +152,17 @@ impl RuntimeConfig {
|
||||
.join("src")
|
||||
.join("api")
|
||||
.join("content_i18n")
|
||||
.join("weblate")
|
||||
.join("locales"),
|
||||
CatalogLayout::StaticTs(StaticTsCatalogConfig {
|
||||
CatalogLayout::StaticJson(StaticJsonCatalogConfig {
|
||||
kind: StaticTsCatalogKind::SimpleMessages,
|
||||
source_path: repo_root
|
||||
.join("fluxer_api")
|
||||
.join("src")
|
||||
.join("api")
|
||||
.join("content_i18n")
|
||||
.join("ContentI18nMessages.ts"),
|
||||
source_export: "CONTENT_I18N_MESSAGES".to_string(),
|
||||
locale_function: "defineContentI18nLocaleMessages".to_string(),
|
||||
.join("weblate")
|
||||
.join("messages.json"),
|
||||
}),
|
||||
),
|
||||
CatalogName::Email => (
|
||||
@@ -167,8 +172,9 @@ impl RuntimeConfig {
|
||||
.join("email")
|
||||
.join("src")
|
||||
.join("email_i18n")
|
||||
.join("weblate")
|
||||
.join("locales"),
|
||||
CatalogLayout::StaticTs(StaticTsCatalogConfig {
|
||||
CatalogLayout::StaticJson(StaticJsonCatalogConfig {
|
||||
kind: StaticTsCatalogKind::EmailTemplates,
|
||||
source_path: repo_root
|
||||
.join("fluxer_api")
|
||||
@@ -176,9 +182,8 @@ impl RuntimeConfig {
|
||||
.join("email")
|
||||
.join("src")
|
||||
.join("email_i18n")
|
||||
.join("EmailI18nMessages.ts"),
|
||||
source_export: "EMAIL_I18N_MESSAGES".to_string(),
|
||||
locale_function: "defineEmailI18nLocaleMessages".to_string(),
|
||||
.join("weblate")
|
||||
.join("messages.json"),
|
||||
}),
|
||||
),
|
||||
};
|
||||
@@ -426,6 +431,7 @@ pub fn run_translation(config: &RuntimeConfig, args: &TranslateArgs) -> Result<u
|
||||
CatalogLayout::NestedMessages => "nested messages.po",
|
||||
CatalogLayout::FlatPo => "flat .po",
|
||||
CatalogLayout::StaticTs(_) => "static TypeScript locale map",
|
||||
CatalogLayout::StaticJson(_) => "weblate JSON locale catalog",
|
||||
}
|
||||
),
|
||||
false,
|
||||
@@ -436,6 +442,12 @@ pub fn run_translation(config: &RuntimeConfig, args: &TranslateArgs) -> Result<u
|
||||
false,
|
||||
);
|
||||
}
|
||||
if let CatalogLayout::StaticJson(static_config) = &config.catalog_layout {
|
||||
log(
|
||||
&format!("Source catalog: {}", static_config.source_path.display()),
|
||||
false,
|
||||
);
|
||||
}
|
||||
log(&format!("Model: {}", client.model()), false);
|
||||
log(&format!("LLM endpoint: {}", client.base_url()), false);
|
||||
log(
|
||||
@@ -589,9 +601,14 @@ fn available_locales(config: &RuntimeConfig) -> Result<Vec<String>> {
|
||||
};
|
||||
stem.to_string()
|
||||
}
|
||||
CatalogLayout::StaticTs(_) => {
|
||||
CatalogLayout::StaticTs(_) | CatalogLayout::StaticJson(_) => {
|
||||
let extension = if matches!(&config.catalog_layout, CatalogLayout::StaticJson(_)) {
|
||||
"json"
|
||||
} else {
|
||||
"ts"
|
||||
};
|
||||
let path = entry.path();
|
||||
if path.extension().and_then(|ext| ext.to_str()) != Some("ts") {
|
||||
if path.extension().and_then(|ext| ext.to_str()) != Some(extension) {
|
||||
continue;
|
||||
}
|
||||
let Some(stem) = path.file_stem().and_then(|stem| stem.to_str()) else {
|
||||
@@ -1155,7 +1172,10 @@ pub fn sync_source_locale(
|
||||
catalog_path: &Path,
|
||||
args: &TranslateArgs,
|
||||
) -> Result<LocaleResult> {
|
||||
if matches!(&config.catalog_layout, CatalogLayout::StaticTs(_)) {
|
||||
if matches!(
|
||||
&config.catalog_layout,
|
||||
CatalogLayout::StaticTs(_) | CatalogLayout::StaticJson(_)
|
||||
) {
|
||||
log(
|
||||
&format!(
|
||||
"[{SOURCE_LOCALE}] Source strings live in the static source catalog; nothing to sync"
|
||||
@@ -1223,9 +1243,13 @@ fn read_catalog_entries_from_content(
|
||||
parse_po(&content)
|
||||
}
|
||||
CatalogLayout::StaticTs(static_config) => {
|
||||
let source_content = read_static_source_catalog(static_config)?;
|
||||
let source_content = read_static_source_catalog(&static_config.source_path)?;
|
||||
read_static_ts_entries(static_config, &source_content, content, reset)
|
||||
}
|
||||
CatalogLayout::StaticJson(static_config) => {
|
||||
let source_content = read_static_source_catalog(&static_config.source_path)?;
|
||||
read_static_json_entries(static_config, &source_content, content, reset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1235,6 +1259,9 @@ fn reset_catalog_translations(config: &RuntimeConfig, content: &str) -> Result<S
|
||||
CatalogLayout::StaticTs(static_config) => {
|
||||
reset_static_ts_translations(static_config, content)
|
||||
}
|
||||
CatalogLayout::StaticJson(static_config) => {
|
||||
reset_static_json_translations(static_config, content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1248,15 +1275,24 @@ fn rebuild_catalog_allow_replacing(
|
||||
rebuild_po_allow_replacing(content, translations)
|
||||
}
|
||||
CatalogLayout::StaticTs(static_config) => {
|
||||
let source_content = read_static_source_catalog(static_config)?;
|
||||
let source_content = read_static_source_catalog(&static_config.source_path)?;
|
||||
rebuild_static_ts_allow_replacing(static_config, &source_content, content, translations)
|
||||
}
|
||||
CatalogLayout::StaticJson(static_config) => {
|
||||
let source_content = read_static_source_catalog(&static_config.source_path)?;
|
||||
rebuild_static_json_allow_replacing(
|
||||
static_config,
|
||||
&source_content,
|
||||
content,
|
||||
translations,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_static_source_catalog(config: &StaticTsCatalogConfig) -> Result<String> {
|
||||
fs::read_to_string(&config.source_path)
|
||||
.with_context(|| format!("failed to read {}", config.source_path.display()))
|
||||
fn read_static_source_catalog(source_path: &Path) -> Result<String> {
|
||||
fs::read_to_string(source_path)
|
||||
.with_context(|| format!("failed to read {}", source_path.display()))
|
||||
}
|
||||
|
||||
fn catalog_path(config: &RuntimeConfig, locale: &str) -> PathBuf {
|
||||
@@ -1264,6 +1300,7 @@ fn catalog_path(config: &RuntimeConfig, locale: &str) -> PathBuf {
|
||||
CatalogLayout::NestedMessages => config.locales_dir.join(locale).join("messages.po"),
|
||||
CatalogLayout::FlatPo => config.locales_dir.join(format!("{locale}.po")),
|
||||
CatalogLayout::StaticTs(_) => config.locales_dir.join(format!("{locale}.ts")),
|
||||
CatalogLayout::StaticJson(_) => config.locales_dir.join(format!("{locale}.json")),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user