Skip to content

Commit

Permalink
fix: rework translation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Nov 27, 2022
1 parent 54a5d98 commit 927bcb3
Show file tree
Hide file tree
Showing 14 changed files with 867 additions and 779 deletions.
32 changes: 16 additions & 16 deletions plugin/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {GithubBranch} from "./publishing/branch";
import { Octokit } from "@octokit/core";
import {MetadataCache, Notice, TFile, Vault} from "obsidian";
import GithubPublisher from "./main";
import t from './i18n'
import {error, informations} from './i18n'
import { StringFunc } from "./i18n";


Expand Down Expand Up @@ -43,7 +43,7 @@ export async function shareAllMarkedNotes(PublisherManager: GithubBranch, settin
} catch {
errorCount++;
new Notice(
(t("unablePublishNote") as StringFunc)(sharedFiles[files].name)
(error("unablePublishNote") as StringFunc)(sharedFiles[files].name)
);
}
}
Expand All @@ -54,13 +54,13 @@ export async function shareAllMarkedNotes(PublisherManager: GithubBranch, settin
if (update) {
await noticeMessage(PublisherManager, noticeValue, settings, repoFrontmatter);
} else {
new Notice((t("errorPublish") as StringFunc)(settings.githubRepo));
new Notice((error("errorPublish") as StringFunc)(settings.githubRepo));
}
}
} catch (error) {
console.error(error);
new Notice(
t("unablePublishMultiNotes") as string
error("unablePublishMultiNotes") as string
);
statusBar.error();
}
Expand All @@ -75,7 +75,7 @@ export async function deleteUnsharedDeletedNotes(PublisherManager: GithubBranch,
* @param branchName
*/
try {
new Notice((t("startingClean") as StringFunc)(settings.githubRepo))
new Notice((informations("startingClean") as StringFunc)(settings.githubRepo))
await PublisherManager.newBranch(branchName, repoFrontmatter);
await deleteFromGithub(false, settings,octokit, branchName, PublisherManager, repoFrontmatter);
await PublisherManager.updateRepository(branchName, repoFrontmatter);
Expand Down Expand Up @@ -104,14 +104,14 @@ export async function shareOneNote(branchName: string, PublisherManager: GithubB
await noticeMessage(PublisherManager, file, settings, repoFrontmatter);
await createLink(file, repoFrontmatter, metadataCache, vault, settings);
} else {
new Notice((t("errorPublish") as StringFunc)(settings.githubRepo));
new Notice((error("errorPublish") as StringFunc)(settings.githubRepo));
}
}
}
catch (error) {
if (!(error instanceof DOMException)) {
console.error(error);
new Notice((t("errorPublish") as StringFunc)(settings.githubRepo));
new Notice((error("errorPublish") as StringFunc)(settings.githubRepo));
}
}
}
Expand All @@ -126,18 +126,18 @@ export async function shareNewNote(PublisherManager: GithubBranch, octokit: Octo
* @class plugin
*/
const settings = plugin.settings;
new Notice(t("scanningRepo") as string);
new Notice(informations("scanningRepo") as string);
const branchMaster = settings.githubBranch;
const sharedFilesWithPaths = PublisherManager.getAllFileWithPath();
const githubSharedNotes = await PublisherManager.getAllFileFromRepo(branchMaster, octokit, settings, repoFrontmatter);
const newlySharedNotes = PublisherManager.getNewFiles(sharedFilesWithPaths, githubSharedNotes, vault);
if (newlySharedNotes.length > 0) {
new Notice((t("foundNoteToSend") as StringFunc)(`${newlySharedNotes.length}`));
new Notice((informations("foundNoteToSend") as StringFunc)(`${newlySharedNotes.length}`));
const statusBarElement = plugin.addStatusBarItem();
await PublisherManager.newBranch(branchName, repoFrontmatter);
await shareAllMarkedNotes(PublisherManager, plugin.settings, octokit, statusBarElement, branchName, repoFrontmatter, newlySharedNotes);
} else {
new Notice(t("noNewNote") as string);
new Notice(informations("noNewNote") as string);
}

}
Expand All @@ -152,19 +152,19 @@ export async function shareAllEditedNotes(PublisherManager: GithubBranch, octoki
* @class plugin
*/
const settings = plugin.settings;
new Notice(t("scanningRepo") as string);
new Notice(informations("scanningRepo") as string);
const branchMaster = settings.githubBranch;
const sharedFilesWithPaths = PublisherManager.getAllFileWithPath();
const githubSharedNotes = await PublisherManager.getAllFileFromRepo(branchMaster, octokit, settings, repoFrontmatter);
const newSharedFiles = PublisherManager.getNewFiles(sharedFilesWithPaths, githubSharedNotes, vault);
const newlySharedNotes = await PublisherManager.getEditedFiles(sharedFilesWithPaths, githubSharedNotes, vault, newSharedFiles);
if (newlySharedNotes.length > 0) {
new Notice((t("foundNoteToSend") as StringFunc)(`${newlySharedNotes.length}`));
new Notice((informations("foundNoteToSend") as StringFunc)(`${newlySharedNotes.length}`));
const statusBarElement = plugin.addStatusBarItem();
await PublisherManager.newBranch(branchName, repoFrontmatter);
await shareAllMarkedNotes(PublisherManager, settings, octokit, statusBarElement, branchName, repoFrontmatter, newlySharedNotes);
} else {
new Notice(t("noNewNote") as string);
new Notice(informations("noNewNote") as string);
}
}

