Skip to content

Commit

Permalink
Support for pseudo language (#34)
Browse files Browse the repository at this point in the history
* Support for pseudo language

* More fix for the pseudo language

* Lint the code

* More lint

* Fix packaging pseudo language

---------

Co-authored-by: Frédéric Collonval <[email protected]>
  • Loading branch information
fcollonval and fcollonval authored May 15, 2024
1 parent 0372972 commit 8e9f697
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
44 changes: 21 additions & 23 deletions jupyterlab_translate/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,40 @@

# Minimal percentage needed to compile a PO file
COMPILATION_THRESHOLD = 0
PACKAGE_PREFIX = "jupyterlab_language_pack_"


class JupyterLanguageBuildHook(BuildHookInterface):
"""Hatch build plugin to package Jupyter language pack."""

PLUGIN_NAME = "jupyter-translate"

def clean(self, versions: list[str]) -> None:
"""This occurs before the build process if the -c/--clean flag was
passed to the build command, or when invoking the clean command.
"""

def _get_locale_name(self) -> tuple[Path, str]:
package_folder = Path(self.root)
python_folder = next(
package_folder.glob("jupyterlab_language_pack_??_??"), None
package_folder.glob(f"{PACKAGE_PREFIX}??_??"),
next(package_folder.glob(f"{PACKAGE_PREFIX}???_??"), None),
)
if python_folder is None:
self.app.display_error(
f"Unable to get the Python folder name in {package_folder!s}"
)
return
else:
locale_name = python_folder.name[-5:]
raise RuntimeError()

locale_name = python_folder.name[len(PACKAGE_PREFIX) :]
messages_folder = python_folder / "locale" / locale_name / "LC_MESSAGES"

return messages_folder, locale_name

def clean(self, versions: list[str]) -> None:
"""This occurs before the build process if the -c/--clean flag was
passed to the build command, or when invoking the clean command.
"""
try:
messages_folder, locale_name = self._get_locale_name()
except RuntimeError:
return

for bundle in itertools.chain(
messages_folder.glob("*.json"), messages_folder.glob("*.mo")
):
Expand All @@ -51,22 +59,12 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
Any modifications to the build data will be seen by the build target.
"""

package_folder = Path(self.root)
python_folder = next(
package_folder.glob("jupyterlab_language_pack_??_??"), None
)
if python_folder is None:
self.app.display_error(
f"Unable to get the Python folder name in {package_folder!s}"
)
try:
messages_folder, locale_name = self._get_locale_name()
except RuntimeError:
return
else:
locale_name = python_folder.name[-5:]

if self.target_name == "wheel":
messages_folder = python_folder / "locale" / locale_name / "LC_MESSAGES"

po_files = list(filter(lambda f: f.is_file(), messages_folder.glob("*.po")))
for file in po_files:
po = polib.pofile(str(file))
Expand All @@ -91,7 +89,7 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
)
else:
# Update the contributors file
contributors = package_folder / CONTRIBUTORS
contributors = Path(self.root) / CONTRIBUTORS
content = get_contributors_report(
locale=locale_name.replace("_", "-"), crowdin_key=CROWDIN_API_KEY
)
Expand Down
22 changes: 13 additions & 9 deletions jupyterlab_translate/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,27 @@ def create_new_language_pack(
if not check_locale(locale):
raise Exception("Invalid locale!")

loc = babel.Locale.parse("nb_NO" if locale == "no_NO" else locale)
try:
loc = babel.Locale.parse("nb_NO" if locale == "no_NO" else locale)
language_name = loc.english_name
except babel.UnknownLocaleError:
language_name = "unknown"

locale_dash = locale.replace("_", "-")
pkg_dir = Path(output_dir) / f"jupyterlab-language-pack-{locale_dash}"
pkg_dir.mkdir(parents=True, exist_ok=True)
copier.run_auto(
template_url,
pkg_dir,
data={"locale": locale_dash, "language": loc.english_name, "version": version},
data={"locale": locale_dash, "language": language_name, "version": version},
vcs_ref=template_ref,
)


def check_locale(locale: str) -> bool:
"""Check if a locale is a valid value."""
# Add exception for no_NO
if locale == "no_NO":
# Add exceptions
if locale in {"ach_UG", "no_NO"}:
return True

value = False
Expand Down Expand Up @@ -307,11 +311,11 @@ def get_line(lines: List[str], value: str) -> str:
"definitions/.*/properties/.*/title": _default_settings_context,
"definitions/.*/properties/.*/description": _default_settings_context,
# JupyterLab-specific
"jupyter\.lab\.setting-icon-label": _default_settings_context,
"jupyter\.lab\.menus/.*/label": "menu",
"jupyter\.lab\.metadataforms/.*/label": "metadataforms",
"jupyter\.lab\.toolbars/.*/label": "toolbar",
"jupyter\.lab\.toolbars/.*/caption": "toolbar",
r"jupyter\.lab\.setting-icon-label": _default_settings_context,
r"jupyter\.lab\.menus/.*/label": "menu",
r"jupyter\.lab\.metadataforms/.*/label": "metadataforms",
r"jupyter\.lab\.toolbars/.*/label": "toolbar",
r"jupyter\.lab\.toolbars/.*/caption": "toolbar",
}


Expand Down

0 comments on commit 8e9f697

Please sign in to comment.