[{"data":1,"prerenderedAt":809},["ShallowReactive",2],{"$fWGHXGUXFJdLQ6hnfo6TnR3IQdUoTYVSuz2C_ocLJ-w0":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":796,"editUrl":803,"prev":804,"next":807},"techniques\u002Fi18n","Internationalization (i18n)","Load message catalogs JSON and resolve translated keys at runtime with I18nService.","---\ntitle: Internationalization (i18n)\ndescription: Load message catalogs JSON and resolve translated keys at runtime with I18nService.\n---\n\n# Internationalization (i18n)\n\n`ferrite-i18n` loads catalog files and resolves translated message keys at runtime, with an `I18nService` you inject anywhere.\n\n## Enable it\n\n```cargo\nfr add i18n\n```\n\n```rust\n#[module(imports = [I18nModuleImpl])]\npub struct AppModule;\n```\n\nRequires the `I18n` feature flag:\n\n```toml\nferrite = { version = \"=0.1.0\", features = [\"I18n\"] }\n```\n\n## Catalogs\n\nPlace JSON catalogs where the loader looks (default `locales\u002F`), named by locale code:\n\n```\nlocales\u002F\n  en.json   { \"welcome\": \"Welcome, {name}!\" }\n  vi.json   { \"welcome\": \"Xin chào, {name}!\" }\n  fr.json   { \"welcome\": \"Bienvenue, {name}!\" }\n```\n\n| Env | Default |\n| --- | --- |\n| `LOCALE_DIR` | `locales` |\n| `DEFAULT_LOCALE` | `en` |\n\n`I18nModuleImpl` implements `OnApplicationBootstrap` and loads all catalogs at startup.\n\n## `I18nService`\n\nInject it wherever translations are needed:\n\n```rust\nuse ferrite_i18n::I18nService;\n\n#[injectable]\npub struct MailService {\n    i18n: I18nService,\n}\n\nimpl MailService {\n    #[inject]\n    pub fn new(i18n: I18nService) -> Self {\n        Self { i18n }\n    }\n\n    pub fn subject(&self, locale: &str, name: &str) -> String {\n        self.i18n.format(\"mail.welcome\", locale, &[(\"name\", name)])\n    }\n}\n```\n\n| Method | Purpose |\n| --- | --- |\n| `resolve(key, locale)` | Look up a key |\n| `format(key, locale, params)` | Look up + substitute `{param}` placeholders |\n| `has_key(key, locale)` | Presence check |\n| `available_locales()` | Loaded locales |\n| `default_locale()` | The configured default |\n\n`format` falls back to the default locale when a key is missing in the requested locale.\n\n## From HTTP\n\nResolve the locale from a header or query then pass it along:\n\n```rust\nuse axum::extract::Query;\n\n#[get(\"\u002Fgreet\")]\npub async fn greet(\n    &self,\n    Query(params): Query\u003CHashMap\u003CString, String>>,\n    headers: HeaderMap,\n) -> Result\u003CJson\u003CString>, HttpError> {\n    let locale = headers\n        .get(\"accept-language\")\n        .and_then(|v| v.to_str().ok())\n        .unwrap_or(\"en\");\n    Ok(Json(self.i18n.format(\"welcome\", locale, &[(\"name\", \"Ada\")]).into()))\n}\n```\n\n## Next steps\n\n- [Configuration](\u002Fdocs\u002Foverview\u002Fconfiguration).\n- [CLI reference](\u002Fdocs\u002Fecosystem\u002Fcli-reference).",{"frontmatter":9,"meta":10,"nodes":31},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28],{"id":17,"depth":13,"text":18},"enable-it","Enable it",{"id":20,"depth":13,"text":21},"catalogs","Catalogs",{"id":23,"depth":13,"text":24},"i18nservice","I18nService",{"id":26,"depth":13,"text":27},"from-http","From HTTP",{"id":29,"depth":13,"text":30},"next-steps","Next steps",[32,36,47,50,59,104,111,135,137,144,167,211,220,224,227,435,503,508,510,513,777,779],[33,34,5],"h1",{"id":35},"internationalization-i18n",[37,38,39,43,44,46],"p",{},[40,41,42],"code",{},"ferrite-i18n"," loads catalog files and resolves translated message keys at runtime, with an ",[40,45,24],{}," you inject anywhere.",[48,49,18],"h2",{"id":17},[51,52,55],"pre",{"language":53,"class":54},"cargo","shiki dark:github-dark",[40,56,58],{"class":57},"language-cargo","fr add i18n",[51,60,63],{"language":61,"class":62},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[40,64,66,89,90],{"class":65},"language-rust",[67,68,71,75,79,82,86],"span",{"class":69,"style":70},"line","display: inline",[67,72,74],{"style":73},"color:#24292E;--shiki-dark:#E1E4E8","#[module(imports ",[67,76,78],{"style":77},"color:#D73A49;--shiki-dark:#F97583","=",[67,80,81],{"style":73}," [",[67,83,85],{"style":84},"color:#6F42C1;--shiki-dark:#B392F0","I18nModuleImpl",[67,87,88],{"style":73},"])]","\n",[67,91,92,95,98,101],{"class":69,"style":70},[67,93,94],{"style":77},"pub",[67,96,97],{"style":77}," struct",[67,99,100],{"style":84}," AppModule",[67,102,103],{"style":73},";",[37,105,106,107,110],{},"Requires the ",[40,108,109],{},"I18n"," feature flag:",[51,112,114],{"language":113,"class":62},"toml",[40,115,117],{"class":116},"language-toml",[67,118,119,122,126,129,132],{"class":69,"style":70},[67,120,121],{"style":73},"ferrite = { version = ",[67,123,125],{"style":124},"color:#032F62;--shiki-dark:#9ECBFF","\"=0.1.0\"",[67,127,128],{"style":73},", features = [",[67,130,131],{"style":124},"\"I18n\"",[67,133,134],{"style":73},"] }",[48,136,21],{"id":20},[37,138,139,140,143],{},"Place JSON catalogs where the loader looks (default ",[40,141,142],{},"locales\u002F","), named by locale code:",[51,145,146],{"class":62},[40,147,148,89,152,89,157,89,162],{},[67,149,150],{"class":69,"style":70},[67,151,142],{},[67,153,154],{"class":69,"style":70},[67,155,156],{},"  en.json   { \"welcome\": \"Welcome, {name}!\" }",[67,158,159],{"class":69,"style":70},[67,160,161],{},"  vi.json   { \"welcome\": \"Xin chào, {name}!\" }",[67,163,164],{"class":69,"style":70},[67,165,166],{},"  fr.json   { \"welcome\": \"Bienvenue, {name}!\" }",[168,169,170,183],"table",{},[171,172,173],"thead",{},[174,175,176,180],"tr",{},[177,178,179],"th",{},"Env",[177,181,182],{},"Default",[184,185,186,199],"tbody",{},[174,187,188,194],{},[189,190,191],"td",{},[40,192,193],{},"LOCALE_DIR",[189,195,196],{},[40,197,198],{},"locales",[174,200,201,206],{},[189,202,203],{},[40,204,205],{},"DEFAULT_LOCALE",[189,207,208],{},[40,209,210],{},"en",[37,212,213,215,216,219],{},[40,214,85],{}," implements ",[40,217,218],{},"OnApplicationBootstrap"," and loads all catalogs at startup.",[48,221,222],{"id":23},[40,223,24],{},[37,225,226],{},"Inject it wherever translations are needed:",[51,228,229],{"language":61,"class":62},[40,230,231,89,246,89,248,89,253,89,265,89,279,89,284,89,286,89,295,89,300,89,330,89,338,89,343,89,345,89,392,89,427,89,431],{"class":65},[67,232,233,236,239,242,244],{"class":69,"style":70},[67,234,235],{"style":77},"use",[67,237,238],{"style":84}," ferrite_i18n",[67,240,241],{"style":77},"::",[67,243,24],{"style":84},[67,245,103],{"style":73},[67,247],{"class":69,"style":70},[67,249,250],{"class":69,"style":70},[67,251,252],{"style":73},"#[injectable]",[67,254,255,257,259,262],{"class":69,"style":70},[67,256,94],{"style":77},[67,258,97],{"style":77},[67,260,261],{"style":84}," MailService",[67,263,264],{"style":73}," {",[67,266,267,270,273,276],{"class":69,"style":70},[67,268,269],{"style":73},"    i18n",[67,271,272],{"style":77},":",[67,274,275],{"style":84}," I18nService",[67,277,278],{"style":73},",",[67,280,281],{"class":69,"style":70},[67,282,283],{"style":73},"}",[67,285],{"class":69,"style":70},[67,287,288,291,293],{"class":69,"style":70},[67,289,290],{"style":77},"impl",[67,292,261],{"style":84},[67,294,264],{"style":73},[67,296,297],{"class":69,"style":70},[67,298,299],{"style":73},"    #[inject]",[67,301,302,305,308,311,314,316,318,321,324,328],{"class":69,"style":70},[67,303,304],{"style":77},"    pub",[67,306,307],{"style":77}," fn",[67,309,310],{"style":84}," new",[67,312,313],{"style":73},"(i18n",[67,315,272],{"style":77},[67,317,275],{"style":84},[67,319,320],{"style":73},") ",[67,322,323],{"style":77},"->",[67,325,327],{"style":326},"color:#005CC5;--shiki-dark:#79B8FF"," Self",[67,329,264],{"style":73},[67,331,332,335],{"class":69,"style":70},[67,333,334],{"style":326},"        Self",[67,336,337],{"style":73}," { i18n }",[67,339,340],{"class":69,"style":70},[67,341,342],{"style":73},"    }",[67,344],{"class":69,"style":70},[67,346,347,349,351,354,357,360,363,366,368,371,374,377,379,381,383,385,387,390],{"class":69,"style":70},[67,348,304],{"style":77},[67,350,307],{"style":77},[67,352,353],{"style":84}," subject",[67,355,356],{"style":73},"(",[67,358,359],{"style":77},"&",[67,361,362],{"style":326},"self",[67,364,365],{"style":73},", locale",[67,367,272],{"style":77},[67,369,370],{"style":77}," &",[67,372,373],{"style":84},"str",[67,375,376],{"style":73},", name",[67,378,272],{"style":77},[67,380,370],{"style":77},[67,382,373],{"style":84},[67,384,320],{"style":73},[67,386,323],{"style":77},[67,388,389],{"style":84}," String",[67,391,264],{"style":73},[67,393,394,397,400,403,405,408,410,413,416,418,421,424],{"class":69,"style":70},[67,395,396],{"style":326},"        self",[67,398,399],{"style":77},".",[67,401,402],{"style":73},"i18n",[67,404,399],{"style":77},[67,406,407],{"style":84},"format",[67,409,356],{"style":73},[67,411,412],{"style":124},"\"mail.welcome\"",[67,414,415],{"style":73},", locale, ",[67,417,359],{"style":77},[67,419,420],{"style":73},"[(",[67,422,423],{"style":124},"\"name\"",[67,425,426],{"style":73},", name)])",[67,428,429],{"class":69,"style":70},[67,430,342],{"style":73},[67,432,433],{"class":69,"style":70},[67,434,283],{"style":73},[168,436,437,447],{},[171,438,439],{},[174,440,441,444],{},[177,442,443],{},"Method",[177,445,446],{},"Purpose",[184,448,449,459,473,483,493],{},[174,450,451,456],{},[189,452,453],{},[40,454,455],{},"resolve(key, locale)",[189,457,458],{},"Look up a key",[174,460,461,466],{},[189,462,463],{},[40,464,465],{},"format(key, locale, params)",[189,467,468,469,472],{},"Look up + substitute ",[40,470,471],{},"{param}"," placeholders",[174,474,475,480],{},[189,476,477],{},[40,478,479],{},"has_key(key, locale)",[189,481,482],{},"Presence check",[174,484,485,490],{},[189,486,487],{},[40,488,489],{},"available_locales()",[189,491,492],{},"Loaded locales",[174,494,495,500],{},[189,496,497],{},[40,498,499],{},"default_locale()",[189,501,502],{},"The configured default",[37,504,505,507],{},[40,506,407],{}," falls back to the default locale when a key is missing in the requested locale.",[48,509,27],{"id":26},[37,511,512],{},"Resolve the locale from a header or query then pass it along:",[51,514,515],{"language":61,"class":62},[40,516,517,89,536,89,538,89,549,89,563,89,572,89,604,89,616,89,643,89,656,89,672,89,708,89,723,89,773],{"class":65},[67,518,519,521,524,526,529,531,534],{"class":69,"style":70},[67,520,235],{"style":77},[67,522,523],{"style":84}," axum",[67,525,241],{"style":77},[67,527,528],{"style":84},"extract",[67,530,241],{"style":77},[67,532,533],{"style":84},"Query",[67,535,103],{"style":73},[67,537],{"class":69,"style":70},[67,539,540,543,546],{"class":69,"style":70},[67,541,542],{"style":73},"#[get(",[67,544,545],{"style":124},"\"\u002Fgreet\"",[67,547,548],{"style":73},")]",[67,550,551,553,556,558,561],{"class":69,"style":70},[67,552,94],{"style":77},[67,554,555],{"style":77}," async",[67,557,307],{"style":77},[67,559,560],{"style":84}," greet",[67,562,356],{"style":73},[67,564,565,568,570],{"class":69,"style":70},[67,566,567],{"style":77},"    &",[67,569,362],{"style":326},[67,571,278],{"style":73},[67,573,574,577,580,582,585,588,591,593,596,599,601],{"class":69,"style":70},[67,575,576],{"style":84},"    Query",[67,578,579],{"style":73},"(params)",[67,581,272],{"style":77},[67,583,584],{"style":84}," Query",[67,586,587],{"style":73},"\u003C",[67,589,590],{"style":84},"HashMap",[67,592,587],{"style":73},[67,594,595],{"style":84},"String",[67,597,598],{"style":73},", ",[67,600,595],{"style":84},[67,602,603],{"style":73},">>,",[67,605,606,609,611,614],{"class":69,"style":70},[67,607,608],{"style":73},"    headers",[67,610,272],{"style":77},[67,612,613],{"style":84}," HeaderMap",[67,615,278],{"style":73},[67,617,618,620,622,625,627,630,632,634,637,640],{"class":69,"style":70},[67,619,320],{"style":73},[67,621,323],{"style":77},[67,623,624],{"style":84}," Result",[67,626,587],{"style":73},[67,628,629],{"style":84},"Json",[67,631,587],{"style":73},[67,633,595],{"style":84},[67,635,636],{"style":73},">, ",[67,638,639],{"style":84},"HttpError",[67,641,642],{"style":73},"> {",[67,644,645,648,651,653],{"class":69,"style":70},[67,646,647],{"style":77},"    let",[67,649,650],{"style":73}," locale ",[67,652,78],{"style":77},[67,654,655],{"style":73}," headers",[67,657,658,661,664,666,669],{"class":69,"style":70},[67,659,660],{"style":77},"        .",[67,662,663],{"style":84},"get",[67,665,356],{"style":73},[67,667,668],{"style":124},"\"accept-language\"",[67,670,671],{"style":73},")",[67,673,674,676,679,681,684,687,689,692,694,697,700,702,705],{"class":69,"style":70},[67,675,660],{"style":77},[67,677,678],{"style":84},"and_then",[67,680,356],{"style":73},[67,682,683],{"style":77},"|",[67,685,686],{"style":73},"v",[67,688,683],{"style":77},[67,690,691],{"style":73}," v",[67,693,399],{"style":77},[67,695,696],{"style":84},"to_str",[67,698,699],{"style":73},"()",[67,701,399],{"style":77},[67,703,704],{"style":84},"ok",[67,706,707],{"style":73},"())",[67,709,710,712,715,717,720],{"class":69,"style":70},[67,711,660],{"style":77},[67,713,714],{"style":84},"unwrap_or",[67,716,356],{"style":73},[67,718,719],{"style":124},"\"en\"",[67,721,722],{"style":73},");",[67,724,725,728,730,732,734,736,738,740,742,744,746,749,751,753,755,757,759,762,765,767,770],{"class":69,"style":70},[67,726,727],{"style":84},"    Ok",[67,729,356],{"style":73},[67,731,629],{"style":84},[67,733,356],{"style":73},[67,735,362],{"style":326},[67,737,399],{"style":77},[67,739,402],{"style":73},[67,741,399],{"style":77},[67,743,407],{"style":84},[67,745,356],{"style":73},[67,747,748],{"style":124},"\"welcome\"",[67,750,415],{"style":73},[67,752,359],{"style":77},[67,754,420],{"style":73},[67,756,423],{"style":124},[67,758,598],{"style":73},[67,760,761],{"style":124},"\"Ada\"",[67,763,764],{"style":73},")])",[67,766,399],{"style":77},[67,768,769],{"style":84},"into",[67,771,772],{"style":73},"()))",[67,774,775],{"class":69,"style":70},[67,776,283],{"style":73},[48,778,30],{"id":29},[780,781,782,790],"ul",{},[783,784,785,399],"li",{},[786,787,789],"a",{"href":788},"\u002Fdocs\u002Foverview\u002Fconfiguration","Configuration",[783,791,792,399],{},[786,793,795],{"href":794},"\u002Fdocs\u002Fecosystem\u002Fcli-reference","CLI reference",{"title":12,"searchDepth":13,"depth":14,"links":797},[798,799,800,801,802],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24},{"id":26,"depth":13,"text":27},{"id":29,"depth":13,"text":30},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Ftechniques\u002Fi18n.md",{"title":805,"slug":806},"CQRS","techniques\u002Fcqrs",{"title":795,"slug":808},"ecosystem\u002Fcli-reference",1789565440201]