-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
.../snapshots/examples__build_load_babelbox__0.data_pack/load_babelbox_data_pack/pack.mcmeta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"pack": { | ||
"pack_format": 6, | ||
"description": "" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ad_babelbox__1.resource_pack/load_babelbox_resource_pack/assets/minecraft/lang/de_de.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
9 changes: 9 additions & 0 deletions
9
...ad_babelbox__1.resource_pack/load_babelbox_resource_pack/assets/minecraft/lang/en_us.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
9 changes: 9 additions & 0 deletions
9
...ad_babelbox__1.resource_pack/load_babelbox_resource_pack/assets/minecraft/lang/fr_fr.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
6 changes: 6 additions & 0 deletions
6
...ts/examples__build_load_babelbox__1.resource_pack/load_babelbox_resource_pack/pack.mcmeta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"pack": { | ||
"pack_format": 6, | ||
"description": "" | ||
} | ||
} |