Skip to content

Commit

Permalink
Merge pull request #38 from SARDONYX-sard/feature/implement-import-la…
Browse files Browse the repository at this point in the history
…ng-button

Feature/implement import lang button
  • Loading branch information
SARDONYX-sard authored Jan 19, 2024
2 parents cecbf88 + 8542bf6 commit 047798c
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 29 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
"**/node_modules/**": true,
"**/target/**": true
},
"i18n-ally.defaultNamespace": "en-US",
"rust-analyzer.check.command": "clippy"
}
50 changes: 50 additions & 0 deletions frontend/src/components/buttons/import_lang_btn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { FileOpen } from '@mui/icons-material';
import { Button, Tooltip } from '@mui/material';
import { toast } from 'react-hot-toast';

import { useTranslation } from '@/hooks';
import { importLang } from '@/tauri_cmd';

export const ImportLangButton = () => {
const { t } = useTranslation();

const handleClick = async () => {
try {
const [isCancelled, contents] = await importLang();
JSON.parse(contents); // Parse test
if (!isCancelled) {
localStorage.setItem('custom-translation-dict', contents);
localStorage.setItem('locale', 'custom');
window.location.reload(); // To enable
}
} catch (e) {
toast.error(`${e}`);
}
};

return (
<Tooltip
title={
<>
<p>{t('import-lang-tooltip')}</p>
<p>{t('import-lang-tooltip2')}</p>
</>
}
>
<Button
sx={{
height: '4em',
marginTop: '8px',
minWidth: '120px',
width: '120px',
}}
onClick={handleClick}
startIcon={<FileOpen />}
type="button"
variant="outlined"
>
{t('import-lang-btn')}
</Button>
</Tooltip>
);
};
1 change: 1 addition & 0 deletions frontend/src/components/buttons/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @index('./*', f => `export * from '${f.path}'`)
export * from './convert_btn';
export * from './import_lang_btn';
export * from './log_dir_btn';
export * from './log_file_btn';
export * from './path_selector';
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Box, Button } from '@mui/material';
import InputLabel from '@mui/material/InputLabel';
import AceEditor from 'react-ace';

