Skip to content

Commit

Permalink
feat: add unicode_escape option to babelbox
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jan 13, 2024
1 parent cb69414 commit bed6a0e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions beet/contrib/babelbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class BabelboxOptions(PluginOptions):
dialect: Optional[str] = None
namespace: str = "minecraft"
filename_prefix: bool = False
unicode_escape: bool = False


def beet_default(ctx: Context):
Expand All @@ -57,6 +58,7 @@ def babelbox(ctx: Context, opts: BabelboxOptions):
path=path,
dialect=opts.dialect,
prefix=Path(path).stem + "." if opts.filename_prefix else "",
unicode_escape=opts.unicode_escape,
)
)

Expand All @@ -65,6 +67,7 @@ def load_languages(
path: FileSystemPath,
dialect: Optional[DialectLike] = None,
prefix: str = "",
unicode_escape: bool = False,
) -> Dict[str, Language]:
"""Return a dictionnary mapping each column to a language file."""
with open(path, encoding="utf-8", newline="") as csv_file:
Expand All @@ -85,6 +88,8 @@ def load_languages(

for code in language_codes:
if value := row[code]:
if unicode_escape:
value = value.encode("utf-8").decode("unicode_escape")
languages[code].data[identifier] = value
else:
logger.warning(
Expand Down
9 changes: 9 additions & 0 deletions examples/load_babelbox/beet.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
"namespace": "demo"
}
}
},
{
"pipeline": ["beet.contrib.babelbox"],
"meta": {
"babelbox": {
"load": ["unicode_escape.csv"],
"unicode_escape": true
}
}
}
]
}
3 changes: 3 additions & 0 deletions examples/load_babelbox/unicode_escape.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,en_us
entity.gm4.congealed_shard,"Congealed Soul Shard\u00A7"
entity.gm4.fire_sprite,"Fire Sprite ""named bob"""
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"other.prefix.bar": "u1",
"other.prefix.baz": "v1",
"foo.hello": "1",
"foo.world": "4"
"foo.world": "4",
"entity.gm4.congealed_shard": "Congealed Soul Shard\u00a7",
"entity.gm4.fire_sprite": "Fire Sprite \"named bob\""
}

0 comments on commit bed6a0e

Please sign in to comment.