Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up json for tradional chinese and english. #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/i18next/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { moment } from 'obsidian';
import * as resources from './locales'

type I18Config = ReturnType<typeof manuI18Config>
export const i18next = (async function(global, factory) {

const i18next = global.i18next
if (i18next) {
return await factory(i18next)
}
throw new Error("No instance of i18 found.")
})(
globalThis,
async function factory(i18, config = manuI18Config()) {
const current_lang = moment.locale();
const _config: I18Config = {
...manuI18Config(),
lng: current_lang,
...config
}
// middleware
await i18.init(_config)
return i18;
}
);


function manuI18Config(config = {}) {
return ({
lng: 'en',
debug: true,
defaultNS: "translation",
ns: "translation",
resources,
...config
})
};





25 changes: 25 additions & 0 deletions src/i18next/locales/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const RENAME = "Rename";
export const CREATE_TAG_PAGE = "Create tag page";
export const CREATE_A_TAG = "Create A Tag";
export const OPEN_TAG_PAGE = "Open tag page";
export const NEW_SEARCH_FOR = "New search for";
export const REQUIRE = "Require";
export const IN_SEARCH = "in search";
export const EXCLUDE = "Exclude";
export const FROM_SEARCH = "from search";
export const enKeys = [
RENAME,
CREATE_TAG_PAGE,
CREATE_A_TAG,
OPEN_TAG_PAGE,
NEW_SEARCH_FOR,
REQUIRE,
IN_SEARCH,
EXCLUDE,
FROM_SEARCH
] as const;

const _en = enKeys.map((en_key) => ([en_key, en_key]))
export default {
translation: Object.fromEntries(_en)
}
3 changes: 3 additions & 0 deletions src/i18next/locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

export {default as en} from "./en"
export {default as zhTW} from "./zh-tw"
29 changes: 29 additions & 0 deletions src/i18next/locales/zh-tw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {enKeys, REQUIRE, IN_SEARCH, EXCLUDE, FROM_SEARCH, OPEN_TAG_PAGE} from "./en"
type tKey = typeof enKeys[number]

type PartialTranslation = Partial<Record<tKey,string>>
type Namespace = "translation"

const translation: PartialTranslation = {
"Rename": "重命名",
["Create A Tag"]: "创建標籤",
["Create tag page"]: "创建標籤葉",
["New search for"]: "重新搜索",
[OPEN_TAG_PAGE]: "打開標簽葉",
[REQUIRE]: "必须先包含",
[IN_SEARCH]: "然后搜索",
[EXCLUDE]: "排除",
[FROM_SEARCH]: "在搜索之外",
}



type TranslationObject = {
[K in Namespace]: PartialTranslation;
};

const translationObject: TranslationObject = {
translation,
};

export default translationObject;
41 changes: 35 additions & 6 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Component, Keymap, Menu, Notice, parseFrontMatterAliases, Plugin, Scope}
import {renameTag, findTargets} from "./renaming";
import {Tag} from "./Tag";
import {around} from "monkey-around";
import {i18next} from './i18next/locale';
import { CREATE_A_TAG, enKeys, EXCLUDE, FROM_SEARCH, NEW_SEARCH_FOR, OPEN_TAG_PAGE} from './i18next/locales/en';

const tagHoverMain = "tag-wrangler:tag-pane";

Expand All @@ -10,9 +12,14 @@ function onElement(el, event, selector, callback, options) {
return () => el.off(event, selector, callback, options);
}



