define config.default_language = "Chinese"

init python early hide:
    import os
    import importlib
    import inspect

    fonts_dir = os.path.join(config.gamedir, 'fonts')
    if os.path.exists(fonts_dir):
        for file in os.listdir(fonts_dir):
            if file.lower().endswith(('.ttf', '.otf', '.ttc')):
                try:
                    old_path = os.path.join(fonts_dir, file)
                    new_path = old_path + ".bak"
                    os.rename(old_path, new_path)
                except Exception:
                    pass

    global check_function_exists
    def check_function_exists(module_name, function_name):
        try:
            module = importlib.import_module(module_name)
            function = getattr(module, function_name)
            if inspect.isfunction(function):
                return True
            else:
                return False
        except ImportError:
            return False
        except AttributeError:
            return False

    global my_old_show_screen
    my_old_show_screen = renpy.show_screen
    global my_old_lookup
    my_old_lookup = None
    if check_function_exists('renpy.ast.Translate','lookup'):
        my_old_lookup = renpy.ast.Translate.lookup
        
    def my_show_screen(_screen_name, *_args, **kwargs):
        if _screen_name == 'preferences':
            _screen_name = 'my_preferences'
        if _screen_name == 'director':
            if my_old_lookup is not None:
                renpy.ast.Translate.lookup = my_old_lookup
        return my_old_show_screen(_screen_name, *_args, **kwargs)
        
    renpy.show_screen = my_show_screen


screen my_preferences():
    python:
        global os
        import os
        def traverse_first_dir(path):
            translator = renpy.game.script.translator
            languages = translator.languages
            l = languages
            if (os.path.exists(path)):
                files = os.listdir(path)
                for file in files:
                    m = os.path.join(path,file)
                    if (os.path.isdir(m)):
                        h = os.path.split(m)
                        l.add(h[1])
            return l
        l = traverse_first_dir('game/tl')
        
    tag menu
    use preferences
    
    vbox:
        align(.99, .99)
        hbox:
            box_wrap True
            vbox:
                xalign 1.0
                spacing 5
                
                label _("Language"):
                    xalign 0.5 # 标签靠右（若需居中改为0.5）
                    
                textbutton _("English"):
                    xalign 0.5 # 按钮内容靠右
                    action Language(None)
                    
                $ cnt = 0
                for i in l:
                    if i is not None and i != 'None':
                        textbutton "%s" % i:
                            xalign 1.0
                            action Language(i)