1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Per-field, per-locale translation overrides for **snippets**.
//!
//! Mirrors [`crate::translation`] (which localizes pages) for the
//! `cms_snippet` library. The canonical `cms_snippet` row holds the
//! default-locale content; `cms_snippet_translation` overrides
//! individual fields (`title`, `body_markdown`, or dotted `data.*`
//! paths) per locale. A narrow tall table — same rationale as the page
//! table: column-friendly in PG/MySQL, easy to migrate, keeps the
//! canonical row free of locale concerns.
//!
//! pt 1 (this module + the admin translate editor) lets editors author
//! snippet translations. Wiring the public render to substitute them
//! for the active locale is pt 2 — the `fetch_for*` helpers here are
//! the seam it will use.
use chrono::{DateTime, Utc};
use rustango::sql::Auto;
use rustango::Model;
use serde::{Deserialize, Serialize};
/// One row per `(snippet_id, locale_id, field_path)`. An empty `value`
/// is treated as "no override" and the canonical content shows through.
#[derive(Model, Debug, Clone, Serialize, Deserialize)]
#[rustango(
table = "cms_snippet_translation",
app = "cms",
admin(
list_display = "snippet_id, locale_id, field_path, value, updated_at",
ordering = "snippet_id",
list_filter = "locale_id, field_path",
)
)]
pub struct SnippetTranslation {
#[rustango(primary_key)]
pub id: Auto<i64>,
#[rustango(fk = "cms_snippet", on = "id", index)]
pub snippet_id: i64,
#[rustango(fk = "cms_locale", on = "id", index)]
pub locale_id: i64,
/// Field on `cms_snippet`: `title`, `body_markdown`, or a dotted
/// `data.<key>` path into the JSON bag.
#[rustango(max_length = 64, index)]
pub field_path: String,
/// Localized value. Empty string = "no override".
pub value: String,
#[rustango(auto_now)]
pub updated_at: Auto<DateTime<Utc>>,
}
/// Fetch every translation for `(snippet_id, locale_id)` as a map keyed
/// on `field_path`. Empty values are dropped (canonical shows through).
///
/// # Errors
/// Driver / query failures.
pub async fn fetch_for(
pool: &rustango::sql::Pool,
snippet_id: i64,
locale_id: i64,
) -> Result<std::collections::HashMap<String, String>, rustango::sql::ExecError> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
let rows: Vec<SnippetTranslation> = SnippetTranslation::objects()
.where_(SnippetTranslation::snippet_id.eq(snippet_id))
.where_(SnippetTranslation::locale_id.eq(locale_id))
.fetch(pool)
.await?;
Ok(rows
.into_iter()
.filter(|t| !t.value.is_empty())
.map(|t| (t.field_path, t.value))
.collect())
}
/// Fetch translations for every snippet in `snippet_ids` under one
/// `locale_id`, as `{ snippet_id -> { field_path -> value } }`. Single
/// `WHERE locale_id = $1 AND snippet_id IN (...)` SELECT — the seam the
/// public render (pt 2) will use to localize a page's snippets without
/// N round-trips. Empty `snippet_ids` → empty map; empty values dropped.
///
/// # Errors
/// Driver / query failures.
pub async fn fetch_for_snippets(
pool: &rustango::sql::Pool,
snippet_ids: &[i64],
locale_id: i64,
) -> Result<
std::collections::HashMap<i64, std::collections::HashMap<String, String>>,
rustango::sql::ExecError,
> {
use rustango::core::Column as _;
use rustango::sql::FetcherPool as _;
if snippet_ids.is_empty() {
return Ok(std::collections::HashMap::new());
}
let rows: Vec<SnippetTranslation> = SnippetTranslation::objects()
.where_(SnippetTranslation::locale_id.eq(locale_id))
.where_(SnippetTranslation::snippet_id.is_in(snippet_ids.iter().copied()))
.fetch(pool)
.await?;
let mut out: std::collections::HashMap<i64, std::collections::HashMap<String, String>> =
std::collections::HashMap::new();
for row in rows {
if row.value.is_empty() {
continue;
}
out.entry(row.snippet_id)
.or_default()
.insert(row.field_path, row.value);
}
Ok(out)
}
/// Validate a translation `field_path`: ≤64 chars, ASCII alphanumeric
/// plus `_` and `.` (the dotted `data.*` form). Mirrors the page
/// translate handler's new-field guard. Returns the trimmed path on
/// success. Pure — unit-tested + reused by the submit handler.
///
/// # Errors
/// A human-readable message when the path is empty, too long, or has
/// disallowed characters.
pub fn validate_field_path(raw: &str) -> Result<String, String> {
let p = raw.trim();
if p.is_empty() {
return Err("Field name is required.".to_owned());
}
if p.chars().count() > 64 {
return Err("Field name is too long (max 64 characters).".to_owned());
}
if !p
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '.')
{
return Err("Field name may only contain letters, digits, '_' and '.'.".to_owned());
}
Ok(p.to_owned())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn validate_field_path_accepts_scalar_and_dotted() {
assert_eq!(validate_field_path("title").unwrap(), "title");
assert_eq!(
validate_field_path(" body_markdown ").unwrap(),
"body_markdown"
);
assert_eq!(
validate_field_path("data.cta_label").unwrap(),
"data.cta_label"
);
}
#[test]
fn validate_field_path_rejects_bad_input() {
assert!(validate_field_path("").is_err());
assert!(validate_field_path(" ").is_err());
assert!(validate_field_path("has space").is_err());
assert!(validate_field_path("inject;drop").is_err());
assert!(validate_field_path(&"x".repeat(65)).is_err());
}
}