Skip to content

Multi LanguagePackage

Features#

Multi -language package plug -in implementation data to ARKID registered language translation data function,Developers only need to provide the dictionary and corresponding language name after the translation is completed to complete the multi -language internationalization configuration function。

Implementation#

When developing a multi -language package plug -in,Only inherit the multi -language plug -in base class and re -load LANGUAGE_type与language_Data two functions can be,The platform will integrate data in the plug -in into the platform translation data,Distributed to the front end through OpenAPI description,You can get the corresponding data on the front page to complete the international function。

Notice: A language package plug -in only supports one language translation data,At the same time Database custom translation data > Plug -in translation data > The platform comes with translation data

sequenceDiagram
    participant U as Client
    participant C as Platform core
    participant B as Multi -language package plugin

    C->>B: Loading plug -in
    B->>C: Registered language package
    U->>C: Request OpenAPI description data
    C->>C: Organize translation data,Overlay platform translation in turn、Plug -in translation、Practitioner custom translation
    C->>U: Completed translation data

The example is as follows:

    class TranslationZhExtension(LanguageExtension):

        def language_type(self) -> str:
            return _("Simplified Chinese")

        def language_data(self) -> dict:
            return {
                "data":"data"
            }

Abstract function#

Foundation definition#

arkid.core.extension.language.LanguageExtension (Extension) #

Source code in arkid/core/extension/language.py
class LanguageExtension(Extension):

    TYPE = "language"

    @property
    def type(self):
        return LanguageExtension.TYPE

    def load(self):
        super().load()
        self.load_language_data(
            language_type=self.language_type(),
            data=self.language_data()
        )

    def load_language_data(self, data, language_type=_("简体中文")):
        """加载语言包

        Args:
            data (dict): 翻译数据
            language_type (str, optional): 语言名称. Defaults to _("简体中文").
        """

        self.language_type = language_type
        self.extension_data = data

        extension = self.model
        try:
            language_data, _ = LanguageData.active_objects.get_or_create(
                extension=extension)
            language_data.extension_data = self.extension_data
            language_data.name = self.language_type

            language_data.save()
        except Exception as err:
            logger.error(err)

        self.refresh_lang_maps()

    @abstractmethod
    def language_type(self) -> str:
        """ 语言类型

        Returns:
            str: 语言类型名称 默认为简体中文
        """
        return _("简体中文")

    @abstractmethod
    def language_data(self) -> dict:
        """翻译数据

        Returns:
            dict: 翻译数据字典
        """
        return {}

    def refresh_lang_maps(self):
        """刷新语言包
        """
        core_translation.lang_maps = core_translation.reset_lang_maps()

language_data(self) #

翻译数据

Returns:

Type Description
dict

翻译数据字典

Source code in arkid/core/extension/language.py
@abstractmethod
def language_data(self) -> dict:
    """翻译数据

    Returns:
        dict: 翻译数据字典
    """
    return {}

language_type(self) #

语言类型

Returns:

Type Description
str

语言类型名称 默认为简体中文

Source code in arkid/core/extension/language.py
@abstractmethod
def language_type(self) -> str:
    """ 语言类型

    Returns:
        str: 语言类型名称 默认为简体中文
    """
    return _("简体中文")

load(self) #

抽象方法,插件加载的入口方法

Source code in arkid/core/extension/language.py
def load(self):
    super().load()
    self.load_language_data(
        language_type=self.language_type(),
        data=self.language_data()
    )

load_language_data(self, data, language_type='简体中文') #

加载语言包

Parameters:

Name Type Description Default
data dict

翻译数据

required
language_type str

语言名称. Defaults to _("简体中文").

'简体中文'
Source code in arkid/core/extension/language.py
def load_language_data(self, data, language_type=_("简体中文")):
    """加载语言包

    Args:
        data (dict): 翻译数据
        language_type (str, optional): 语言名称. Defaults to _("简体中文").
    """

    self.language_type = language_type
    self.extension_data = data

    extension = self.model
    try:
        language_data, _ = LanguageData.active_objects.get_or_create(
            extension=extension)
        language_data.extension_data = self.extension_data
        language_data.name = self.language_type

        language_data.save()
    except Exception as err:
        logger.error(err)

    self.refresh_lang_maps()

refresh_lang_maps(self) #

刷新语言包

Source code in arkid/core/extension/language.py
def refresh_lang_maps(self):
    """刷新语言包
    """
    core_translation.lang_maps = core_translation.reset_lang_maps()

Exemplary#

extension_root.com_longgui_language_zh.TranslationZhExtension (LanguageExtension) #

Source code in extension_root/com_longgui_language_zh/__init__.py
class TranslationZhExtension(LanguageExtension):
    def language_type(self) -> str:
        return _("简体中文")

    def language_data(self) -> dict:
        return {
            "data":"数据"
        }

language_data(self) #

翻译数据

Returns:

Type Description
dict

翻译数据字典

Source code in extension_root/com_longgui_language_zh/__init__.py
def language_data(self) -> dict:
    return {
        "data":"数据"
    }

language_type(self) #

语言类型

Returns:

Type Description
str

语言类型名称 默认为简体中文

Source code in extension_root/com_longgui_language_zh/__init__.py
def language_type(self) -> str:
    return _("简体中文")

评论