Expand All @@ -178,18 +178,18 @@ export async function shareOnlyEdited(PublisherManager: GithubBranch, octokit: O
* @class plugin
*/
const settings = plugin.settings;
new Notice(t("scanningRepo") as string);
new Notice(informations("scanningRepo") as string);
const branchMaster = settings.githubBranch;
const sharedFilesWithPaths = PublisherManager.getAllFileWithPath();
const githubSharedNotes = await PublisherManager.getAllFileFromRepo(branchMaster, octokit, settings, repoFrontmatter);
const newSharedFiles:TFile[]=[]
const newlySharedNotes = await PublisherManager.getEditedFiles(sharedFilesWithPaths, githubSharedNotes, vault, newSharedFiles);
if (newlySharedNotes.length > 0) {
new Notice((t("foundNoteToSend") as StringFunc)(`${newlySharedNotes.length}`));
new Notice((informations("foundNoteToSend") as StringFunc)(`${newlySharedNotes.length}`));
const statusBarElement = plugin.addStatusBarItem();
await PublisherManager.newBranch(branchName, repoFrontmatter);
await shareAllMarkedNotes(PublisherManager, settings, octokit, statusBarElement, branchName, repoFrontmatter, newlySharedNotes);
} else {
new Notice(t("noNewNote") as string);
new Notice(informations("noNewNote") as string);
}
}
47 changes: 35 additions & 12 deletions plugin/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
*/

import { moment } from 'obsidian';
import enUS from './locales/en-us';
import zhCN from './locales/zh-cn';
import frFR from "./locales/fr-fr";
import ruRU from "./locales/ru";

const localeMap: { [k: string]: Partial<typeof enUS> } = {
enUS,
'zh-cn': zhCN,
'fr': frFR,
'ru': ruRU
import en from './locales/en';
import fr from './locales/fr';
import ru from './locales/ru';
import cn from './locales/zh-cn';

const localeMap: { [k: string]: Partial<typeof en> } = {
en: en,
fr: fr,
ru: ru,
'zh-cn': cn,
};

const locale = localeMap[moment.locale()];
Expand All @@ -42,6 +42,29 @@ export interface StringFunc {
(params: string|string[]): string;
}

export default function t(str: keyof typeof enUS): string | StringFunc {
return (locale && locale[str]) || enUS[str];
// export value from en locale into string map
export function error(str: string): string | StringFunc{
// @ts-ignore
return (locale && locale.error[str]) || en.error[str];
}

export function commands(key: string): string | StringFunc{
// @ts-ignore
return (locale && locale.commands[key]) || en.commands[key];
}

export function deletion(key: string): string | StringFunc{
// @ts-ignore
return (locale && locale.deletion[key]) || en.deletion[key];
}

export function settings(type:string, msg: string): string | StringFunc{
// @ts-ignore
return (locale && locale.settings[type][msg]) || en.settings[type][msg];
}

export function informations(key: string): string | StringFunc{
// @ts-ignore
return (locale && locale.informations[key]) || en.informations[key];
}

148 changes: 0 additions & 148 deletions plugin/i18n/locales/en-us.ts

This file was deleted.

Loading

0 comments on commit 927bcb3

Please sign in to comment.