forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

strings.rs24 lines, 764 bytesHistoryRawDownload
1//! UI strings, rendered by the server into `<script type="application/json" id="editor-strings">`.
2
3use wasm_bindgen::JsValue;
4
5pub struct Strings(JsValue);
6
7impl Strings {
8 pub fn load(document: &web_sys::Document) -> Self {
9 let v = document
10 .get_element_by_id("editor-strings")
11 .and_then(|e| e.text_content())
12 .and_then(|t| js_sys::JSON::parse(&t).ok())
13 .unwrap_or_else(|| js_sys::Object::new().into());
14 Strings(v)
15 }
16
17 /// The string for `key`, or the key itself when missing.
18 pub fn get(&self, key: &str) -> String {
19 js_sys::Reflect::get(&self.0, &key.into())
20 .ok()
21 .and_then(|v| v.as_string())
22 .unwrap_or_else(|| key.to_owned())
23 }
24}