Code Monkey home page Code Monkey logo

rust-language-tags's Introduction

rust-language-tags

Build Status Coverage Status MIT licensed crates.io

Language tags can be used identify human languages, scripts e.g. Latin script, countries and other regions.

Language tags are defined in BCP47, an introduction is "Language tags in HTML and XML" by the W3C. They are commonly used in HTML and HTTP Content-Language and Accept-Language header fields.

This package currently supports parsing (fully conformant parser), formatting and comparing language tags.

Examples

Create a simple language tag representing the French language as spoken in Belgium and print it:

use language_tags::LanguageTag;
let mut langtag: LanguageTag = Default::default();
langtag.language = Some("fr".to_owned());
langtag.region = Some("BE".to_owned());
assert_eq!(format!("{}", langtag), "fr-BE");

Parse a tag representing a special type of English specified by private agreement:

use language_tags::LanguageTag;
let langtag: LanguageTag = "en-x-twain".parse().unwrap();
assert_eq!(format!("{}", langtag.language.unwrap()), "en");
assert_eq!(format!("{:?}", langtag.privateuse), "[\"twain\"]");

You can check for equality, but more often you should test if two tags match. In this example we check if the resource in German language is suitable for a user from Austria. While people speaking Austrian German normally understand standard German the opposite is not always true. So the resource can be presented to the user but if the resource was in de-AT and a user asked for a representation in de the request should be rejected.

use language_tags::LanguageTag;
let mut langtag1: LanguageTag = Default::default();
langtag1.language = Some("de".to_owned());
langtag1.region = Some("AT".to_owned());
let mut langtag2: LanguageTag = Default::default();
langtag2.language = Some("de".to_owned());
assert!(langtag2.matches(&langtag1));

There is also the langtag! macro for creating language tags.

rust-language-tags's People

Contributors

pyfisch avatar nox avatar jturner314 avatar mgeisler avatar

Watchers

Zibi Braniecki avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.