Zero-K icon indicating copy to clipboard operation
Zero-K copied to clipboard

Bulk i18n of epicmenu widget options

Open moreginger opened this issue 1 year ago • 8 comments

moreginger avatar Apr 04 '24 10:04 moreginger

This doesn't cover everything, but it's a good start. It seems to start and I went through all the settings pages - they seem OK?

using this script and some fixup:

import argparse
import json
import re

options_block_regex = re.compile(r'^options_(path|order)?\s*=')
options_start_regex = re.compile(r'^options\s*=\s*{')
widget_name_regex = re.compile(r'name\s*=\s*[\'"]([^\'"]+)[\'"],')
option_start_regex = re.compile(r'^\s+([a-zA-Z0-9_]+)\s*=\s*{')
option_string_regex = re.compile(r'^(\s+)(name|desc)\s*=\s*[\'"]([^\'"]+)[\'"],\s*$')

parser = argparse.ArgumentParser(prog='i18n_options')
parser.add_argument('filename')
parser.add_argument('--i18nfile', help='Path to translations file')

args = parser.parse_args()

filename = args.filename
i18nfile = args.i18nfile

def cleanup_string(s):
    return re.sub(r'\W+', '', s).replace('_', '').lower()

widget_prefix = None

new_content = []

epicmenu_json = None
with open(i18nfile) as json_file:
    epicmenu_json = json.load(json_file)

option_name = None

print('processing ' + filename + '...')

with open(filename) as lua_file:
    wrote_prefix = False
    in_options = False
    for line in lua_file.readlines():
        if 'i18nPrefix' in line:
            print('i18nPrefix already exists in ' + filename + '. Exiting...')
            exit(0)

        if not wrote_prefix and options_block_regex.search(line):
            new_content.append("i18nPrefix = '" + widget_prefix + "'\n")
            wrote_prefix = True

        if not widget_prefix and widget_name_regex.search(line):
            widget_prefix = cleanup_string(widget_name_regex.search(line).group(1)) + '_'
        elif options_start_regex.search(line):
            in_options = True
        elif line.startswith('}'):
            in_options = False
        elif line.endswith('},'):
            option_name = False
        elif option_start_regex.search(line):
            match = option_start_regex.search(line)
            option_name = match.group(1).lower() + '_option'
        elif in_options and option_string_regex.search(line):
            match = option_string_regex.search(line)
            content = match.group(3)
            if match.group(2) == 'name':
                epicmenu_json[widget_prefix + option_name] = content
                line = None
            elif match.group(2) == 'desc':
                epicmenu_json[widget_prefix + option_name + '_desc'] = content
                line = None

        if line:
            new_content.append(line)

if not option_name:
    print('No options found in ' + filename + '. Exiting...')
    exit(0)

with open(filename, 'w') as lua_file:
    lua_file.write(''.join(new_content))

with open(i18nfile, 'w') as json_file:
    json.dump(epicmenu_json, json_file, indent='\t', sort_keys=True, separators=(',', ' : '))

moreginger avatar Apr 04 '24 10:04 moreginger

I wish translations didn't make the rest of development so inconvenient.

GoogleFrog avatar Apr 04 '24 12:04 GoogleFrog

They are a pain, I've gotten used to them being annoying. One option is to have the English translation with the code, and not in a resources file. But you'd have to extract and store it for switching back to English.

moreginger avatar Apr 04 '24 12:04 moreginger

Monoliths are hard to review and there's a bunch of issues as above. I think ideally there would be a bunch of commits with, say, 10 widgets per commit and then they would be easy to review and mergeable individually.

sprunk avatar Apr 05 '24 13:04 sprunk

You can safely replace all the % with %% in that file.

Monoliths are hard to review

Hmm I thought you'd prefer to get it all done in one.

I'm not sure that I have the staying power to create a series of 14 PRs. Especially after having done a conversion twice already. I appreciate it's a massive pain to review the large change.

I think maybe if I make a couple of changes to the script it might be best if you use it in batches you are happy with. Perhaps you can feel happier that it is acting in a sensible way? I can let you know the 2 widgets that it breaks. That will leave 10 or 20 that either partially or don't i18n without changes, but we can chase those later.

moreginger avatar Apr 05 '24 16:04 moreginger

You might also want to review the technique. I wonder if it's better not to move the English translation to the resources file, @GoogleFrog didn't seem happy with it. I can imagine how that might work - providing the current value as a default when requesting translations, and store it as English if you don't have an English translation stored, but I think you'd want to consider the API carefully together. If I start trying to guess what you want then we'll be here a long time!

I guess that makes it harder to translate them though 🤷

moreginger avatar Apr 05 '24 16:04 moreginger

@sprunk I hope you're not waiting on me for anything.

GoogleFrog avatar May 04 '24 10:05 GoogleFrog

No, I'm waiting for chore mana from myself.

sprunk avatar May 04 '24 17:05 sprunk