浏览代码

Create translation class.

Cixo Develop 4 月之前
父节点
当前提交
232e443011
共有 6 个文件被更改,包括 97 次插入0 次删除
  1. 23 0
      pyproject.toml
  2. 6 0
      setup.cfg
  3. 1 0
      source/cx_libtranslate/__init__.py
  4. 37 0
      source/cx_libtranslate/translation.py
  5. 11 0
      tests/000-example.py
  6. 19 0
      tests/001-translation.py

+ 23 - 0
pyproject.toml

@@ -0,0 +1,23 @@
+[project]
+name = "cx_libtranslate"
+version = "1.0.0"
+authors = [
+    { name = "Cixo Develop", email = "[email protected]" }
+]
+description = "This is library for translating apps."
+readme = "README.md"
+requires-python = ">=3.7"
+classifiers = [
+    "Programming Language :: Python :: 3",
+    "Operating System :: OS Independent"
+]
+license = "MIT"
+license-files = [ "LICENSE" ]
+
+[project.urls]
+Homepage = "https://git.cixoelectronic.pl/cixo-electronic/cx-libtranslate-python"
+Issues = "https://git.cixoelectronic.pl/cixo-electronic/cx-libtranslate-python/issues"
+
+[build-system]
+requires = [ "setuptools >= 77.0.3" ]
+build-backend = "setuptools.build_meta"

+ 6 - 0
setup.cfg

@@ -0,0 +1,6 @@
+[options]
+package_dir = source
+packages = find:
+
+[options.packages.find]
+where = source

+ 1 - 0
source/cx_libtranslate/__init__.py

@@ -0,0 +1 @@
+from .translation import translation

+ 37 - 0
source/cx_libtranslate/translation.py

@@ -0,0 +1,37 @@
+class translation:
+    def __init__(self, content: str, success: bool = True) -> None:
+        self.__success = success
+        self.__content = content
+
+    def __str__(self) -> str:
+        return self.__content
+
+    @property
+    def text(self) -> str:
+        return self.__content
+
+    @property
+    def valid(self) -> bool:
+        return self.__success
+
+    def format(self, params: dict) -> str:
+        parts = self.__content.split("#{")
+        results = parts.pop(0)
+
+        for count in parts:
+            elements = count.split("}")
+
+            if len(elements) == 1:
+                results.append(count)
+                continue
+
+            name = elements.pop(0).strip()
+            rest = str("}").join(elements)
+
+            if not name in params:
+                results = results + rest
+                continue
+
+            results = results + str(params[name]) + rest
+
+        return results

+ 11 - 0
tests/000-example.py

@@ -0,0 +1,11 @@
+import pathlib
+
+current = pathlib.Path(__file__).parent
+root = current.parent
+package = root / pathlib.Path("source")
+
+import sys
+sys.path.append(str(package))
+
+import cx_libtranslate
+

+ 19 - 0
tests/001-translation.py

@@ -0,0 +1,19 @@
+import pathlib
+
+current = pathlib.Path(__file__).parent
+root = current.parent
+package = root / pathlib.Path("source")
+
+import sys
+sys.path.append(str(package))
+
+import cx_libtranslate
+
+sample = cx_libtranslate.translation("This is #{ sample } with #{ number }")
+
+print("Testing...")
+print("Result: " + str(sample))
+print("Formated: " + sample.format({
+    "sample": "example", 
+    "number": 10
+}))