Skip to content

Commit

Permalink
feat: add babelbox plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Mar 19, 2021
1 parent eacd2ae commit 70e1829
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 0 deletions.
85 changes: 85 additions & 0 deletions beet/contrib/babelbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""Plugin that loads translations from csv files.
Adapted from https://github.com/OrangeUtan/babelbox
Credit: Oran9eUtan <Oran9eUtan@gmail.com>
"""


__all__ = [
"babelbox",
"load_languages",
]


import logging
from csv import Dialect, DictReader, Sniffer
from typing import Dict, Iterable, Optional, Type, Union

from beet import Context, Language, Plugin
from beet.core.utils import FileSystemPath

DialectLike = Union[str, Dialect, Type[Dialect]]


logger = logging.getLogger(__name__)


def beet_default(ctx: Context):
config = ctx.meta.get("babelbox", {})

load = config.get("load", ())
dialect = config.get("dialect")
filename_prefix = config.get("filename_prefix", False)

ctx.require(babelbox(load, dialect, filename_prefix))


def babelbox(
load: Iterable[str] = (),
dialect: Optional[str] = None,
filename_prefix: bool = False,
) -> Plugin:
"""Return a plugin that loads translations from csv files."""

def plugin(ctx: Context):
minecraft = ctx.assets["minecraft"]

for pattern in load:
for path in ctx.directory.glob(pattern):
minecraft.languages.merge(
load_languages(
path=path,
dialect=dialect,
prefix=path.stem + "." if filename_prefix else "",
)
)

return plugin


def load_languages(
path: FileSystemPath,
dialect: Optional[DialectLike] = None,
prefix: str = "",
) -> Dict[str, Language]:
"""Return a dictionnary mapping each column to a language file."""
with open(path, newline="") as csv_file:
if not dialect:
dialect = Sniffer().sniff(csv_file.read(1024))
csv_file.seek(0)

reader = DictReader(csv_file, dialect=dialect)

key, *language_codes = reader.fieldnames or [""]
languages = {code: Language({}) for code in language_codes}

for row in reader:
if identifier := row[key]:
for code in language_codes:
if value := row[code]:
languages[code].data[prefix + identifier] = value
else:
msg = f"Locale {code!r} has no translation for {identifier!r}."
logger.warning(msg)

return languages
21 changes: 21 additions & 0 deletions tests/examples/load_babelbox/beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"pipeline": [
{
"pipeline": ["beet.contrib.babelbox"],
"meta": {
"babelbox": {
"load": ["translations/*.csv"],
"filename_prefix": true
}
}
},
{
"pipeline": ["beet.contrib.babelbox"],
"meta": {
"babelbox": {
"load": ["translations.csv"]
}
}
}
]
}
3 changes: 3 additions & 0 deletions tests/examples/load_babelbox/translations.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello;en_us;fr_fr;de_de
foo.hello;1;2;3
foo.world;4;5;6
5 changes: 5 additions & 0 deletions tests/examples/load_babelbox/translations/other.prefix.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Thing,en_us,fr_fr,de_de
foo,t1,t2,t3
,,,
bar,u1,u2,u3
baz,v1,v2,v3
3 changes: 3 additions & 0 deletions tests/examples/load_babelbox/translations/some.prefix.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Identifier,en_us,fr_fr
foo,hello,world
bar,egg,spam
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": ""
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"other.prefix.foo": "t3",
"other.prefix.bar": "u3",
"other.prefix.baz": "v3",
"foo.hello": "3",
"foo.world": "6"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"some.prefix.foo": "hello",
"some.prefix.bar": "egg",
"other.prefix.foo": "t1",
"other.prefix.bar": "u1",
"other.prefix.baz": "v1",
"foo.hello": "1",
"foo.world": "4"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"some.prefix.foo": "world",
"some.prefix.bar": "spam",
"other.prefix.foo": "t2",
"other.prefix.bar": "u2",
"other.prefix.baz": "v2",
"foo.hello": "2",
"foo.world": "5"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 6,
"description": ""
}
}

0 comments on commit 70e1829

Please sign in to comment.