Crate twine[][src]

Rust Latest Version LOC Dependency Status

Library for internationalization using the Twine file format.


  1. You need to add twine to your [build-dependencies] in Cargo.toml.

  2. Create (or edit) your file:

fn main() {
    twine::build_translations(&["translations.ini"], "");
  1. You need an INI file with your translations. Example with translations.ini:
    en = Ruin a band name by translating it in French
    fr = Ruiner le nom d'un groupe en le traduisant en français
    en = Tool
    fr = Outil
    en = The Doors
    fr = Les portes
    en = Rage Against the Machine
    en-gb = Wrath Against the Machine
    fr = Colère contre la machine
    en = The Jackson 5
    fr = Les 5 fils de Jack
    en = %s, %@!
    fr = %s, %@ !
    en = %.0f%
    fr = %.0f %
    en = %x
    fr = %#X
  1. Now in your project you can use the macro t! to translate anything:
// you need to include the generated file somewhere
include!(concat!(env!("OUT_DIR"), "/"));

fn main() {
    // use "" if there is no localization
    let lang = Lang::Fr("be");

    // will output "Ruiner le nom d'un groupe en le traduisant en français"
    t!(app_ruin_the_band => lang);

    // using formatted arguments, this will output "73 %"
    t!(format_percentage, 73.02f32 => lang);

Implementation Notes

All translation keys must have all the languages of all the keys. For example, if all your keys have translations for en and fr, if one key has only en, it will fail to compile.

Localized translation can be provided and will be used if available. Otherwise it will fallback to the default translation for that language.

Any typo in the key will make the compilation fail. Missing format arguments will also make the compilation fail.


  • serde: when this feature is activated you will need to add serde to your dependencies and the Lang enum generated implements Serialize and Deserialize.


This work is dual-licensed under Apache 2.0 and MIT. You can choose between one of them if you use this work.



Generate the t!() macro based on the provided list of paths to Twine INI translation files.


Generate the t!() macro based on the provided list of readers containing Twine INI translations.


Generate the t!() macro based on the provided list of &str containing Twine INI translations.