Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions langs/zh_TW_extra_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"hello": "哈囉,世界!",
"goodbye": "再見"
}
18 changes: 12 additions & 6 deletions src/key_copy/json_gettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl<'a> JSONGetText<'a> {
/// Create a new JSONGetText instance with context and a default key.
pub(crate) fn from_context_with_default_key(
default_key: Key,
ignore_extra_keys: bool,
mut context: Context<'a>,
) -> Result<JSONGetText<'a>, JSONGetTextBuildError> {
if !context.contains_key(&default_key) {
Expand All @@ -36,12 +37,17 @@ impl<'a> JSONGetText<'a> {
{
for (key, mut map) in context {
{
for map_key in map.keys() {
if !default_map.contains_key(map_key) {
return Err(JSONGetTextBuildError::TextInKeyNotInDefaultKey {
key,
text: map_key.clone(),
});
if ignore_extra_keys {
map =
map.into_iter().filter(|(k, _)| default_map.contains_key(k)).collect();
} else {
for map_key in map.keys() {
if !default_map.contains_key(map_key) {
return Err(JSONGetTextBuildError::TextInKeyNotInDefaultKey {
key,
text: map_key.clone(),
});
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/key_string/json_get_text_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ impl<'a> JSONGetTextBuilder<'a> {
}

/// Build a `JSONGetText` instance.
pub fn build(self) -> Result<JSONGetText<'a>, JSONGetTextBuildError> {
JSONGetText::from_context_with_default_key(self.default_key, self.context)
pub fn build(self, ignore_extra_keys: bool) -> Result<JSONGetText<'a>, JSONGetTextBuildError> {
JSONGetText::from_context_with_default_key(
self.default_key,
ignore_extra_keys,
self.context,
)
}
}

Expand Down
35 changes: 22 additions & 13 deletions src/key_string/json_gettext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl<'a> JSONGetText<'a> {
/// Create a new JSONGetText instance with context and a default key.
pub(crate) fn from_context_with_default_key<S: AsRef<str> + Into<String>>(
default_key: S,
ignore_extra_keys: bool,
mut context: Context<'a>,
) -> Result<JSONGetText<'a>, JSONGetTextBuildError> {
if !context.contains_key(default_key.as_ref()) {
Expand All @@ -38,25 +39,33 @@ impl<'a> JSONGetText<'a> {
{
for (key, mut map) in context {
{
for map_key in map.keys() {
if !default_map.contains_key(map_key) {
return Err(JSONGetTextBuildError::TextInKeyNotInDefaultKey {
key,
text: map_key.clone(),
});
if ignore_extra_keys {
map =
map.into_iter().filter(|(k, _)| default_map.contains_key(k)).collect();
} else {
for map_key in map.keys() {
if !default_map.contains_key(map_key) {
return Err(JSONGetTextBuildError::TextInKeyNotInDefaultKey {
key,
text: map_key.clone(),
});
}
}
}
}

{
for map_key in default_map.keys() {
if !map.contains_key(map_key) {
map.insert(map_key.clone(), default_map.get(map_key).unwrap().clone());
{
for map_key in default_map.keys() {
if !map.contains_key(map_key) {
map.insert(
map_key.clone(),
default_map.get(map_key).unwrap().clone(),
);
}
}
}
}

inner_context.insert(key, map);
inner_context.insert(key, map);
}
}

inner_context.insert(default_key.clone().into(), default_map);
Expand Down
2 changes: 1 addition & 1 deletion src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro_rules! static_json_gettext_build {
builder.add_json($key, include_str!($crate::manifest_dir_macros::path!($path))).unwrap();
)*

builder.build()
builder.build(false)
}
};
}
Expand Down
14 changes: 14 additions & 0 deletions tests/key_string_single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#[macro_use]
extern crate json_gettext;

use json_gettext::JSONGetTextBuilder;

#[test]
fn single_get() {
let ctx = static_json_gettext_build!(
Expand Down Expand Up @@ -44,3 +46,15 @@ fn map_get() {
assert_eq!("哈囉,世界!", map.get("hello").unwrap());
assert_eq!("Rust!", map.get("rust").unwrap());
}

#[test]
fn extra_keys() {
let mut builder = JSONGetTextBuilder::new("en_US");
builder.add_json_file("en_US", "langs/en_US.json").unwrap();
builder.add_json_file("zh_TW", "langs/zh_TW_extra_key.json").unwrap();
let ctx = builder.build(true).unwrap();

let map = ctx.get("zh_TW");
assert_eq!("哈囉,世界!", map.get("hello").unwrap());
assert_eq!(None, map.get("goodbye"));
}