GithubHelp home page GithubHelp logo

rust-locale / locale_config Goto Github PK

View Code? Open in Web Editor NEW
12.0 4.0 8.0 868 KB

Rust crate to maintain locale preferences for process and thread and initialising them by inspecting the system for user preference.

License: MIT License

Shell 1.98% Rust 98.02%

locale_config's Introduction

TravisCI Build Status AppVeyor Build Status Crates.io Version Docs.rs

locale_config

Remembers locale configuration per-thread and per-process and initializes the values by inspecting the system for user preferences.

Installation

You can depend on this library by adding locale_config to your Cargo dependencies:

[dependencies]
locale_config = "*"

Usually it is not recommended to depend on *, but in this case it is important that incompatible version requirements don't cause multiple versions to be pulled in the final binary, so I do recommend it here and promise I will maintain good compatibility. Just please don't add traits to the types defined here to avoid conflicts with potential future methods.

Using

Usually you want to use this indirectly via a localization crate like locale. However if you need to work with the identifier itself, or you need to override it, use

Locale::current()

to find what you should be using at any given point in the application,

Locale::set_current()

to override it for current thread and

Locale::set_global_default()

to override it for new threads.

In case you need to access the initial value, you'll find it under

Locale::user_default()

The value may contain language tags specific for various localization aspects, called categories, and fallbacks. The Locale::tags_for method will take care of selecting relevant tags for you. For preferred language of translations, use

Locale::current().tags_for("messages")

For formatting, use categories "numeric" for numbers, "time" for date and time and "monetary" for money amounts. And use "collate" for collation.

Note that this crate does not itself provide any translation, formatting nor collation functionality. Formatting and collation will be provided by locale crate, translation has multiple available implementations.

See full documentation on Docs.rs or github.

Supported systems

  • Unix: Using the POSIX standard environment variables LANG, LC_* and LANGUAGES. The variables are recognized on all systems and take precedence on most of them.

  • Windows: Vista and newer

    • Uses API available from Vista and Server 2008 only.
    • The GetUserPreferredUILanguages is only available for desktop, but not store applications. Store applications should have equivalent functionality, but I didn't try accessing it from Rust yet.
    • Customization to individual locale elements done in “Regional and Language options” (digits, calendar, decimal and thousand separator etc.) are not detected (yet).
    • Not well tested.
  • OS X: Reads setting from NSLocale, can be overridden by setting the Unix environment variables.

  • CGI: The HTTP_ACCEPT_LANGUAGE environment variable is used if detected. Hopefully it is specific enough to the CGI environment that it can be used whenever detected.

Changelog

0.3.0

  • Support OS X NSLocale. Thanks Sophie Tauchert (@999eagle).

0.2.3

  • Try support getting locale in emscripten targets in browser. Unfortunately the emscripten targets seem to have broken in cross meanwhile, so they are not being tested.
  • Update to winapi 0.3.
  • Update ro regex 1.0.

0.2.2

  • Update dependencies: regex 0.2.

0.2.1

  • Interpret some overrides that can be set on Windows in Region and Language dialog, namely: group, decimal and list separators, first day of week, 12/24-hour time, measurement system, (decimal) number system, to an extent negative monetary value format (only whether to use parenthesized format or not) and to an extent date format (if ISO-8601 variant is selected).

0.2.0

  • Changed error handling to proper error type.

0.1.1

  • Added basic Windows support.

0.1.0

  • Initial version, with Unix and CGI support.

locale_config's People

Contributors

999eagle avatar eijebong avatar ignatenkobrain avatar jan-hudec avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

locale_config's Issues

Not always get the "plain" locale

fn get_locales() -> Vec<String> {
   locale_config::Locale::current()
        .tags_for("messages")
        .map(|l| l.to_string()).collect()
}
no environment        =>    ["en-US", "en"]  
LANG=en_US.utf8       =>    ["en-US", "en"]
LC_ALL=en_US.utf8     =>    ["en-US"]                   expected   ["en-US", "en"]
LANG=el_GR.utf8       =>    ["el-GR", "en-US", "en"]    expected   ["el-GR", "el", "en-US", "en"]
LC_ALL=el_GR.utf8     =>    ["el-GR"]                   expected   ["el-GR", "el", "en-US", "en"] 

Integration with Chrono

Hello o/

I recently added support for localization in chrono. I think it would be very useful for people to have a simple way to use locale_config in combination with pure-rust-locales so they can get date and time in the user's process language easily.

I'm really not sure about how it should be done. This is why I'm asking for your opinion.

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.