Skip to content

Commit

Permalink
Simpler te (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Jul 15, 2024
1 parent f261962 commit fd0a864
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/corpus/config/schema-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import type { JSONSchema6 } from "json-schema";
import { schemaWalk, type Visitor } from "@cloudflare/json-schema-walker";
import { useI18n } from "vue-i18n";
import { capitalize } from "lodash";
import useLocale from "@/i18n/locale.composable";

const isPropertyName = (name: string) =>
!/^[0-9]*$/.test(name) &&
!["properties", "allOf", "anyOf", "if", "then", "else", "not"].includes(name);

export function useTransformSchema() {
const { t, te } = useI18n();
const { t } = useI18n();
const { te } = useLocale();

function transformSchema(schema: JSONSchema6) {
schemaWalk(schema, undefined, postFunc);
Expand Down
8 changes: 0 additions & 8 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,3 @@ export const languageNames = {
sv: "Svenska",
en: "English",
};

// Thanks @jclaveau: https://github.com/kazupon/vue-i18n/issues/1521#issuecomment-1799047164
i18n.global.te = (key, locale?): boolean => {
const effectiveLocale =
locale && locale.length ? locale : i18n.global.locale.value;
const messages = i18n.global.messages.value[effectiveLocale];
return Object.prototype.hasOwnProperty.call(messages, key);
};
11 changes: 10 additions & 1 deletion src/i18n/locale.composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { ByLang, SvEn, SweEng } from "@/util.types";
const storedLocale = useStorage<SvEn | "">("locale", "");

export default function useLocale() {
const { locale } = useI18n();
const { locale, messages } = useI18n();
const formkitConfig = inject(configSymbol);

const exportLocale = () => {
Expand All @@ -35,6 +35,14 @@ export default function useLocale() {
exportLocale();
});

/**
* Check if translation exists.
* Original `te` broken as per https://github.com/kazupon/vue-i18n/issues/1521
*/
function te(key: string): boolean {
return !!messages.value[locale.value][key];
}

/** Translate here - picks the current language out of a strings-by-language object. */
function th(map?: ByLang | string): string | undefined {
if (!map) return undefined;
Expand All @@ -53,6 +61,7 @@ export default function useLocale() {
return {
locale,
locale3,
te,
th,
filesize: myFilesize,
};
Expand Down
4 changes: 3 additions & 1 deletion src/message/messenger.composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import remove from "lodash/remove";
import { AxiosError } from "axios";
import { randomString } from "@/util";
import type { MinkResponse } from "@/api/api.types";
import useLocale from "@/i18n/locale.composable";

export type Alert = {
key: string;
Expand All @@ -16,7 +17,8 @@ export type MessageLevel = "error" | "success" | "debug";
const alerts = ref<Alert[]>([]);

export default function useMessenger() {
const { t, te, locale, messages } = useI18n();
const { t } = useI18n();
const { te } = useLocale();

function alert(message: string, level?: MessageLevel) {
if (message && level !== "success") {
Expand Down

0 comments on commit fd0a864

Please sign in to comment.