rust
58 lines · 8 steps
A custom Accept-Language extractor in Axum
Build a reusable Axum extractor that reads a client's Accept-Language header and resolves it to a supported translation.
Explained by
highlit
1use axum::{
2 extract::{FromRef, FromRequestParts, State},
3 http::{header::ACCEPT_LANGUAGE, request::Parts, StatusCode},
4 response::{IntoResponse, Response},
5 Json,
6};
7use serde_json::json;
8use std::{collections::HashMap, sync::Arc};
9
10#[derive(Clone)]
11pub struct Translations {
12 tables: Arc<HashMap<String, HashMap<String, String>>>,
13 fallback: String,
14}
15
16impl Translations {
17 pub fn message(&self, lang: &str, key: &str) -> String {
18 self.tables
19 .get(lang)
20 .and_then(|t| t.get(key))
21 .or_else(|| self.tables.get(&self.fallback).and_then(|t| t.get(key)))
22 .cloned()
23 .unwrap_or_else(|| key.to_string())
24 }
25}
26
27pub struct PreferredLang(pub String);
28
29impl<S> FromRequestParts<S> for PreferredLang
30where
31 Translations: FromRef<S>,
32 S: Send + Sync,
33{
34 type Rejection = std::convert::Infallible;
35
36 async fn from_request_parts(parts: &Parts, state: &S) -> Result<Self, Self::Rejection> {
37 let translations = Translations::from_ref(state);
38 let chosen = parts
39 .headers
40 .get(ACCEPT_LANGUAGE)
41 .and_then(|v| v.to_str().ok())
42 .into_iter()
43 .flat_map(|raw| raw.split(','))
44 .map(|tag| tag.trim().split(';').next().unwrap_or("").trim())
45 .map(|tag| tag.split('-').next().unwrap_or(tag).to_lowercase())
46 .find(|tag| translations.tables.contains_key(tag))
47 .unwrap_or_else(|| translations.fallback.clone());
48 Ok(PreferredLang(chosen))
49 }
50}
51
52pub async fn get_widget(
53 PreferredLang(lang): PreferredLang,
54 State(translations): State<Translations>,
55) -> Response {
56 let body = json!({ "error": translations.message(&lang, "widget.not_found") });
57 (StatusCode::NOT_FOUND, Json(body)).into_response()
58}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Implementing FromRequestParts lets you inject derived request data as a first-class handler parameter.
- 2Bounding on FromRef keeps an extractor decoupled from the concrete application state type.
- 3Chaining iterator adapters parses and normalizes header lists into a single resolved value cleanly.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
rust
use std::collections::VecDeque; use std::sync::{Arc, Condvar, Mutex}; use std::time::{Duration, Instant};
Building a counting semaphore in Rust
concurrency
synchronization
condition-variable
Advanced
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-custom-accept-language-extractor-in-axum-explained-rust-4730/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.