import { ImportLangButton } from '@/components/buttons';
import { SelectEditorMode, StyleList, TranslationList } from '@/components/lists';
import { Toaster } from '@/components/notifications';
import { useDynStyle, useInjectScript, useLocale, useStorageState, useTranslation } from '@/hooks';
Expand Down Expand Up @@ -56,6 +57,7 @@ export default function Settings() {
>
<SelectEditorMode editorMode={selectEditorMode(editorMode)} setEditorMode={setEditorKeyMode} />
<StyleList preset={preset} setPreset={setPreset} setStyle={setStyle} />
<ImportLangButton />
<TranslationList />
</div>
<Help />
Expand Down
24 changes: 24 additions & 0 deletions frontend/src/tauri_cmd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,31 @@ export async function removeOarDir(path: string) {
await invoke('remove_oar_dir', { path });
}

/**
* Read the entire contents of a file into a string.
* @param {string} path - target path
*
* @return [isCancelled, contents]
* @throws
*/
export async function importLang() {
const langPathKey = 'lang-file-path';
let path = localStorage.getItem(langPathKey) ?? '';

const setPath = (newPath: string) => {
path = newPath;
localStorage.setItem(langPathKey, path);
};

const isCancelled = await openPath(path, setPath, false);
const contents = await invoke<string>('read_to_string', { path });
return [isCancelled, contents] as const;
}

/**
* Open a file or Dir
*
* @returns {Promise<boolean>} - isCancelled
* @throws
*/
export async function openPath(path: string, setPath: (path: string) => void, isDir: boolean) {
Expand All @@ -118,6 +140,8 @@ export async function openPath(path: string, setPath: (path: string) => void, is
//! If we don't get the result within this function, somehow the previous value comes in.
setPath(res);
}

return res === null;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"hide-dar-btn": "Hide DAR",
"hide-dar-btn-tooltip": "After conversion, add \".mohidden\" to all DAR files to hide them.(For MO2 user)",
"hide-dar-btn-tooltip2": "INFO: This will be effective when the destination of the OAR output is not specified.",
"import-lang-btn": "Import Language",
"import-lang-tooltip": "Import any language from a Json file. (automatically reloads for validation).",
"import-lang-tooltip2": "Note: For invalid Json, fall back to English. (See Wiki for how to write Json)",
"log-level-list-label": "Log Level",
"log-level-list-tooltip": "Minor log level encompasses the more critical log levels. (i.e. Error ⊂ Info)",
"log-level-list-tooltip2": "Debug: Logs data on the way of the converted condition.",
Expand All @@ -42,8 +45,8 @@
"mapping-wiki-url-leaf": "wiki#what-is-the-mapping-file",
"open-log-btn": "Open log",
"open-log-dir-btn": "Log(dir)",
"open-log-tooltip": "Open current log file.(Rotate to a new log file each time the application is launched.)",
"open-log-dir-tooltip": "Open the log storage location.",
"open-log-tooltip": "Open current log file.(Rotate to a new log file each time the application is launched.)",
"progress-btn": "ProgressBar",
"progress-btn-tooltip": "Display detail progress",
"progress-btn-tooltip2": "",
Expand Down
7 changes: 5 additions & 2 deletions locales/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"convert-form-dar-helper3": "\"[...]/animations/DynamicAnimationReplacer\" -> 3人称のみ変換",
"convert-form-dar-label": "DAR(src)ディレクトリ",
"convert-form-mapping-1st-label": "マッピングテーブルのパス(1人称用)",
"convert-form-mapping-help-link-name": "マッピングファイルとは?",
"convert-form-mapping-helper": "[任意] 優先番号とセクション名の対応付けが書かれたファイルを指定",
"convert-form-mapping-helper2": "ヘルプ: ",
"convert-form-mapping-help-link-name": "マッピングファイルとは?",
"convert-form-mapping-label": "マッピングテーブルのパス",
"convert-form-mod-name": "Mod名",
"convert-form-mod-name-helper": "[任意] ASCII(英語)推奨",
Expand All @@ -34,6 +34,9 @@
"hide-dar-btn": "DARを非表示",
"hide-dar-btn-tooltip": "変換後、DARの全ファイルに\".mohidden\"を追加して非表示化します(MO2ユーザ向け)",
"hide-dar-btn-tooltip2": "INFO: OARの出力先を指定しない場合、特に便利です。",
"import-lang-btn": "言語インポート",
"import-lang-tooltip": "Jsonファイルから任意の言語をインポートします。(有効化のため自動で再読み込みします)",
"import-lang-tooltip2": "注: 無効なJsonの場合、英語にフォールバックします。(Jsonの書き方はWikiを参照してください)",
"log-level-list-label": "ログレベル",
"log-level-list-tooltip": "軽度なログレベルはより重大なログレベルを包含します。(Error ⊂ Info)",
"log-level-list-tooltip2": "Debug: 変換されたconditionの途中データを記録します",
Expand All @@ -42,8 +45,8 @@
"mapping-wiki-url-leaf": "wiki#what-is-the-mapping-file",
"open-log-btn": "ログを閲覧",
"open-log-dir-btn": "ログ(dir)",
"open-log-tooltip": "現在のログファイルを開きます。(アプリを起動するたびに新しいログファイルにローテーションします)",
"open-log-dir-tooltip": "ログの格納場所を開きます。",
"open-log-tooltip": "現在のログファイルを開きます。(アプリを起動するたびに新しいログファイルにローテーションします)",
"progress-btn": "進捗バー",
"progress-btn-tooltip": "詳細な進捗状況を表示します",
"progress-btn-tooltip2": "",
Expand Down
9 changes: 7 additions & 2 deletions src-tauri/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,16 @@ pub(crate) async fn change_log_level(log_level: Option<&str>) -> Result<(), Stri
}

#[tauri::command]
pub(crate) async fn unhide_dar_dir(window: Window, dar_dir: &str) -> Result<(), String> {
time!("unhide_dar", unhide_dar(dar_dir, sender!(window)))
pub(crate) async fn read_to_string(path: &str) -> Result<String, String> {
std::fs::read_to_string(path).or_else(|err| bail!(err))
}

#[tauri::command]
pub(crate) async fn remove_oar_dir(window: Window, path: &str) -> Result<(), String> {
time!("remove_oar", remove_oar(path, sender!(window)))
}

#[tauri::command]
pub(crate) async fn unhide_dar_dir(window: Window, dar_dir: &str) -> Result<(), String> {
time!("unhide_dar", unhide_dar(dar_dir, sender!(window)))
}
1 change: 1 addition & 0 deletions src-tauri/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub fn run_tauri() -> anyhow::Result<()> {
crate::cmd::change_log_level,
crate::cmd::convert_dar2oar,
crate::cmd::convert_dar2oar_with_progress,
crate::cmd::read_to_string,
crate::cmd::remove_oar_dir,
crate::cmd::unhide_dar_dir,
])
Expand Down
52 changes: 29 additions & 23 deletions test/sample_scripts/custom_translation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(() => {
// Origin: https://github.com/SARDONYX-sard/dar-to-oar/blob/main/locales/en-US.json
const i18n = {
'all-clear-btn': '清除全部输入',
'all-clear-btn': '全部清除',
'conversion-complete': '转换完成',
'convert-btn': '转换',
'convert-form-author-name': 'Mod作者名',
Expand All @@ -12,14 +12,14 @@
'convert-form-dar-helper3': '"[...]/animations/DynamicAnimationReplacer" -> 仅转换第三人称',
'convert-form-dar-label': 'DAR(src)目录',
'convert-form-mapping-1st-label': '映射表路径(第一人称用)',
'convert-form-mapping-help-link-name': '什么是映射文件?',
'convert-form-mapping-helper': '[可选] 指定包含优先级和部分名称对应关系的文件',
'convert-form-mapping-helper2': '帮助: ',
'convert-form-mapping-help-link-name': '什么是映射文件?',
'convert-form-mapping-label': '映射表路径',
'convert-form-mod-name': 'Mod名称',
'convert-form-mod-name-helper': '[可选] 推荐使用ASCII(英语)',
'convert-form-oar-helper': '[可选] 指定OAR的输出位置(例如: "NewMod" -> "NewMod/meshes/[...])"',
'convert-form-oar-helper2': '如果未指定,OAR将在DAR的同级目录创建。',
'convert-form-mod-name-helper': '[可选] 推荐使用ASCII(英文)',
'convert-form-oar-helper': '[可选] 指定OAR的输出目录(例如: "NewMod" -> "NewMod/meshes/[...])"',
'convert-form-oar-helper2': '如果未指定,则OAR将在与DAR相同的位置创建',
'convert-form-oar-label': 'OAR(dist)目录',
'converting-btn': '转换中...',
'css-preset-list-item0': '自定义',
Expand All @@ -29,44 +29,50 @@
'css-preset-list-item4': '预设4',
'css-preset-list-label': '预设',
'css-preset-list-tooltip': '选择CSS预设',
'css-preset-list-tooltip2': '注意: 编辑"预设"将覆盖"自定义"',
'css-preset-list-tooltip2': '注意: 编辑"预设"会覆盖"自定义"',
'custom-css-label': '当前应用的CSS',
'custom-js-label': '自定义JavaScript(请勿运行不可信脚本)',
'custom-js-label': '自定义JavaScript(请勿执行不可信脚本)',
'editor-mode-list-label': '编辑模式',
'hide-dar-btn': '隐藏DAR',
'hide-dar-btn-tooltip': '在转换后,将“.mohidden”添加到DAR的所有文件中,使其不可见(MO2用户专用)',
'hide-dar-btn-tooltip2': '信息: 如果未指定OAR的输出位置,这特别方便。',
'hide-dar-btn-tooltip': '在转换后,将".mohidden"添加到DAR的所有文件中以隐藏它们(MO2用户专用)',
'hide-dar-btn-tooltip2': '信息: 如果未指定OAR的输出目录,则这很方便。',
'import-lang-btn': '导入语言',
'import-lang-tooltip': '从Json文件导入任何语言。(启用后会自动重新加载)',
'import-lang-tooltip2': '注意: 如果Json无效,将回退到英语。(请参考Wiki了解Json的写法)',
'log-level-list-label': '日志级别',
'log-level-list-tooltip': '轻微的日志级别包含更严重的日志级别。(Error ⊂ Info)',
'log-level-list-tooltip': '较轻的日志级别包含更严重的日志级别。(Error ⊂ Info)',
'log-level-list-tooltip2': 'Debug: 记录转换条件的中间数据',
'log-level-list-tooltip3': ' Info: 记录转换时间',
'log-level-list-tooltip4': 'Error: 仅记录严重错误',
'log-level-list-tooltip3': 'Info: 记录转换时间',
'log-level-list-tooltip4': 'Error: 除了严重错误外不记录',
'mapping-wiki-url-leaf': 'wiki#what-is-the-mapping-file',
'open-log-btn': '查看日志',
'open-log-dir-btn': '日志(dir)',
'open-log-tooltip': '打开当前日志文件。(每次启动应用都会轮换到新的日志文件)',
'open-log-dir-tooltip': '打开日志存储位置。',
'open-log-dir-btn': '打开日志(目录)',
'open-log-dir-tooltip': '打开日志的存储位置。',
'open-log-tooltip': '打开当前的日志文件。(每次启动应用程序时都会轮换到新的日志文件)',
'progress-btn': '进度条',
'progress-btn-tooltip': '显示详细进度情况',
'progress-btn-tooltip': '显示详细的进度信息',
'progress-btn-tooltip2': '',
'remove-oar-btn': '删除OAR',
'remove-oar-failed': '未找到"OpenAnimationReplacer"目录',
'remove-oar-specify-error': '未输入DAR或OAR',
'remove-oar-success': '已删除OAR目录',
'remove-oar-tooltip': '从OAR(dist)(如果没有则从DAR(src))中搜索并删除OAR目录',
'remove-oar-tooltip': '从OAR(dist)(如果不存在则从DAR(src))中搜索并删除OAR目录',
'run-parallel-btn-tooltip': '使用多线程',
'run-parallel-btn-tooltip2':
'注意: 可望获得2倍以上的处理速度,但由于并行处理,线程结束的顺序是无序的,日志写入也是无序的,可读性大幅下降。',
'注意: 可以期望2倍以上的处理速度,但由于并行处理,线程的结束时间可能是无序的,因此日志写入也是无序的,降低了日志的可读性。',
'run-parallel-label': '并行执行',
'select-btn': '选择',
'unhide-dar-btn': 'DAR重新显示',
'unhide-dar-btn-tooltip': '取消"隐藏DAR"使其重新显示(MO2用户专用)',
'unhide-dar-failed': '未找到带有扩展名.mohidden的文件',
'unhide-dar-btn': '解除DAR隐藏',
'unhide-dar-btn-tooltip': '取消"隐藏DAR"以取消隐藏(MO2用户专用)',
'unhide-dar-failed': '未找到带有扩展名".mohidden"的文件',
'unhide-dar-specify-error': '请指定DAR(src)',
'unhide-dar-success': '已取消DAR的隐藏',
'unhide-dar-success': '已解除DAR的隐藏',
};

const setCustomTranslation = () => localStorage.setItem('custom-translation-dict', JSON.stringify(i18n));
const setCustomTranslation = () => {
localStorage.setItem('custom-translation-dict', JSON.stringify(i18n));
localStorage.setItem('locale', 'custom');
};
const clearCustomTranslation = () => localStorage.removeItem('custom-translation-dict');

// You can turn them on and off by deleting the `//` in these functions.
Expand Down

0 comments on commit 047798c

Please sign in to comment.