1use chrono::{DateTime, Utc};
9use rustango::sql::Auto;
10use rustango::Model;
11use serde::{Deserialize, Serialize};
12
13#[derive(Model, Debug, Clone, Serialize, Deserialize)]
16#[rustango(
17 table = "cms_locale",
18 app = "cms",
19 display = "name",
20 admin(
21 list_display = "code, name, is_default, active, sort_order",
22 ordering = "sort_order, code",
23 list_filter = "is_default, active",
24 )
25)]
26pub struct Locale {
27 #[rustango(primary_key)]
28 pub id: Auto<i64>,
29
30 #[rustango(max_length = 16, index)]
32 pub code: String,
33
34 #[rustango(max_length = 64)]
37 pub name: String,
38
39 pub is_default: bool,
42
43 pub active: bool,
48
49 pub sort_order: i32,
52
53 #[rustango(auto_now_add)]
54 pub created_at: Auto<DateTime<Utc>>,
55}
56
57pub fn validate_code(raw: &str) -> Result<String, String> {
72 let s = raw.trim().replace('_', "-");
73 if s.is_empty() {
74 return Err("Locale code is required.".to_owned());
75 }
76 if s.chars().count() > 16 {
77 return Err("Locale code is too long (max 16 characters).".to_owned());
78 }
79 let mut out: Vec<String> = Vec::new();
80 for (i, sub) in s.split('-').enumerate() {
81 if sub.is_empty() {
82 return Err("Locale code has an empty segment (check the hyphens).".to_owned());
83 }
84 if i == 0 {
85 if (2..=3).contains(&sub.len()) && sub.bytes().all(|b| b.is_ascii_alphabetic()) {
87 out.push(sub.to_ascii_lowercase());
88 } else {
89 return Err(format!(
90 "`{sub}` is not a valid language subtag (2–3 letters, e.g. `en`, `zh`)."
91 ));
92 }
93 } else if sub.len() == 4 && sub.bytes().all(|b| b.is_ascii_alphabetic()) {
94 let mut chars = sub.chars();
96 let first = chars.next().unwrap_or_default().to_ascii_uppercase();
97 out.push(format!("{first}{}", chars.as_str().to_ascii_lowercase()));
98 } else if (sub.len() == 2 && sub.bytes().all(|b| b.is_ascii_alphabetic()))
99 || (sub.len() == 3 && sub.bytes().all(|b| b.is_ascii_digit()))
100 {
101 out.push(sub.to_ascii_uppercase());
103 } else if sub.len() <= 8 && sub.bytes().all(|b| b.is_ascii_alphanumeric()) {
104 out.push(sub.to_ascii_lowercase());
106 } else {
107 return Err(format!("`{sub}` is not a valid locale subtag."));
108 }
109 }
110 Ok(out.join("-"))
111}
112
113#[cfg(test)]
114mod tests {
115 use super::validate_code;
116
117 #[test]
118 fn accepts_and_normalizes_well_formed_tags() {
119 assert_eq!(validate_code("en").unwrap(), "en");
120 assert_eq!(validate_code(" fr-CA ").unwrap(), "fr-CA");
121 assert_eq!(validate_code("zh-Hans").unwrap(), "zh-Hans");
122 assert_eq!(validate_code("pt-BR").unwrap(), "pt-BR");
123 assert_eq!(validate_code("ckb").unwrap(), "ckb");
124 assert_eq!(validate_code("EN_us").unwrap(), "en-US");
126 assert_eq!(validate_code("ZH-hans").unwrap(), "zh-Hans");
127 assert_eq!(validate_code("es-419").unwrap(), "es-419");
128 }
129
130 #[test]
131 fn rejects_malformed_tags() {
132 assert!(validate_code("").is_err());
133 assert!(validate_code(" ").is_err());
134 assert!(validate_code("has space").is_err());
135 assert!(validate_code("123").is_err()); assert!(validate_code("e").is_err()); assert!(validate_code("en-").is_err()); assert!(validate_code("en--US").is_err());
139 assert!(validate_code(&"a".repeat(17)).is_err()); }
141}