Godot 翻译 - 如何处理其他语言中的较长字符串?

问题描述 投票:0回答:1

如上的问题,翻译服务器完成工作后,某些语言的字符串比其他语言长得多。 我知道容器可能会调整大小,或者我可以剪辑文本或将其换行。但是,如果我设置了容器大小并且不想更改它,并且换行文本没有帮助,该怎么办? 我的想法是调整文本大小,但我不知道在

TranslationServer.set_locale(lang)
之后我应该做什么?

我最好的选择是将所有标签/按钮

clip_text
属性设置为
true
并执行以下操作:

TranslationServer.set_locale(lang)
loop through entire tree:
    if CURRENT_NODE does have properity .text:
        var clipping = true
        while clipping
             if CURRENT_NODE clip_text properity is currently NOT clipping anything #I have no idea how to check this?
                 clipping = false
             else:
                 CURRENT_NODE font_size -= 1

我的下一个想法是关闭 Clip_text 并像上面一样循环遍历每个节点,但不是检查剪切活动,而是为用户创建一个不可见的节点副本,该副本也会复制节点宽度大小,但这次允许节点调整大小,并测量节点宽度,与原始节点进行比较,并像上面那样调整字体大小,直到复制的节点宽度相同或更低,然后将建立的 int 方式设置为原始节点的大小。但这是疯狂的矫枉过正,而且看起来像是一件奇怪的事情,对吧?每次都需要对包含文本的每个新节点执行此操作,并且如果任何文本确实一遍又一遍地更改。这是它应该的样子吗?

翻译后字符串过大似乎是一个常见问题,对吗?但我无法找到任何可能对我的情况有帮助的合理解决方案或示例。任何帮助都会很棒。

string localization translation godot godot4
1个回答
0
投票

第一个建议是用长文本进行设计和测试。您可以通过 Pseudolocalization 来实现。

您似乎想要缩小(使文本变小,请注意项目设置中的拉伸模式),这可能会导致字体难以辨认。


把它排除在外......

我建议将

autowrap_mode
AUTOWRAP_OFF
中的
AUTOWRAP_WORD_SMART
设置为
Label
RichTextLabel

并在

fit_content
中将
RichTextLabel
设置为 true。所以它们会随着文本调整大小。

如果使用

visible_characters
,请将
visible_characters_behavior
设置为
VC_CHARS_AFTER_SHAPING


是的,这会带来对齐问题(例如,

Control
调整大小并且不再居中),您可以对调整大小(或主题和区域设置更改)做出反应并使用
set_anchors_and_offsets_preset

我的标签中有这个:

func _notification(what: int) -> void:
    if what == NOTIFICATION_TRANSLATION_CHANGED or what == NOTIFICATION_THEME_CHANGED:
        _update_text()

哪里

_update_text()
可以做我需要做的任何事情。我使用延迟调用来执行
set_anchors_and_offsets_preset

您可以对

OptionButton
使用类似的方法。


好的,这是标签,但其他控件可能需要调整大小,或者不适应您想要的自定义。

例如,如果

Button
的默认行为不好,您可以创建一个自定义
Button
类,在内部实例化
Label
Label
的工作原理如上所述。并且
Button
会将其自身调整为
Label
的大小(如果需要,还可添加一些额外的边距)。您可以在
_process
resized
Label

信号中执行此操作

同样的想法也适用于调整大小以匹配孩子大小的

Container

更通用的解决方案是使用一个

Control
复制另一个
Control
的大小和位置,并添加额外的边距。我可以给出完整的代码:

@tool
extends Control


@export var source:Control:
    set(mod_value):
        if source == mod_value:
            return

        source = mod_value
        if is_node_ready():
            _update_source()


@export var grow_left:float
@export var grow_top:float
@export var grow_right:float
@export var grow_bottom:float


func _ready() -> void:
    _update_source()


func _exit_tree() -> void:
    if is_queued_for_deletion():
        return

    request_ready()


func _process(_delta: float) -> void:
    if not is_instance_valid(source):
        _update_source()
        return

    var parent_control := get_parent_control()
    # global transform of the parent
    var parent_control_global_transform := Transform2D.IDENTITY
    if is_instance_valid(parent_control):
        parent_control_global_transform = parent_control.get_global_transform()

    # global transform of source
    var _source_global_transform := source.get_global_transform()
    # transform of source relative to the parent
    var _source_to_local_transform := parent_control_global_transform.affine_inverse() * _source_global_transform

    visible = source.visible
    size_flags_horizontal = source.size_flags_horizontal
    size_flags_vertical = source.size_flags_vertical
    size_flags_stretch_ratio = source.size_flags_stretch_ratio
    custom_minimum_size = source.get_combined_minimum_size() + Vector2(grow_left + grow_right, grow_top + grow_bottom)
    size = source.size + Vector2(grow_left + grow_right, grow_top + grow_bottom)
    global_position = _source_global_transform.origin - Vector2(grow_left, grow_top)
    rotation = _source_to_local_transform.get_rotation()
    scale = _source_to_local_transform.get_scale()


func _update_source() -> void:
    if not is_inside_tree():
        return

    if not is_instance_valid(source):
        source = get_parent_control()

    set_process(is_instance_valid(source))

您可以将其视为穷人对

Control
的远程改造。

上面的代码使用

_process
,因为并非所有更改都有适当的通知。

因此,例如,您可以让它遵循

VBoxContainer
的大小和位置,其中
Button
可能会因局部变化而调整大小...然后在其内部有
Panel
StyleBox
提供了围绕
VBoxContainer
的边框。


在某些情况下,您还可以资源使用

HFlowContainer
VFlowContainer
,这样您的
Control
就可以移动到另一行或另一列。

我相信最后一种情况是当您想要隐藏溢出并提供“查看更多”选项或分页时,我相信这将根据具体情况进行定制。

© www.soinside.com 2019 - 2024. All rights reserved.