1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
//! # languages-rs
//!
//! An internationalization library for your applications.
//!
//! # Features
//! - `JSON` or `TOML` languages files.
//! - Only can use Objects, Arrays and Strings.
//! - Customize the languages directory.
//!
//! # JSON Language File
//! ```json
//! {
//!     "hello_world": "Hello, world!",
//!     "home": {
//!         "title": "Home page",
//!         "description": "This is the home page."
//!     },
//!     "data": {
//!         "messages": [
//!             "Message 1",
//!             "Message 2"
//!         ]
//!     ]
//! }
//! ```
//!
//! # TOML Language File
//! ```toml
//! hello_world = "Hello, world!"
//!
//! [home]
//! title = "Home page"
//! description = "This is the home page."
//!
//! [data]
//! messages = [
//!     "Message 1",
//!     "Message 2"
//! ]
//! ```
//!
//! # Basic Usage
//! `languages/en.json`
//! ```json
//! {
//!     "hello_world": "Hello world!"
//! }
//! ```
//!
//! `src/main.rs`
//! ```rust, ignore
//! use languages_rs::{Config, Languages, load, Value};
//!
//! fn main() -> Result<()> {
//!     let mut configuration: Config = Config::default().unwrap();
//!     configuration.add_language("en").unwrap();
//!
//!     // Load all default languages.
//!     let texts: Languages = load(configuration).unwrap();
//!
//!     // Get the English texts from `/languages/es.json`.
//!     let texts_en: LanguagesTexts = texts.try_get_language("en").unwrap();
//!
//!     // Get the `hello_world` text from English texts.
//!     let en_hello_world: Value = texts_en.try_get_text("hello_world").unwrap();
//!     assert!(en_hello_world.is_string());
//!
//!     // Other alternative to get the `hello_world` text from English texts.
//!     let en_hello_world_2: Value = texts.try_get_text_from_language("en", "hello_world").unwrap();
//!     assert!(en_hello_world_2.is_string());
//!
//!     assert_eq!(en_hello_world, en_hello_world_2);
//!     assert_eq!(en_hello_world.get_string(), en_hello_world_2.get_string());
//! }
//! ```

mod config;
mod languages;
mod value;

pub use config::{Config, Format};
pub use languages::{LanguageTexts, Languages};
pub use value::Value;

/// Load the languages of a configuration and return the `Languages` struct.
///
/// # Example
/// ```rust, ignore
/// use languages_rs::{Config, load};
///
/// let mut config = Config::default();
///
/// // Add `en` language to the configuration.
/// config.add_language("en").unwrap();
///
/// // This loads `languages/en.json` to the cache.
/// let texts = load(config);
/// ```
pub fn load(configuration: Config) -> anyhow::Result<Languages> {
    let mut languages = Languages::new(&configuration);

    for lang in configuration.get_languages().iter() {
        languages.try_get_language(lang)?;
    }

    Ok(languages)
}