mirror of
https://github.com/NetworkCats/rustyip.git
synced 2026-11-24 00:05:36 +00:00
feat: add full i18n support for 14 languages with gettext PO translations
- Add i18n module with Locale enum, PO parser, and Accept-Language negotiation
- Create gettext .po translation files for en, es, de, ja, ko, id, fr, ru, pt, it, zh-Hant, zh-Hans, nl, ar
- Route HTML pages under /{lang}/ prefix using IETF language tags
- Auto-detect browser language and redirect from / to /{lang}/
- Add language selector dropdown in footer
- Localize geo names (country, city) from MMDB where available
- Add hreflang alternate links and multilingual sitemap for SEO
- Support RTL layout for Arabic
- Update integration tests for new routing structure
This commit is contained in:
+436
-36
@@ -2,14 +2,15 @@ use std::net::IpAddr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use askama::Template;
|
||||
use axum::extract::{Query, State};
|
||||
use axum::extract::{Path, Query, State};
|
||||
use axum::http::{HeaderMap, StatusCode, header};
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum::response::{Html, IntoResponse, Redirect, Response};
|
||||
|
||||
use crate::cli_detect::is_cli_user_agent;
|
||||
use crate::db::{self, SharedDb};
|
||||
use crate::error::AppError;
|
||||
use crate::models::{IpInfo, get_en_name};
|
||||
use crate::i18n::{self, Locale};
|
||||
use crate::models::{IpInfo, get_localized_name};
|
||||
use crate::static_assets;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
@@ -42,6 +43,61 @@ struct IndexTemplate {
|
||||
is_vpn: bool,
|
||||
is_hosting: bool,
|
||||
is_tor: bool,
|
||||
locale: Locale,
|
||||
lang_tag: &'static str,
|
||||
html_dir: &'static str,
|
||||
t_title: &'static str,
|
||||
t_description: &'static str,
|
||||
t_search_label: &'static str,
|
||||
t_search_placeholder: &'static str,
|
||||
t_asn: &'static str,
|
||||
t_org: &'static str,
|
||||
t_country: &'static str,
|
||||
t_city: &'static str,
|
||||
t_proxy: &'static str,
|
||||
t_vpn: &'static str,
|
||||
t_hosting: &'static str,
|
||||
t_tor: &'static str,
|
||||
t_yes: &'static str,
|
||||
t_no: &'static str,
|
||||
t_faq: &'static str,
|
||||
t_faq_cli_q: &'static str,
|
||||
t_faq_cli_a: &'static str,
|
||||
t_faq_cli_query_current: &'static str,
|
||||
t_faq_cli_query_own: &'static str,
|
||||
t_faq_cli_json: &'static str,
|
||||
t_faq_cli_specific: &'static str,
|
||||
t_faq_ipv46_q: &'static str,
|
||||
t_faq_ipv46_a: &'static str,
|
||||
t_faq_json_q: &'static str,
|
||||
t_faq_json_a: &'static str,
|
||||
t_faq_automate_q: &'static str,
|
||||
t_faq_automate_a1: &'static str,
|
||||
t_faq_automate_a2: &'static str,
|
||||
t_faq_data_q: &'static str,
|
||||
t_faq_data_a: &'static str,
|
||||
t_faq_data_attr: &'static str,
|
||||
t_faq_selfhost_q: &'static str,
|
||||
t_faq_selfhost_a: &'static str,
|
||||
t_faq_diff_q: &'static str,
|
||||
t_faq_diff_a1: &'static str,
|
||||
t_faq_diff_a2: &'static str,
|
||||
t_faq_diff_a3: &'static str,
|
||||
t_faq_stable_q: &'static str,
|
||||
t_faq_stable_a1: &'static str,
|
||||
t_faq_stable_a2: &'static str,
|
||||
t_faq_stable_a3: &'static str,
|
||||
t_faq_sponsor_q: &'static str,
|
||||
t_faq_sponsor_a: &'static str,
|
||||
t_footer_license: &'static str,
|
||||
t_footer_source: &'static str,
|
||||
t_footer_db: &'static str,
|
||||
t_footer_attribution: &'static str,
|
||||
t_table_aria: &'static str,
|
||||
t_faq_aria: &'static str,
|
||||
t_nav_aria: &'static str,
|
||||
t_language: &'static str,
|
||||
all_locales: &'static [Locale],
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
@@ -51,6 +107,18 @@ struct ErrorAlertTemplate {
|
||||
css_version: u64,
|
||||
query: String,
|
||||
error_message: String,
|
||||
locale: Locale,
|
||||
lang_tag: &'static str,
|
||||
html_dir: &'static str,
|
||||
t_error: &'static str,
|
||||
t_search_label: &'static str,
|
||||
t_search_placeholder: &'static str,
|
||||
t_footer_license: &'static str,
|
||||
t_footer_source: &'static str,
|
||||
t_footer_db: &'static str,
|
||||
t_nav_aria: &'static str,
|
||||
t_language: &'static str,
|
||||
all_locales: &'static [Locale],
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
@@ -61,6 +129,10 @@ struct ErrorPageTemplate {
|
||||
status_code: u16,
|
||||
status_text: String,
|
||||
error_message: String,
|
||||
lang_tag: &'static str,
|
||||
html_dir: &'static str,
|
||||
t_go_home: &'static str,
|
||||
locale: Locale,
|
||||
}
|
||||
|
||||
fn resolve_ip(headers: &HeaderMap, query: &IpQuery, dev_mode: bool) -> Result<IpAddr, AppError> {
|
||||
@@ -116,26 +188,208 @@ fn format_org(info: &IpInfo) -> &str {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn format_country(info: &IpInfo) -> &str {
|
||||
fn format_country(info: &IpInfo, locale: Locale) -> &str {
|
||||
info.country
|
||||
.as_ref()
|
||||
.map(|c| get_en_name(&c.names))
|
||||
.map(|c| get_localized_name(&c.names, locale.mmdb_key()))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn format_city(info: &IpInfo) -> &str {
|
||||
fn format_city(info: &IpInfo, locale: Locale) -> &str {
|
||||
info.city
|
||||
.as_ref()
|
||||
.map(|c| get_en_name(&c.names))
|
||||
.map(|c| get_localized_name(&c.names, locale.mmdb_key()))
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn render_error_alert(state: &AppState, query: &str, error: &AppError) -> Response {
|
||||
fn build_translations(locale: Locale) -> Translations {
|
||||
Translations {
|
||||
t_title: i18n::translate(locale, "What is my IP address?"),
|
||||
t_description: i18n::translate(
|
||||
locale,
|
||||
"Find your public IP address instantly. Look up geolocation, ASN, organization, and proxy/VPN/Tor detection for any IP address.",
|
||||
),
|
||||
t_search_label: i18n::translate(locale, "Search IP address"),
|
||||
t_search_placeholder: i18n::translate(locale, "Type to search IP data"),
|
||||
t_asn: i18n::translate(locale, "ASN"),
|
||||
t_org: i18n::translate(locale, "Org"),
|
||||
t_country: i18n::translate(locale, "Country"),
|
||||
t_city: i18n::translate(locale, "City"),
|
||||
t_proxy: i18n::translate(locale, "Proxy"),
|
||||
t_vpn: i18n::translate(locale, "VPN"),
|
||||
t_hosting: i18n::translate(locale, "Hosting"),
|
||||
t_tor: i18n::translate(locale, "Tor"),
|
||||
t_yes: i18n::translate(locale, "Yes"),
|
||||
t_no: i18n::translate(locale, "No"),
|
||||
t_faq: i18n::translate(locale, "FAQ"),
|
||||
t_faq_cli_q: i18n::translate(locale, "How to use this service from the command line"),
|
||||
t_faq_cli_a: i18n::translate(
|
||||
locale,
|
||||
"You can use various command-line tools such as curl, httpie, and wget to use this service. Taking the most popular one, curl, as an example:",
|
||||
),
|
||||
t_faq_cli_query_current: i18n::translate(locale, "Query current IP:"),
|
||||
t_faq_cli_query_own: i18n::translate(locale, "Query information for your own IP:"),
|
||||
t_faq_cli_json: i18n::translate(locale, "Or display all information in JSON format:"),
|
||||
t_faq_cli_specific: i18n::translate(locale, "Query information for a specific IP:"),
|
||||
t_faq_ipv46_q: i18n::translate(
|
||||
locale,
|
||||
"How to force the display of my IPv4 or IPv6 address?",
|
||||
),
|
||||
t_faq_ipv46_a: i18n::translate(
|
||||
locale,
|
||||
"You can use <code>curl -4</code> or <code>curl -6</code> to view only your IPv4 or IPv6 address respectively.",
|
||||
),
|
||||
t_faq_json_q: i18n::translate(locale, "How to get a JSON format response?"),
|
||||
t_faq_json_a: i18n::translate(
|
||||
locale,
|
||||
"You can send a request to the <code>/json</code> endpoint, or send a request with an <code>application/json</code> header.",
|
||||
),
|
||||
t_faq_automate_q: i18n::translate(
|
||||
locale,
|
||||
"Can I automate calls to your service via programs?",
|
||||
),
|
||||
t_faq_automate_a1: i18n::translate(
|
||||
locale,
|
||||
"Certainly, as long as the rate limit is respected. Under normal conditions, the rate limit is 30 RPM (Requests Per Minute); under high load, we will increase the restriction to 10-5 RPM.",
|
||||
),
|
||||
t_faq_automate_a2: i18n::translate(
|
||||
locale,
|
||||
"Our API is only suitable for manual calls or small-scale projects. If your website uses our API to query visitor IPs, please ensure you use a message queue to send requests to avoid blocking. If your project has high traffic or is latency-sensitive, please use our open-source offline database: <a href=\"https://github.com/NetworkCats/Merged-IP-Data\">Merged IP Database</a>, which is the same database used by this project.",
|
||||
),
|
||||
t_faq_data_q: i18n::translate(locale, "Where does the IP data come from?"),
|
||||
t_faq_data_a: i18n::translate(
|
||||
locale,
|
||||
"IP geographic data primarily comes from the free databases of MaxMind and DB-IP; AS data comes from IPinfo's free database; and IP proxy data comes from my own <a href=\"https://github.com/NetworkCats/OpenProxyDB\">OpenProxyDB</a> database.",
|
||||
),
|
||||
t_faq_data_attr: i18n::translate(locale, "Data Attributions:"),
|
||||
t_faq_selfhost_q: i18n::translate(locale, "Can I run my own service?"),
|
||||
t_faq_selfhost_a: i18n::translate(
|
||||
locale,
|
||||
"Yes, the source code and database for this project are both open-source and hosted on GitHub:",
|
||||
),
|
||||
t_faq_diff_q: i18n::translate(
|
||||
locale,
|
||||
"How is this website different from other IP lookup sites?",
|
||||
),
|
||||
t_faq_diff_a1: i18n::translate(
|
||||
locale,
|
||||
"The biggest difference is that our database includes proxy-related information, and the proxy intelligence we provide is highly accurate. We have privately compared it with commercial proxy detection databases like IPinfo; our database's precision is fully comparable to commercial ones, and it is even more accurate in detecting residential proxies.",
|
||||
),
|
||||
t_faq_diff_a2: i18n::translate(
|
||||
locale,
|
||||
"Secondly, our geographic coverage for IPv6 is more comprehensive because we merge multiple databases to provide more complete data.",
|
||||
),
|
||||
t_faq_diff_a3: i18n::translate(
|
||||
locale,
|
||||
"Our UX is also better than any IP lookup site on the market. The site runs without any JavaScript, and we only load a minimal amount of external resources (fonts, icons), making the initial load speed extremely fast.",
|
||||
),
|
||||
t_faq_stable_q: i18n::translate(locale, "Is this service stable?"),
|
||||
t_faq_stable_a1: i18n::translate(
|
||||
locale,
|
||||
"The business logic of this service is written entirely in Rust, offering excellent performance that ensures the site remains operational even under L7 DDoS attacks. The site runs on a VPS with 2 cores and 2 GB of RAM, which is already overkill for this setup -- unless we encounter a massive L7 DDoS attack, though we have Cloudflare in front of us.",
|
||||
),
|
||||
t_faq_stable_a2: i18n::translate(
|
||||
locale,
|
||||
"Running this website only costs me $18.00 per month. Because the cost is so low, I will not stop operations due to hardware costs in the foreseeable future, nor will I add any advertisements.",
|
||||
),
|
||||
t_faq_stable_a3: i18n::translate(
|
||||
locale,
|
||||
"Rest assured, this service will definitely not \"Rug Pull.\"",
|
||||
),
|
||||
t_faq_sponsor_q: i18n::translate(locale, "How can I sponsor this service?"),
|
||||
t_faq_sponsor_a: i18n::translate(
|
||||
locale,
|
||||
"I accept cryptocurrency donations. If you would like to sponsor, please contact:",
|
||||
),
|
||||
t_footer_license: i18n::translate(locale, "Website source code licensed under"),
|
||||
t_footer_source: i18n::translate(locale, "Website Source"),
|
||||
t_footer_db: i18n::translate(locale, "IP Database"),
|
||||
t_footer_attribution: i18n::translate(locale, "IP data provided by:"),
|
||||
t_error: i18n::translate(locale, "Error"),
|
||||
t_go_home: i18n::translate(locale, "Go to Home"),
|
||||
t_table_aria: i18n::translate(locale, "IP address information"),
|
||||
t_faq_aria: i18n::translate(locale, "Frequently Asked Questions"),
|
||||
t_nav_aria: i18n::translate(locale, "Project links"),
|
||||
t_language: i18n::translate(locale, "Language"),
|
||||
}
|
||||
}
|
||||
|
||||
struct Translations {
|
||||
t_title: &'static str,
|
||||
t_description: &'static str,
|
||||
t_search_label: &'static str,
|
||||
t_search_placeholder: &'static str,
|
||||
t_asn: &'static str,
|
||||
t_org: &'static str,
|
||||
t_country: &'static str,
|
||||
t_city: &'static str,
|
||||
t_proxy: &'static str,
|
||||
t_vpn: &'static str,
|
||||
t_hosting: &'static str,
|
||||
t_tor: &'static str,
|
||||
t_yes: &'static str,
|
||||
t_no: &'static str,
|
||||
t_faq: &'static str,
|
||||
t_faq_cli_q: &'static str,
|
||||
t_faq_cli_a: &'static str,
|
||||
t_faq_cli_query_current: &'static str,
|
||||
t_faq_cli_query_own: &'static str,
|
||||
t_faq_cli_json: &'static str,
|
||||
t_faq_cli_specific: &'static str,
|
||||
t_faq_ipv46_q: &'static str,
|
||||
t_faq_ipv46_a: &'static str,
|
||||
t_faq_json_q: &'static str,
|
||||
t_faq_json_a: &'static str,
|
||||
t_faq_automate_q: &'static str,
|
||||
t_faq_automate_a1: &'static str,
|
||||
t_faq_automate_a2: &'static str,
|
||||
t_faq_data_q: &'static str,
|
||||
t_faq_data_a: &'static str,
|
||||
t_faq_data_attr: &'static str,
|
||||
t_faq_selfhost_q: &'static str,
|
||||
t_faq_selfhost_a: &'static str,
|
||||
t_faq_diff_q: &'static str,
|
||||
t_faq_diff_a1: &'static str,
|
||||
t_faq_diff_a2: &'static str,
|
||||
t_faq_diff_a3: &'static str,
|
||||
t_faq_stable_q: &'static str,
|
||||
t_faq_stable_a1: &'static str,
|
||||
t_faq_stable_a2: &'static str,
|
||||
t_faq_stable_a3: &'static str,
|
||||
t_faq_sponsor_q: &'static str,
|
||||
t_faq_sponsor_a: &'static str,
|
||||
t_footer_license: &'static str,
|
||||
t_footer_source: &'static str,
|
||||
t_footer_db: &'static str,
|
||||
t_footer_attribution: &'static str,
|
||||
t_error: &'static str,
|
||||
t_go_home: &'static str,
|
||||
t_table_aria: &'static str,
|
||||
t_faq_aria: &'static str,
|
||||
t_nav_aria: &'static str,
|
||||
t_language: &'static str,
|
||||
}
|
||||
|
||||
fn render_error_alert(state: &AppState, locale: Locale, query: &str, error: &AppError) -> Response {
|
||||
let t = build_translations(locale);
|
||||
let error_msg = i18n::translate(locale, error.message());
|
||||
let template = ErrorAlertTemplate {
|
||||
site_domain: state.site_domain.clone(),
|
||||
css_version: static_assets::asset_version("style.css"),
|
||||
query: query.to_owned(),
|
||||
error_message: error.message().to_owned(),
|
||||
error_message: error_msg.to_owned(),
|
||||
locale,
|
||||
lang_tag: locale.tag(),
|
||||
html_dir: locale.html_dir(),
|
||||
t_error: t.t_error,
|
||||
t_search_label: t.t_search_label,
|
||||
t_search_placeholder: t.t_search_placeholder,
|
||||
t_footer_license: t.t_footer_license,
|
||||
t_footer_source: t.t_footer_source,
|
||||
t_footer_db: t.t_footer_db,
|
||||
t_nav_aria: t.t_nav_aria,
|
||||
t_language: t.t_language,
|
||||
all_locales: Locale::ALL,
|
||||
};
|
||||
let status = error.status_code();
|
||||
match template.render() {
|
||||
@@ -144,13 +398,24 @@ fn render_error_alert(state: &AppState, query: &str, error: &AppError) -> Respon
|
||||
}
|
||||
}
|
||||
|
||||
fn render_error_page(site_domain: &Arc<str>, status: StatusCode, message: &str) -> Response {
|
||||
fn render_error_page(
|
||||
site_domain: &Arc<str>,
|
||||
locale: Locale,
|
||||
status: StatusCode,
|
||||
message: &str,
|
||||
) -> Response {
|
||||
let t = build_translations(locale);
|
||||
let localized_message = i18n::translate(locale, message);
|
||||
let template = ErrorPageTemplate {
|
||||
site_domain: site_domain.clone(),
|
||||
css_version: static_assets::asset_version("style.css"),
|
||||
status_code: status.as_u16(),
|
||||
status_text: status.canonical_reason().unwrap_or("Error").to_owned(),
|
||||
error_message: message.to_owned(),
|
||||
error_message: localized_message.to_owned(),
|
||||
lang_tag: locale.tag(),
|
||||
html_dir: locale.html_dir(),
|
||||
t_go_home: t.t_go_home,
|
||||
locale,
|
||||
};
|
||||
match template.render() {
|
||||
Ok(html) => (status, Html(html)).into_response(),
|
||||
@@ -158,11 +423,62 @@ fn render_error_page(site_domain: &Arc<str>, status: StatusCode, message: &str)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn root(
|
||||
pub async fn root_redirect(
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
Query(query): Query<IpQuery>,
|
||||
) -> Response {
|
||||
let ua = headers
|
||||
.get("User-Agent")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("");
|
||||
|
||||
// CLI clients and JSON requests bypass i18n entirely.
|
||||
if is_cli_user_agent(ua) && query.ip.is_none() {
|
||||
return match extract_client_ip(&headers, state.dev_mode) {
|
||||
Ok(ip) => format!("{ip}\n").into_response(),
|
||||
Err(e) => e.into_response(),
|
||||
};
|
||||
}
|
||||
|
||||
if wants_json(&headers) && query.ip.is_none() {
|
||||
return match extract_client_ip(&headers, state.dev_mode)
|
||||
.and_then(|ip| lookup_ip(&state.db, ip))
|
||||
{
|
||||
Ok(info) => axum::Json(info).into_response(),
|
||||
Err(e) => e.into_response(),
|
||||
};
|
||||
}
|
||||
|
||||
let locale = i18n::negotiate_locale(&headers);
|
||||
let mut redirect_path = format!("/{}", locale.tag());
|
||||
|
||||
if let Some(ref ip) = query.ip {
|
||||
redirect_path.push_str("?ip=");
|
||||
redirect_path.push_str(&urlencoding::encode(ip));
|
||||
}
|
||||
|
||||
Redirect::temporary(&redirect_path).into_response()
|
||||
}
|
||||
|
||||
pub async fn root(
|
||||
State(state): State<AppState>,
|
||||
Path(lang): Path<String>,
|
||||
headers: HeaderMap,
|
||||
Query(query): Query<IpQuery>,
|
||||
) -> Response {
|
||||
let locale = match Locale::from_tag(&lang) {
|
||||
Some(l) => l,
|
||||
None => {
|
||||
return render_error_page(
|
||||
&state.site_domain,
|
||||
Locale::En,
|
||||
StatusCode::NOT_FOUND,
|
||||
"The page you are looking for does not exist.",
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let ua = headers
|
||||
.get("User-Agent")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
@@ -188,14 +504,16 @@ pub async fn root(
|
||||
|
||||
let ip = match resolve_ip(&headers, &query, state.dev_mode) {
|
||||
Ok(ip) => ip,
|
||||
Err(e) => return render_error_alert(&state, &query_str, &e),
|
||||
Err(e) => return render_error_alert(&state, locale, &query_str, &e),
|
||||
};
|
||||
|
||||
let info = match lookup_ip(&state.db, ip) {
|
||||
Ok(info) => info,
|
||||
Err(e) => return render_error_alert(&state, &query_str, &e),
|
||||
Err(e) => return render_error_alert(&state, locale, &query_str, &e),
|
||||
};
|
||||
|
||||
let t = build_translations(locale);
|
||||
|
||||
let template = IndexTemplate {
|
||||
ip: info.ip.clone(),
|
||||
query: query.ip.unwrap_or_default(),
|
||||
@@ -203,17 +521,72 @@ pub async fn root(
|
||||
css_version: static_assets::asset_version("style.css"),
|
||||
asn: format_asn(&info),
|
||||
org: format_org(&info).to_owned(),
|
||||
country: format_country(&info).to_owned(),
|
||||
city: format_city(&info).to_owned(),
|
||||
country: format_country(&info, locale).to_owned(),
|
||||
city: format_city(&info, locale).to_owned(),
|
||||
is_proxy: info.proxy.is_proxy,
|
||||
is_vpn: info.proxy.is_vpn,
|
||||
is_hosting: info.proxy.is_hosting,
|
||||
is_tor: info.proxy.is_tor,
|
||||
locale,
|
||||
lang_tag: locale.tag(),
|
||||
html_dir: locale.html_dir(),
|
||||
t_title: t.t_title,
|
||||
t_description: t.t_description,
|
||||
t_search_label: t.t_search_label,
|
||||
t_search_placeholder: t.t_search_placeholder,
|
||||
t_asn: t.t_asn,
|
||||
t_org: t.t_org,
|
||||
t_country: t.t_country,
|
||||
t_city: t.t_city,
|
||||
t_proxy: t.t_proxy,
|
||||
t_vpn: t.t_vpn,
|
||||
t_hosting: t.t_hosting,
|
||||
t_tor: t.t_tor,
|
||||
t_yes: t.t_yes,
|
||||
t_no: t.t_no,
|
||||
t_faq: t.t_faq,
|
||||
t_faq_cli_q: t.t_faq_cli_q,
|
||||
t_faq_cli_a: t.t_faq_cli_a,
|
||||
t_faq_cli_query_current: t.t_faq_cli_query_current,
|
||||
t_faq_cli_query_own: t.t_faq_cli_query_own,
|
||||
t_faq_cli_json: t.t_faq_cli_json,
|
||||
t_faq_cli_specific: t.t_faq_cli_specific,
|
||||
t_faq_ipv46_q: t.t_faq_ipv46_q,
|
||||
t_faq_ipv46_a: t.t_faq_ipv46_a,
|
||||
t_faq_json_q: t.t_faq_json_q,
|
||||
t_faq_json_a: t.t_faq_json_a,
|
||||
t_faq_automate_q: t.t_faq_automate_q,
|
||||
t_faq_automate_a1: t.t_faq_automate_a1,
|
||||
t_faq_automate_a2: t.t_faq_automate_a2,
|
||||
t_faq_data_q: t.t_faq_data_q,
|
||||
t_faq_data_a: t.t_faq_data_a,
|
||||
t_faq_data_attr: t.t_faq_data_attr,
|
||||
t_faq_selfhost_q: t.t_faq_selfhost_q,
|
||||
t_faq_selfhost_a: t.t_faq_selfhost_a,
|
||||
t_faq_diff_q: t.t_faq_diff_q,
|
||||
t_faq_diff_a1: t.t_faq_diff_a1,
|
||||
t_faq_diff_a2: t.t_faq_diff_a2,
|
||||
t_faq_diff_a3: t.t_faq_diff_a3,
|
||||
t_faq_stable_q: t.t_faq_stable_q,
|
||||
t_faq_stable_a1: t.t_faq_stable_a1,
|
||||
t_faq_stable_a2: t.t_faq_stable_a2,
|
||||
t_faq_stable_a3: t.t_faq_stable_a3,
|
||||
t_faq_sponsor_q: t.t_faq_sponsor_q,
|
||||
t_faq_sponsor_a: t.t_faq_sponsor_a,
|
||||
t_footer_license: t.t_footer_license,
|
||||
t_footer_source: t.t_footer_source,
|
||||
t_footer_db: t.t_footer_db,
|
||||
t_footer_attribution: t.t_footer_attribution,
|
||||
t_table_aria: t.t_table_aria,
|
||||
t_faq_aria: t.t_faq_aria,
|
||||
t_nav_aria: t.t_nav_aria,
|
||||
t_language: t.t_language,
|
||||
all_locales: Locale::ALL,
|
||||
};
|
||||
|
||||
match template.render() {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(_) => render_error_alert(&state, &query_str, &AppError::TemplateRenderFailed),
|
||||
Err(_) => render_error_alert(&state, locale, &query_str, &AppError::TemplateRenderFailed),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,21 +723,28 @@ pub fn build_openapi_json(site_domain: &str) -> Arc<str> {
|
||||
}
|
||||
|
||||
pub async fn robots_txt(State(state): State<AppState>) -> Response {
|
||||
let mut disallow = String::new();
|
||||
for path in &[
|
||||
"/json",
|
||||
"/ip",
|
||||
"/asn",
|
||||
"/org",
|
||||
"/country",
|
||||
"/city",
|
||||
"/proxy",
|
||||
"/vpn",
|
||||
"/hosting",
|
||||
"/tor",
|
||||
"/health",
|
||||
"/openapi.json",
|
||||
] {
|
||||
disallow.push_str(&format!("Disallow: {path}\n"));
|
||||
}
|
||||
|
||||
let body = format!(
|
||||
"User-agent: *\n\
|
||||
Allow: /\n\
|
||||
Disallow: /json\n\
|
||||
Disallow: /ip\n\
|
||||
Disallow: /asn\n\
|
||||
Disallow: /org\n\
|
||||
Disallow: /country\n\
|
||||
Disallow: /city\n\
|
||||
Disallow: /proxy\n\
|
||||
Disallow: /vpn\n\
|
||||
Disallow: /hosting\n\
|
||||
Disallow: /tor\n\
|
||||
Disallow: /health\n\
|
||||
Disallow: /openapi.json\n\
|
||||
{disallow}\
|
||||
\n\
|
||||
Sitemap: https://{}/sitemap.xml\n",
|
||||
state.site_domain
|
||||
@@ -373,14 +753,32 @@ pub async fn robots_txt(State(state): State<AppState>) -> Response {
|
||||
}
|
||||
|
||||
pub async fn sitemap_xml(State(state): State<AppState>) -> Response {
|
||||
let mut urls = String::new();
|
||||
let domain = &state.site_domain;
|
||||
|
||||
for locale in Locale::ALL {
|
||||
let tag = locale.tag();
|
||||
urls.push_str(&format!(
|
||||
" <url>\n <loc>https://{domain}/{tag}/</loc>\n"
|
||||
));
|
||||
for alt in Locale::ALL {
|
||||
let alt_tag = alt.tag();
|
||||
urls.push_str(&format!(
|
||||
" <xhtml:link rel=\"alternate\" hreflang=\"{alt_tag}\" href=\"https://{domain}/{alt_tag}/\"/>\n"
|
||||
));
|
||||
}
|
||||
urls.push_str(
|
||||
&format!(" <xhtml:link rel=\"alternate\" hreflang=\"x-default\" href=\"https://{domain}/\"/>\n"),
|
||||
);
|
||||
urls.push_str(" </url>\n");
|
||||
}
|
||||
|
||||
let body = format!(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
|
||||
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n\
|
||||
\x20 <url>\n\
|
||||
\x20 <loc>https://{}/</loc>\n\
|
||||
\x20 </url>\n\
|
||||
</urlset>\n",
|
||||
state.site_domain
|
||||
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n\
|
||||
\x20 xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">\n\
|
||||
{urls}\
|
||||
</urlset>\n"
|
||||
);
|
||||
(
|
||||
[(header::CONTENT_TYPE, "application/xml; charset=utf-8")],
|
||||
@@ -389,9 +787,11 @@ pub async fn sitemap_xml(State(state): State<AppState>) -> Response {
|
||||
.into_response()
|
||||
}
|
||||
|
||||
pub async fn not_found(State(state): State<AppState>) -> Response {
|
||||
pub async fn not_found(State(state): State<AppState>, headers: HeaderMap) -> Response {
|
||||
let locale = i18n::negotiate_locale(&headers);
|
||||
render_error_page(
|
||||
&state.site_domain,
|
||||
locale,
|
||||
StatusCode::NOT_FOUND,
|
||||
"The page you are looking for does not exist.",
|
||||
)
|
||||
|
||||
+455
@@ -0,0 +1,455 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use axum::http::HeaderMap;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Locale {
|
||||
En,
|
||||
Es,
|
||||
De,
|
||||
Ja,
|
||||
Ko,
|
||||
Id,
|
||||
Fr,
|
||||
Ru,
|
||||
Pt,
|
||||
It,
|
||||
ZhHant,
|
||||
ZhHans,
|
||||
Nl,
|
||||
Ar,
|
||||
}
|
||||
|
||||
impl Locale {
|
||||
pub const ALL: &[Locale] = &[
|
||||
Locale::En,
|
||||
Locale::Es,
|
||||
Locale::De,
|
||||
Locale::Ja,
|
||||
Locale::Ko,
|
||||
Locale::Id,
|
||||
Locale::Fr,
|
||||
Locale::Ru,
|
||||
Locale::Pt,
|
||||
Locale::It,
|
||||
Locale::ZhHant,
|
||||
Locale::ZhHans,
|
||||
Locale::Nl,
|
||||
Locale::Ar,
|
||||
];
|
||||
|
||||
pub fn tag(self) -> &'static str {
|
||||
match self {
|
||||
Locale::En => "en",
|
||||
Locale::Es => "es",
|
||||
Locale::De => "de",
|
||||
Locale::Ja => "ja",
|
||||
Locale::Ko => "ko",
|
||||
Locale::Id => "id",
|
||||
Locale::Fr => "fr",
|
||||
Locale::Ru => "ru",
|
||||
Locale::Pt => "pt",
|
||||
Locale::It => "it",
|
||||
Locale::ZhHant => "zh-Hant",
|
||||
Locale::ZhHans => "zh-Hans",
|
||||
Locale::Nl => "nl",
|
||||
Locale::Ar => "ar",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Locale::En => "English",
|
||||
Locale::Es => "Espa\u{00f1}ol",
|
||||
Locale::De => "Deutsch",
|
||||
Locale::Ja => "\u{65e5}\u{672c}\u{8a9e}",
|
||||
Locale::Ko => "\u{d55c}\u{ad6d}\u{c5b4}",
|
||||
Locale::Id => "Bahasa Indonesia",
|
||||
Locale::Fr => "Fran\u{00e7}ais",
|
||||
Locale::Ru => "\u{0420}\u{0443}\u{0441}\u{0441}\u{043a}\u{0438}\u{0439}",
|
||||
Locale::Pt => "Portugu\u{00ea}s",
|
||||
Locale::It => "Italiano",
|
||||
Locale::ZhHant => "\u{7e41}\u{9ad4}\u{4e2d}\u{6587}",
|
||||
Locale::ZhHans => "\u{7b80}\u{4f53}\u{4e2d}\u{6587}",
|
||||
Locale::Nl => "Nederlands",
|
||||
Locale::Ar => "\u{0627}\u{0644}\u{0639}\u{0631}\u{0628}\u{064a}\u{0629}",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn html_dir(self) -> &'static str {
|
||||
match self {
|
||||
Locale::Ar => "rtl",
|
||||
_ => "ltr",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mmdb_key(self) -> &'static str {
|
||||
match self {
|
||||
Locale::En => "en",
|
||||
Locale::Es => "es",
|
||||
Locale::De => "de",
|
||||
Locale::Ja => "ja",
|
||||
Locale::Fr => "fr",
|
||||
Locale::Ru => "ru",
|
||||
Locale::Pt => "pt-BR",
|
||||
Locale::ZhHans => "zh-CN",
|
||||
// These languages are not in the MMDB; fall back to English.
|
||||
Locale::Ko | Locale::Id | Locale::It | Locale::ZhHant | Locale::Nl | Locale::Ar => "en",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_tag(tag: &str) -> Option<Locale> {
|
||||
match tag {
|
||||
"en" => Some(Locale::En),
|
||||
"es" => Some(Locale::Es),
|
||||
"de" => Some(Locale::De),
|
||||
"ja" => Some(Locale::Ja),
|
||||
"ko" => Some(Locale::Ko),
|
||||
"id" => Some(Locale::Id),
|
||||
"fr" => Some(Locale::Fr),
|
||||
"ru" => Some(Locale::Ru),
|
||||
"pt" => Some(Locale::Pt),
|
||||
"it" => Some(Locale::It),
|
||||
"zh-Hant" => Some(Locale::ZhHant),
|
||||
"zh-Hans" => Some(Locale::ZhHans),
|
||||
"nl" => Some(Locale::Nl),
|
||||
"ar" => Some(Locale::Ar),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Catalog = HashMap<&'static str, &'static str>;
|
||||
|
||||
struct Translations {
|
||||
catalogs: HashMap<Locale, Catalog>,
|
||||
}
|
||||
|
||||
impl Translations {
|
||||
fn get<'a>(&self, locale: Locale, msgid: &'a str) -> &'a str {
|
||||
self.catalogs
|
||||
.get(&locale)
|
||||
.and_then(|cat| cat.get(msgid).copied())
|
||||
.unwrap_or(msgid)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_po(source: &'static str) -> Catalog {
|
||||
let mut catalog = HashMap::new();
|
||||
let mut current_msgid = String::new();
|
||||
let mut current_msgstr = String::new();
|
||||
let mut reading = Reading::None;
|
||||
|
||||
for line in source.lines() {
|
||||
let trimmed = line.trim();
|
||||
|
||||
if trimmed.is_empty() || trimmed.starts_with('#') {
|
||||
if reading == Reading::Msgstr && !current_msgid.is_empty() {
|
||||
insert_entry(&mut catalog, ¤t_msgid, ¤t_msgstr, source);
|
||||
current_msgid.clear();
|
||||
current_msgstr.clear();
|
||||
}
|
||||
reading = Reading::None;
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(rest) = trimmed.strip_prefix("msgid ") {
|
||||
if reading == Reading::Msgstr && !current_msgid.is_empty() {
|
||||
insert_entry(&mut catalog, ¤t_msgid, ¤t_msgstr, source);
|
||||
}
|
||||
current_msgid.clear();
|
||||
current_msgstr.clear();
|
||||
append_quoted(&mut current_msgid, rest);
|
||||
reading = Reading::Msgid;
|
||||
} else if let Some(rest) = trimmed.strip_prefix("msgstr ") {
|
||||
append_quoted(&mut current_msgstr, rest);
|
||||
reading = Reading::Msgstr;
|
||||
} else if trimmed.starts_with('"') {
|
||||
match reading {
|
||||
Reading::Msgid => append_quoted(&mut current_msgid, trimmed),
|
||||
Reading::Msgstr => append_quoted(&mut current_msgstr, trimmed),
|
||||
Reading::None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if reading == Reading::Msgstr && !current_msgid.is_empty() {
|
||||
insert_entry(&mut catalog, ¤t_msgid, ¤t_msgstr, source);
|
||||
}
|
||||
|
||||
catalog
|
||||
}
|
||||
|
||||
fn insert_entry(catalog: &mut Catalog, msgid: &str, msgstr: &str, source: &'static str) {
|
||||
if msgstr.is_empty() {
|
||||
return;
|
||||
}
|
||||
// Find the msgstr in the static source to get a &'static str reference.
|
||||
// This avoids allocating owned Strings at runtime.
|
||||
let id_static = find_in_static(source, msgid);
|
||||
let str_static = find_in_static(source, msgstr);
|
||||
if let (Some(id), Some(s)) = (id_static, str_static) {
|
||||
catalog.insert(id, s);
|
||||
}
|
||||
}
|
||||
|
||||
fn find_in_static(source: &'static str, needle: &str) -> Option<&'static str> {
|
||||
source
|
||||
.find(needle)
|
||||
.map(|pos| &source[pos..pos + needle.len()])
|
||||
}
|
||||
|
||||
fn append_quoted(target: &mut String, quoted: &str) {
|
||||
let s = quoted.trim();
|
||||
if s.len() >= 2 && s.starts_with('"') && s.ends_with('"') {
|
||||
let inner = &s[1..s.len() - 1];
|
||||
let unescaped = inner
|
||||
.replace("\\n", "\n")
|
||||
.replace("\\\"", "\"")
|
||||
.replace("\\\\", "\\");
|
||||
target.push_str(&unescaped);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Reading {
|
||||
None,
|
||||
Msgid,
|
||||
Msgstr,
|
||||
}
|
||||
|
||||
static TRANSLATIONS: LazyLock<Translations> = LazyLock::new(|| {
|
||||
let mut catalogs = HashMap::new();
|
||||
catalogs.insert(Locale::En, parse_po(include_str!("../locales/en.po")));
|
||||
catalogs.insert(Locale::Es, parse_po(include_str!("../locales/es.po")));
|
||||
catalogs.insert(Locale::De, parse_po(include_str!("../locales/de.po")));
|
||||
catalogs.insert(Locale::Ja, parse_po(include_str!("../locales/ja.po")));
|
||||
catalogs.insert(Locale::Ko, parse_po(include_str!("../locales/ko.po")));
|
||||
catalogs.insert(Locale::Id, parse_po(include_str!("../locales/id.po")));
|
||||
catalogs.insert(Locale::Fr, parse_po(include_str!("../locales/fr.po")));
|
||||
catalogs.insert(Locale::Ru, parse_po(include_str!("../locales/ru.po")));
|
||||
catalogs.insert(Locale::Pt, parse_po(include_str!("../locales/pt.po")));
|
||||
catalogs.insert(Locale::It, parse_po(include_str!("../locales/it.po")));
|
||||
catalogs.insert(
|
||||
Locale::ZhHant,
|
||||
parse_po(include_str!("../locales/zh-Hant.po")),
|
||||
);
|
||||
catalogs.insert(
|
||||
Locale::ZhHans,
|
||||
parse_po(include_str!("../locales/zh-Hans.po")),
|
||||
);
|
||||
catalogs.insert(Locale::Nl, parse_po(include_str!("../locales/nl.po")));
|
||||
catalogs.insert(Locale::Ar, parse_po(include_str!("../locales/ar.po")));
|
||||
Translations { catalogs }
|
||||
});
|
||||
|
||||
pub fn translate(locale: Locale, msgid: &str) -> &str {
|
||||
TRANSLATIONS.get(locale, msgid)
|
||||
}
|
||||
|
||||
pub fn negotiate_locale(headers: &HeaderMap) -> Locale {
|
||||
let accept = match headers.get("Accept-Language").and_then(|v| v.to_str().ok()) {
|
||||
Some(v) => v,
|
||||
None => return Locale::En,
|
||||
};
|
||||
|
||||
let mut candidates: Vec<(&str, f32)> = Vec::new();
|
||||
|
||||
for part in accept.split(',') {
|
||||
let part = part.trim();
|
||||
if let Some((lang, q_part)) = part.split_once(";q=") {
|
||||
let quality: f32 = q_part.trim().parse().unwrap_or(0.0);
|
||||
candidates.push((lang.trim(), quality));
|
||||
} else {
|
||||
candidates.push((part, 1.0));
|
||||
}
|
||||
}
|
||||
|
||||
candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
|
||||
|
||||
for (tag, _) in &candidates {
|
||||
if let Some(locale) = match_language_tag(tag) {
|
||||
return locale;
|
||||
}
|
||||
}
|
||||
|
||||
Locale::En
|
||||
}
|
||||
|
||||
fn match_language_tag(tag: &str) -> Option<Locale> {
|
||||
let tag_lower = tag.to_ascii_lowercase();
|
||||
|
||||
// Exact matches (case-insensitive)
|
||||
match tag_lower.as_str() {
|
||||
"zh-hant" | "zh-tw" | "zh-hk" | "zh-mo" => return Some(Locale::ZhHant),
|
||||
"zh-hans" | "zh-cn" | "zh-sg" | "zh-my" => return Some(Locale::ZhHans),
|
||||
"pt-br" | "pt" => return Some(Locale::Pt),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Try primary subtag
|
||||
let primary = tag_lower.split('-').next().unwrap_or(&tag_lower);
|
||||
match primary {
|
||||
"en" => Some(Locale::En),
|
||||
"es" => Some(Locale::Es),
|
||||
"de" => Some(Locale::De),
|
||||
"ja" => Some(Locale::Ja),
|
||||
"ko" => Some(Locale::Ko),
|
||||
"id" | "ms" => Some(Locale::Id),
|
||||
"fr" => Some(Locale::Fr),
|
||||
"ru" => Some(Locale::Ru),
|
||||
"pt" => Some(Locale::Pt),
|
||||
"it" => Some(Locale::It),
|
||||
"zh" => Some(Locale::ZhHans),
|
||||
"nl" => Some(Locale::Nl),
|
||||
"ar" => Some(Locale::Ar),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn locale_tag_roundtrip() {
|
||||
for locale in Locale::ALL {
|
||||
let tag = locale.tag();
|
||||
let parsed = Locale::from_tag(tag).expect("tag should parse back");
|
||||
assert_eq!(*locale, parsed);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn locale_names_are_non_empty() {
|
||||
for locale in Locale::ALL {
|
||||
assert!(!locale.name().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn translate_english_returns_english() {
|
||||
let result = translate(Locale::En, "FAQ");
|
||||
assert_eq!(result, "FAQ");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn translate_unknown_msgid_returns_msgid() {
|
||||
let result = translate(Locale::En, "this-does-not-exist");
|
||||
assert_eq!(result, "this-does-not-exist");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn translate_non_english_returns_translated() {
|
||||
let result = translate(Locale::Es, "Country");
|
||||
assert!(!result.is_empty());
|
||||
// Spanish for Country should not be "Country"
|
||||
assert_ne!(result, "Country");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiate_locale_empty_header() {
|
||||
let headers = HeaderMap::new();
|
||||
assert_eq!(negotiate_locale(&headers), Locale::En);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiate_locale_japanese() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"Accept-Language",
|
||||
"ja,en-US;q=0.9,en;q=0.8".parse().unwrap(),
|
||||
);
|
||||
assert_eq!(negotiate_locale(&headers), Locale::Ja);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiate_locale_chinese_traditional() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"Accept-Language",
|
||||
"zh-TW,zh;q=0.9,en;q=0.8".parse().unwrap(),
|
||||
);
|
||||
assert_eq!(negotiate_locale(&headers), Locale::ZhHant);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiate_locale_chinese_simplified() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("Accept-Language", "zh-CN,zh;q=0.9".parse().unwrap());
|
||||
assert_eq!(negotiate_locale(&headers), Locale::ZhHans);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiate_locale_portuguese() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"Accept-Language",
|
||||
"pt-BR,pt;q=0.9,en;q=0.8".parse().unwrap(),
|
||||
);
|
||||
assert_eq!(negotiate_locale(&headers), Locale::Pt);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiate_locale_arabic() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("Accept-Language", "ar,en;q=0.5".parse().unwrap());
|
||||
assert_eq!(negotiate_locale(&headers), Locale::Ar);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiate_locale_quality_ordering() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"Accept-Language",
|
||||
"en;q=0.5,de;q=0.9,fr;q=0.7".parse().unwrap(),
|
||||
);
|
||||
assert_eq!(negotiate_locale(&headers), Locale::De);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negotiate_locale_unknown_falls_back_to_english() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("Accept-Language", "xx-YY,zz;q=0.5".parse().unwrap());
|
||||
assert_eq!(negotiate_locale(&headers), Locale::En);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn arabic_is_rtl() {
|
||||
assert_eq!(Locale::Ar.html_dir(), "rtl");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn english_is_ltr() {
|
||||
assert_eq!(Locale::En.html_dir(), "ltr");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mmdb_key_mapping() {
|
||||
assert_eq!(Locale::En.mmdb_key(), "en");
|
||||
assert_eq!(Locale::De.mmdb_key(), "de");
|
||||
assert_eq!(Locale::Pt.mmdb_key(), "pt-BR");
|
||||
assert_eq!(Locale::ZhHans.mmdb_key(), "zh-CN");
|
||||
assert_eq!(Locale::Ko.mmdb_key(), "en");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_locales_have_translations() {
|
||||
for locale in Locale::ALL {
|
||||
let result = translate(*locale, "What is my IP address?");
|
||||
assert!(
|
||||
!result.is_empty(),
|
||||
"locale {:?} has no translation for title",
|
||||
locale
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_po_handles_multiline() {
|
||||
// We cannot use parse_po directly with a non-static str for the
|
||||
// static-reference optimization, but we can verify the PO files load.
|
||||
let _ = translate(Locale::Es, "FAQ");
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ pub mod config;
|
||||
pub mod db;
|
||||
pub mod error;
|
||||
pub mod handlers;
|
||||
pub mod i18n;
|
||||
pub mod middleware;
|
||||
pub mod models;
|
||||
pub mod routes;
|
||||
|
||||
@@ -424,6 +424,22 @@ pub fn get_en_name(names: &Option<Names>) -> &str {
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn get_localized_name<'a>(names: &'a Option<Names>, mmdb_key: &str) -> &'a str {
|
||||
names
|
||||
.as_ref()
|
||||
.and_then(|n| match mmdb_key {
|
||||
"de" => n.german.as_deref().or(n.english.as_deref()),
|
||||
"es" => n.spanish.as_deref().or(n.english.as_deref()),
|
||||
"fr" => n.french.as_deref().or(n.english.as_deref()),
|
||||
"ja" => n.japanese.as_deref().or(n.english.as_deref()),
|
||||
"pt-BR" => n.brazilian_portuguese.as_deref().or(n.english.as_deref()),
|
||||
"ru" => n.russian.as_deref().or(n.english.as_deref()),
|
||||
"zh-CN" => n.simplified_chinese.as_deref().or(n.english.as_deref()),
|
||||
_ => n.english.as_deref(),
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
+7
-2
@@ -1,14 +1,18 @@
|
||||
use axum::Router;
|
||||
use axum::middleware;
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
|
||||
use crate::handlers::{self, AppState};
|
||||
use crate::middleware::security_headers;
|
||||
use crate::static_assets;
|
||||
|
||||
pub fn build_router(state: AppState) -> Router {
|
||||
Router::new()
|
||||
let localized = Router::new()
|
||||
.route("/", get(handlers::root))
|
||||
.with_state(state.clone());
|
||||
|
||||
Router::new()
|
||||
.route("/", get(handlers::root_redirect))
|
||||
.route("/health", get(handlers::health))
|
||||
.route("/openapi.json", get(handlers::openapi_json))
|
||||
.route("/robots.txt", get(handlers::robots_txt))
|
||||
@@ -24,6 +28,7 @@ pub fn build_router(state: AppState) -> Router {
|
||||
.route("/hosting", get(handlers::hosting_handler))
|
||||
.route("/tor", get(handlers::tor_handler))
|
||||
.route("/static/{*path}", get(static_assets::serve_static))
|
||||
.nest("/{lang}", localized)
|
||||
.fallback(handlers::not_found)
|
||||
.layer(middleware::from_fn(security_headers))
|
||||
.with_state(state)
|
||||
|
||||
Reference in New Issue
Block a user