export default class TagWrangler extends Plugin {
pageAliases = new Map();
tagPages = new Map();
i18nextInstance;
it = {}
translate = (text) => text;

tagPage(tag) {
return Array.from(this.tagPages.get(Tag.canonical(tag)) || "")[0]
Expand Down Expand Up @@ -51,6 +58,24 @@ export default class TagWrangler extends Plugin {
this.register(
onElement(document, "contextmenu", ".tag-pane-tag", this.onMenu.bind(this), {capture: true})
);
try {

let i18 = await i18next;
if (i18) {
this.i18nextInstance = i18;
this.translate = function(text) {
return i18.t(text);
}
}
} catch(err) {
console.log({err})
}

this.it = enKeys.reduce((chain, itKey) => {
chain[itKey] = this.translate(itKey);
return chain;
},{})


this.app.workspace.registerHoverLinkSource(tagHoverMain, {display: 'Tag pane', defaultMod: true});

Expand Down Expand Up @@ -175,6 +200,7 @@ export default class TagWrangler extends Plugin {
setTimeout(() => menu.showAtPosition({x: e.pageX, y: e.pageY}), 0);
}


const
tagName = tagEl.find(".tag-pane-tag-text, .tag-pane-tag .tree-item-inner-text").textContent,
tagPage = this.tagPage(tagName),
Expand All @@ -183,30 +209,31 @@ export default class TagWrangler extends Plugin {
search = searchPlugin && searchPlugin.instance,
query = search && search.getGlobalSearchQuery(),
random = this.app.plugins.plugins["smart-random-note"],
menu = e.obsidian_contextmenu.addItem(item("pencil", "Rename #"+tagName, () => this.rename(tagName)));
menu = e.obsidian_contextmenu.addItem(item("pencil", `${this.it.Rename} #`+tagName, () => this.rename(tagName)));

menu.addSeparator();
if (tagPage) {
menu.addItem(
item("popup-open", "Open tag page", (e) => this.openTagPage(tagPage, false, Keymap.isModEvent(e)))
item("popup-open", this.it[OPEN_TAG_PAGE], (e) => this.openTagPage(tagPage, false, Keymap.isModEvent(e)))
)
} else {
menu.addItem(
item("create-new", "Create tag page", (e) => this.createTagPage(tagName, Keymap.isModEvent(e)))
item("create-new",this.it[CREATE_A_TAG], (e) => this.createTagPage(tagName, Keymap.isModEvent(e)))
)
}

if (search) {
menu.addSeparator().addItem(
item("magnifying-glass", "New search for #"+tagName, () => search.openGlobalSearch("tag:" + tagName))
item("magnifying-glass", `${this.it[NEW_SEARCH_FOR]} #`+tagName, () => search.openGlobalSearch("tag:" + tagName))
);
if (query) {
menu.addItem(
item("sheets-in-box", "Require #"+tagName+" in search" , () => search.openGlobalSearch(query+" tag:" + tagName))
item("sheets-in-box", `${this.it[REQUIRE]} #${tagName} ${this.it[IN_SEARCH]}`,
() => search.openGlobalSearch(query+" tag:" + tagName))
);
}
menu.addItem(
item("crossed-star" , "Exclude #"+tagName+" from search", () => search.openGlobalSearch(query+" -tag:" + tagName))
item("crossed-star" , `${this.it[EXCLUDE]} #${tagName} ${this.it[FROM_SEARCH]}`, () => search.openGlobalSearch(query+" -tag:" + tagName))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In short, this violates the very basics of translation.

One must NEVER concatenate strings for any reason.

Word order varies by language. Words need to conjugation to agree with substituted elements (gender agreement, case agreement (declination), sound-agreement (different words used depending if e/i or o/u are in the object, etc).

This fails for languages that:

  • Place Verb Last (JP) - #tag from-search exclude
  • Require conjugation of tag (FI) exclude #tag":n" from-search
  • Place Complement First - from-search exclude #tag
  • That split the components - search #tag-target-from exclude
  • That require reduplication - #tag-from-search #tag-target exclude
    Etc.

);
}

Expand Down Expand Up @@ -267,9 +294,11 @@ class TagPageUIHandler extends Component {
super();
this.opts = opts
this.plugin = plugin;

}

onload() {

const {selector, container, hoverSource, toTag} = this.opts;
this.register(
// Show tag page on hover
Expand Down