Skip to content

Commit

Permalink
translate the pre-restore backup comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Dec 12, 2023
1 parent 66e9274 commit c7f1ba2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ prime_backup:
countdown: '{} second later, the world will §cbe restored§r to {}'
countdown.hover: Click me or enter command {} to abort restore
no_confirm: No choice has been made, restore task aborted
pre_restore_comment: 'Automatic backup before restoring to #{}'
aborted: Restore task aborted
backup_set_tag:
name: set backup tag
Expand Down Expand Up @@ -380,6 +379,7 @@ prime_backup:
backup_comment:
none: no comment
scheduled_backup: Scheduled Backup
pre_restore: 'Automatic backup before restoring to #{}'
backup_full:
creator: 'Creator: {}'
restore: Restore to backup {}
Expand Down
2 changes: 1 addition & 1 deletion lang/zh_cn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ prime_backup:
countdown: '{}秒后将§c回档§r至{}'
countdown.hover: 点我或输入{}以终止回档
no_confirm: 未做出选择, 回档任务中止
pre_restore_comment: '回档至#{}前的自动备份'
aborted: 回档任务中止
backup_set_tag:
name: 设置备份标签
Expand Down Expand Up @@ -380,6 +379,7 @@ prime_backup:
backup_comment:
none:
scheduled_backup: 定时备份
pre_restore: '回档至#{}前的自动备份'
backup_full:
creator: '创建者: {}'
restore: 回档至备份{}
Expand Down
3 changes: 2 additions & 1 deletion prime_backup/mcdr/task/backup/restore_backup_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from prime_backup.types.backup_info import BackupInfo
from prime_backup.types.backup_tags import BackupTags, BackupTagName
from prime_backup.types.operator import Operator, PrimeBackupOperatorNames
from prime_backup.utils import backup_utils
from prime_backup.utils.mcdr_utils import click_and_run, mkcmd
from prime_backup.utils.timer import Timer

Expand Down Expand Up @@ -79,7 +80,7 @@ def run(self):
self.logger.info('Creating backup of existing files to avoid idiot')
CreateBackupAction(
Operator.pb(PrimeBackupOperatorNames.pre_restore),
self.tr('pre_restore_comment', backup.id).to_plain_text(),
backup_utils.create_translated_backup_comment('pre_restore', backup.id),
tags=BackupTags().set(BackupTagName.pre_restore_backup, True),
).run()
cost_backup = timer.get_and_restart()
Expand Down
7 changes: 5 additions & 2 deletions prime_backup/mcdr/text_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ def backup_brief(cls, backup: BackupInfo, *, backup_id_fancy: bool = True) -> RT
@classmethod
def backup_comment(cls, comment: str) -> RTextBase:
if len(comment) > 0:
if (key := backup_utils.extract_backup_comment_translation_key(comment)) is not None:
return cls.tr(f'backup_comment.{key}')
if (er := backup_utils.extract_backup_comment_translation_key(comment)) is not None:
args = er.args
if er.key == 'pre_restore' and len(args) == 0:
args = ('?',)
return cls.tr(f'backup_comment.{er.key}', *args)
return RText(comment)
else:
return cls.tr('backup_comment.none').set_color(RColor.gray).set_styles(RStyle.italic)
Expand Down
22 changes: 16 additions & 6 deletions prime_backup/utils/backup_utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import re
from typing import Optional

from typing import Optional, NamedTuple, Tuple

_PATTERN_WORDS = re.compile(r'\w+')
_PATTERN_EXTRACT = re.compile(r'__pb_translated__:(\w+)')
_PATTERN_EXTRACT_WITH_ARGS = re.compile(r'__pb_translated__:(\w+):(.*)')


def create_translated_backup_comment(key: str):
def create_translated_backup_comment(key: str, *args) -> str:
if not _PATTERN_WORDS.fullmatch(key):
raise ValueError(key)
return f'__pb_translated__:{key}'
comment = f'__pb_translated__:{key}'
if len(args) > 0:
comment += ':' + '|'.join(map(str, args))
return comment


class ExtractResult(NamedTuple):
key: str
args: Tuple[str, ...]


def extract_backup_comment_translation_key(comment: str) -> Optional[None]:
def extract_backup_comment_translation_key(comment: str) -> Optional[ExtractResult]:
if (match := _PATTERN_EXTRACT.fullmatch(comment)) is not None:
return match.group(1)
return ExtractResult(match.group(1), ())
if (match := _PATTERN_EXTRACT_WITH_ARGS.fullmatch(comment)) is not None:
return ExtractResult(match.group(1), tuple(match.group(2).split('|')))
return None

0 comments on commit c7f1ba2

Please sign in to comment.