Skip to content

Commit

Permalink
Merge pull request #89 from liamcain/fix-locale-configuration
Browse files Browse the repository at this point in the history
Fix locale configuration
  • Loading branch information
liamcain authored Dec 27, 2020
2 parents 233e06b + 4d8cbbe commit bb4b6f8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "calendar",
"name": "Calendar",
"description": "Calendar view of your daily notes",
"version": "1.4.8",
"version": "1.4.9",
"author": "Liam Cain",
"authorUrl": "https://github.com/liamcain/",
"isDesktopOnly": false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "calendar",
"version": "1.4.8",
"version": "1.4.9",
"description": "Calendar view of your daily notes",
"author": "liamcain",
"main": "main.js",
Expand Down
14 changes: 11 additions & 3 deletions src/localization.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ISettings } from "./settings";

const langToMomentLocale = {
en: "en-gb",
zh: "zh-cn",
Expand All @@ -22,12 +24,18 @@ const langToMomentLocale = {
ja: "ja",
};

export async function configureMomentLocale(): Promise<void> {
const obsidianLang = localStorage.getItem("language");
export async function configureMomentLocale(
settings: ISettings
): Promise<void> {
const obsidianLang = localStorage.getItem("language") || "en";
const systemLang = navigator.language?.toLowerCase();
const localeOverride = settings.localeOverride || "system-default";

let momentLocale = langToMomentLocale[obsidianLang];
if (systemLang.startsWith(obsidianLang)) {

if (localeOverride !== "system-default") {
momentLocale = localeOverride;
} else if (systemLang.startsWith(obsidianLang)) {
momentLocale = systemLang;
}

Expand Down
3 changes: 1 addition & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ export default class CalendarPlugin extends Plugin {
}

async onload(): Promise<void> {
configureMomentLocale();

this.register(
SettingsInstance.subscribe((value) => {
this.options = value;
configureMomentLocale(this.options);
})
);

Expand Down
34 changes: 34 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "obsidian-daily-notes-interface";

type IWeekStartOption = "sunday" | "monday" | "locale";
type ILocaleOverride = "system-default" | string;

export interface ISettings {
wordsPerDot: number;
Expand All @@ -21,6 +22,8 @@ export interface ISettings {
weeklyNoteFormat: string;
weeklyNoteTemplate: string;
weeklyNoteFolder: string;

localeOverride: ILocaleOverride;
}

export function getWeeklyNoteSettings(settings: ISettings): IDailyNoteSettings {
Expand All @@ -43,6 +46,8 @@ export const SettingsInstance = writable<ISettings>({
weeklyNoteFormat: "",
weeklyNoteTemplate: "",
weeklyNoteFolder: "",

localeOverride: "system-default",
});

export function syncMomentLocaleWithSettings(settings: ISettings): void {
Expand Down Expand Up @@ -106,6 +111,11 @@ export class CalendarSettingsTab extends PluginSettingTab {
"The calendar is best used in conjunction with the Daily Notes plugin. Enable it in your plugin settings for a more optimal experience.",
});
}

this.containerEl.createEl("h3", {
text: "Advanced Settings",
});
this.addLocaleOverrideSetting();
}

addDotThresholdSetting(): void {
Expand Down Expand Up @@ -212,4 +222,28 @@ export class CalendarSettingsTab extends PluginSettingTab {
});
});
}

addLocaleOverrideSetting(): void {
const { moment } = window;

const sysLocale = navigator.language?.toLowerCase();

new Setting(this.containerEl)
.setName("Override locale:")
.setDesc(
"Set this if you want to use a locale different from the default"
)
.addDropdown((dropdown) => {
dropdown.addOption("system-default", `Same as system (${sysLocale})`);
moment.locales().forEach((locale) => {
dropdown.addOption(locale, locale);
});
dropdown.setValue(this.plugin.options.localeOverride);
dropdown.onChange(async (value) => {
this.plugin.writeOptions(
(old) => (old.localeOverride = value as ILocaleOverride)
);
});
});
}
}

0 comments on commit bb4b6f8

Please sign in to comment.