Page 1 of 1

Using ParameterizedText with Chinese language

Posted: Mon Sep 28, 2020 5:54 pm
by thirstyoctopus
Hi everyone, hope someone can help me out.

I'm writing a VN and have been working with a publisher who needs Chinese translation in the game. However, the various fonts I use for displaying in the game do not support Chinese characters and so have implemented a way to register the new font for the say screen. This is fine, but I also have some ParameterizedText set up to show as Chapter screens. Currently, the three different types of ParameterizedText are set up as follows:

Code: Select all

## TEXT AND HEADINGS
image screen_text = ParameterizedText(xalign=0.5, yalign=0.5, size=40, font=gui.name_text_font)
image chapter_text = ParameterizedText(xalign=0.5, yalign=0.46, size=60, font=gui.name_text_font)
image chapter_name_text = ParameterizedText(xalign=0.5, yalign=0.53, size=50, font=gui.name_text_font)
I implement the chapter text at the top of each chapter script:

Code: Select all

show chapter_text "Chapter 1"
show chapter_name_text "Chapter Name Here"
When the language is changed to Chinese however, these headings disappear. So, the only way I can see of getting it to work is to declare additional ParameterizedText specifically for the Chinese font:

Code: Select all

## TEXT AND HEADINGS
image screen_text = ParameterizedText(xalign=0.5, yalign=0.5, size=40, font=gui.name_text_font)
image chapter_text = ParameterizedText(xalign=0.5, yalign=0.46, size=60, font=gui.name_text_font)
image chapter_name_text = ParameterizedText(xalign=0.5, yalign=0.53, size=50, font=gui.name_text_font)
## For Simplified Chinese
image screen_text_sc = ParameterizedText(xalign=0.5, yalign=0.5, size=40, font="gui/fonts/NotoSansSC-Regular.otf")
image chapter_text_sc = ParameterizedText(xalign=0.5, yalign=0.46, size=60, font="gui/fonts/NotoSansSC-Regular.otf")
image chapter_name_text_sc = ParameterizedText(xalign=0.5, yalign=0.53, size=50, font="gui/fonts/NotoSansSC-Regular.otf")
## For Traditional Chinese
image screen_text_tc = ParameterizedText(xalign=0.5, yalign=0.5, size=40, font="gui/fonts/NotoSansTC-Regular.otf")
image chapter_text_tc = ParameterizedText(xalign=0.5, yalign=0.46, size=60, font="gui/fonts/NotoSansTC-Regular.otf")
image chapter_name_text_tc = ParameterizedText(xalign=0.5, yalign=0.53, size=50, font="gui/fonts/NotoSansTC-Regular.otf")
And then when implementing:

Code: Select all

if _preferences.language == "simplified_chinese":
    show chapter_text_sc "Chapter 1"
    show chapter_name_text_sc "Chapter Name Here"
else:        
    show chapter_text "Chapter 1"
    show chapter_name_text "Chapter Name Here"
Bear in mind this is just for simplified_chinese, I'll have to deploy an additional if conditional for traditional. Also, be aware that these English lines have already been translated in the translation folders, so I know it looks odd to have to write the same thing twice, but the one for simplified Chinese will be translated.

All this seems rather complex and unnecessary as I am sure there is a much easier, more succinct way to get this to work. Is there a way to somehow make it so I only need to declare one style and then it be switched when the user changes languages?

Re: Using ParameterizedText with Chinese language

Posted: Mon Sep 28, 2020 7:19 pm
by hell_oh_world
How about try this one.

Code: Select all

init python:
    def TranslatableDisplayable(default_displayable, **languages_and_displayables):
        def td(st, at):
            if _preferences.language is None:
                return default_displayable, 0

            else:
                for l, d in languages_and_displayables.items():
                    if _preferences.language == l:
                        return d, 0

                return Null(), 0

        return DynamicDisplayable(td)


image chapter_text = TranslatableDisplayable(
    ParameterizedText(bold=True, align=(0.5, 0.5)), # the displayable to use for the default language, when language is None
    french=ParameterizedText(italic=True, align=(0.5, 0.5)), # the displayable to use when language is set to 'french'
)

label start:
    show chapter_text "WELCOME!!!"
    
To save yourself from repetitive re-definition of styles if the difference only between each parameterized text is the font, then define a dict with the mutual styles.

Code: Select all

define cts = dict(bold=True, align=(0.5, 0.5))

image chapter_text = TranslatableDisplayable(
    ParameterizedText(font="english_font.ttf", **cts), # **cts make use of the predefined styles.
    french=ParameterizedText(font="french_font.ttf", **cts)
)
Or just use a style.

Code: Select all

style cts:
    bold True
    align (0.5, 0.5)

image chapter_text = TranslatableDisplayable(
    ParameterizedText("cts", font="english_font.ttf"), # pass the style name as the first parameter
    french=ParameterizedText("cts", font="french_font.ttf")
)
Also note that this displayable is flexible, so you can also use this to display other type of displayables depending on the language.

Code: Select all

image dynamic_color = TranslatableDisplayable(
    "#f00", french="#0f0"
)