feat: add writing language

This commit is contained in:
Michel Edighoffer 2023-01-16 00:14:55 +01:00
parent fe07c7effe
commit 155dbf0e90
4 changed files with 25 additions and 8 deletions

View file

@ -2,14 +2,17 @@
import type { ComputedRef } from 'vue' import type { ComputedRef } from 'vue'
import type { LocaleObject } from '#i18n' import type { LocaleObject } from '#i18n'
const userSettings = useUserSettings() const props = defineProps<{
language?: string
}>()
const model = useVModel(props, 'language')
const { locales } = useI18n() as { locales: ComputedRef<LocaleObject[]> } const { locales } = useI18n() as { locales: ComputedRef<LocaleObject[]> }
</script> </script>
<template> <template>
<select v-model="userSettings.language"> <select v-model="model">
<option v-for="item in locales" :key="item.code" :value="item.code" :selected="userSettings.language === item.code"> <option v-for="item in locales" :key="item.code" :value="item.code" :selected="props.language === item.code">
{{ item.name }} {{ item.name }}
</option> </option>
</select> </select>

View file

@ -1,5 +1,5 @@
import type { mastodon } from 'masto'
import type { ComputedRef, Ref } from 'vue' import type { ComputedRef, Ref } from 'vue'
import type { mastodon } from 'masto'
import { STORAGE_KEY_DRAFTS } from '~/constants' import { STORAGE_KEY_DRAFTS } from '~/constants'
import type { Draft, DraftMap } from '~/types' import type { Draft, DraftMap } from '~/types'
import type { Mutable } from '~/types/utils' import type { Mutable } from '~/types/utils'
@ -11,6 +11,10 @@ export const builtinDraftKeys = [
'home', 'home',
] ]
function getLanguageCodeFromLocale(localeCode: string) {
return localeCode.substring(0, 2)
}
export function getDefaultDraft(options: Partial<Mutable<mastodon.v1.CreateStatusParams> & Omit<Draft, 'params'>> = {}): Draft { export function getDefaultDraft(options: Partial<Mutable<mastodon.v1.CreateStatusParams> & Omit<Draft, 'params'>> = {}): Draft {
const { const {
attachments = [], attachments = [],
@ -20,9 +24,10 @@ export function getDefaultDraft(options: Partial<Mutable<mastodon.v1.CreateStatu
visibility, visibility,
sensitive, sensitive,
spoilerText, spoilerText,
language,
mentions, mentions,
} = options } = options
const userSettings = $(useUserSettings())
const postLanguage = getLanguageCodeFromLocale(userSettings.writingLanguage)
return { return {
attachments, attachments,
@ -33,7 +38,7 @@ export function getDefaultDraft(options: Partial<Mutable<mastodon.v1.CreateStatu
visibility: visibility || 'public', visibility: visibility || 'public',
sensitive: sensitive ?? false, sensitive: sensitive ?? false,
spoilerText: spoilerText || '', spoilerText: spoilerText || '',
language: language || 'en', language: postLanguage ?? 'en',
}, },
mentions, mentions,
lastUpdated: Date.now(), lastUpdated: Date.now(),
@ -66,6 +71,7 @@ function getAccountsToMention(status: mastodon.v1.Status) {
export function getReplyDraft(status: mastodon.v1.Status) { export function getReplyDraft(status: mastodon.v1.Status) {
const accountsToMention = getAccountsToMention(status) const accountsToMention = getAccountsToMention(status)
const { writingLanguage } = $(useUserSettings())
return { return {
key: `reply-${status.id}`, key: `reply-${status.id}`,
draft: () => { draft: () => {
@ -74,6 +80,7 @@ export function getReplyDraft(status: mastodon.v1.Status) {
inReplyToId: status!.id, inReplyToId: status!.id,
visibility: status.visibility, visibility: status.visibility,
mentions: accountsToMention, mentions: accountsToMention,
language: getLanguageCodeFromLocale(writingLanguage),
}) })
}, },
} }
@ -85,7 +92,6 @@ export const isEmptyDraft = (draft: Draft | null | undefined) => {
const { params, attachments } = draft const { params, attachments } = draft
const status = params.status || '' const status = params.status || ''
const text = htmlToText(status).trim().replace(/^(@\S+\s?)+/, '').replaceAll(/```/g, '').trim() const text = htmlToText(status).trim().replace(/^(@\S+\s?)+/, '').replaceAll(/```/g, '').trim()
return (text.length === 0) return (text.length === 0)
&& attachments.length === 0 && attachments.length === 0
&& (params.spoilerText || '').length === 0 && (params.spoilerText || '').length === 0

View file

@ -17,6 +17,7 @@ export interface UserSettings {
colorMode?: ColorMode colorMode?: ColorMode
fontSize: FontSize fontSize: FontSize
language: string language: string
writingLanguage: string
zenMode: boolean zenMode: boolean
} }
@ -29,6 +30,7 @@ export function getDefaultLanguage(languages: string[]) {
export function getDefaultUserSettings(locales: string[]): UserSettings { export function getDefaultUserSettings(locales: string[]): UserSettings {
return { return {
language: getDefaultLanguage(locales), language: getDefaultLanguage(locales),
writingLanguage: getDefaultLanguage(locales),
fontSize: DEFAULT_FONT_SIZE, fontSize: DEFAULT_FONT_SIZE,
zenMode: false, zenMode: false,
preferences: {}, preferences: {},

View file

@ -4,6 +4,8 @@ const { t } = useI18n()
useHeadFixed({ useHeadFixed({
title: () => `${t('settings.language.label')} | ${t('nav.settings')}`, title: () => `${t('settings.language.label')} | ${t('nav.settings')}`,
}) })
const userSettings = useUserSettings()
</script> </script>
<template> <template>
@ -16,7 +18,11 @@ useHeadFixed({
<div p6> <div p6>
<label space-y-2> <label space-y-2>
<p font-medium>{{ $t('settings.language.display_language') }}</p> <p font-medium>{{ $t('settings.language.display_language') }}</p>
<SettingsLanguage select-settings /> <SettingsLanguage v-model:language="userSettings.language" select-settings />
</label>
<label space-y-2>
<p font-medium>{{ $t('settings.language.writing_language') }}</p>
<SettingsLanguage v-model:language="userSettings.writingLanguage" select-settings />
</label> </label>
</div> </div>
</MainContent> </MainContent>