{"id":21586137,"url":"https://github.com/magiclen/json-gettext","last_synced_at":"2025-04-10T20:20:23.989Z","repository":{"id":62441391,"uuid":"145522992","full_name":"magiclen/json-gettext","owner":"magiclen","description":"A library for getting text from JSON usually for internationalization.","archived":false,"fork":false,"pushed_at":"2023-12-10T09:58:30.000Z","size":117,"stargazers_count":8,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-03T04:47:48.684Z","etag":null,"topics":["gettext","internationalization","json","localization","rocket","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magiclen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-21T07:17:54.000Z","updated_at":"2024-04-21T14:56:51.000Z","dependencies_parsed_at":"2024-11-14T17:10:34.300Z","dependency_job_id":"e5210012-d32c-4360-b4fb-79573ec3d7b4","html_url":"https://github.com/magiclen/json-gettext","commit_stats":{"total_commits":86,"total_committers":1,"mean_commits":86.0,"dds":0.0,"last_synced_commit":"a98753d7d478c15991bf323a1ecf3be32f7cd216"},"previous_names":[],"tags_count":58,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fjson-gettext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fjson-gettext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fjson-gettext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fjson-gettext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magiclen","download_url":"https://codeload.github.com/magiclen/json-gettext/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248289805,"owners_count":21078921,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gettext","internationalization","json","localization","rocket","rust"],"created_at":"2024-11-24T15:12:46.321Z","updated_at":"2025-04-10T20:20:23.967Z","avatar_url":"https://github.com/magiclen.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"JSON Get Text\n====================\n\n[![CI](https://github.com/magiclen/json-gettext/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/json-gettext/actions/workflows/ci.yml)\n\nThis is a library for getting text from JSON usually for internationalization.\n\n## Example\n\n```rust\n#[macro_use] extern crate json_gettext;\n\nlet ctx = static_json_gettext_build!(\n    \"en_US\";\n    \"en_US\" =\u003e \"langs/en_US.json\",\n    \"zh_TW\" =\u003e \"langs/zh_TW.json\"\n).unwrap();\n\nassert_eq!(\"Hello, world!\", get_text!(ctx, \"hello\").unwrap());\nassert_eq!(\"哈囉，世界！\", get_text!(ctx, \"zh_TW\", \"hello\").unwrap());\n```\n\n## Rocket Support\n\nThis crate supports the Rocket framework. In order to reload changed json files instead of recompiling the program you have to enable the `rocket` feature for this crate.\n\n```toml\n[dependencies.json-gettext]\nversion = \"*\"\nfeatures = [\"rocket\"]\n```\n\nThen, use the `static_json_gettext_build_for_rocket` macro instead of the `static_json_gettext_build` macro to build a `JSONGetText`(`JSONGetTextManager`).\n\n```rust\n#[macro_use] extern crate json_gettext;\n\n#[macro_use] extern crate rocket;\n\nuse rocket::State;\nuse rocket::response::Redirect;\n\nuse json_gettext::JSONGetTextManager;\n\n#[get(\"/\")]\nfn index(ctx: \u0026State\u003cJSONGetTextManager\u003e) -\u003e Redirect {\n    Redirect::temporary(uri!(hello(lang = ctx.get_default_key())))\n}\n\n#[get(\"/\u003clang\u003e\")]\nfn hello(ctx: \u0026State\u003cJSONGetTextManager\u003e, lang: String) -\u003e String {\n    format!(\"Ron: {}\", get_text!(ctx, lang, \"hello\").unwrap().as_str().unwrap())\n}\n\n#[launch]\nfn rocket() -\u003e _ {\n    rocket::build()\n        .attach(static_json_gettext_build_for_rocket!(\n            \"en_US\";\n            \"en_US\" =\u003e \"langs/en_US.json\",\n            \"zh_TW\" =\u003e \"langs/zh_TW.json\"\n        ))\n        .mount(\"/\", routes![index, hello])\n}\n```\n\nIf you are not using the `release` profile, `JSONGetTextManager` can reload the json files automatically if needed.\n\n## `unic-langid` Support\n\nSince string comparison could be slow, the `language_region_pair` feature, the `language` feature or the `region` feature can be enabled to change key's type to `(Language, Option\u003cRegion\u003e)`, `Language` or `Region` respectively where `Language` and `Region` structs are in the `unic-langid` crate.\n\nIn this case, the `key!` macro would be useful for generating a `Key` instance from a literal string.\n\nFor example,\n\n```toml\n[dependencies.json-gettext]\nversion = \"*\"\nfeatures = [\"language_region_pair\", \"rocket\"]\n```\n\n```rust\n#[macro_use]\nextern crate rocket;\n\n#[macro_use]\nextern crate rocket_accept_language;\n\n#[macro_use]\nextern crate json_gettext;\n\nuse rocket::State;\n\nuse rocket_accept_language::unic_langid::subtags::Language;\nuse rocket_accept_language::AcceptLanguage;\n\nuse json_gettext::{JSONGetTextManager, Key};\n\nconst LANGUAGE_EN: Language = language!(\"en\");\n\n#[get(\"/\")]\nfn index(ctx: \u0026State\u003cJSONGetTextManager\u003e, accept_language: \u0026AcceptLanguage) -\u003e String {\n    let (language, region) = accept_language.get_first_language_region().unwrap_or((LANGUAGE_EN, None));\n\n    format!(\"Ron: {}\", get_text!(ctx, Key(language, region), \"hello\").unwrap().as_str().unwrap())\n}\n\n#[launch]\nfn rocket() -\u003e _ {\n    rocket::build()\n        .attach(static_json_gettext_build_for_rocket!(\n            key!(\"en\");\n            key!(\"en\") =\u003e \"langs/en_US.json\",\n            key!(\"zh_TW\") =\u003e \"langs/zh_TW.json\",\n        ))\n        .mount(\"/\", routes![index])\n}\n```\n\n## Crates.io\n\nhttps://crates.io/crates/json-gettext\n\n## Documentation\n\nhttps://docs.rs/json-gettext\n\n## License\n\n[MIT](LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fjson-gettext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagiclen%2Fjson-gettext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fjson-gettext/lists"}