Skip to content

Commit

Permalink
Merge pull request #270 from livingtongues/temporal-mayunmarka-glossi…
Browse files Browse the repository at this point in the history
…ng-sort

glossing sort depends on the dictionary glossLanguages order
  • Loading branch information
Danble authored Jan 20, 2023
2 parents 19ea58b + 20bc062 commit 80d973e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 69 deletions.
76 changes: 76 additions & 0 deletions packages/site/src/lib/helpers/glosses.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { orderEntryAndDictionaryGlossLanguages, orderGlosses } from './glosses';

describe('orderGlosses', () => {
const $t = (id: string) => {
switch (id) {
case 'gl.de':
return 'German';
case 'gl.en':
return 'English';
case 'gl.es':
return 'Spanish';
default:
return 'other';
}
};

const glosses = {
en: 'apple',
es: 'manzana',
scientific: '<i>Neolamarckia cadamba</i>',
empty: '',
null: null,
de: 'apfel',
};
const dictionaryGlossLanguages = ['de', 'es', 'en'];

test('orders based on dictionaryGlossLanguages first', () => {
expect(orderGlosses({ glosses, dictionaryGlossLanguages, $t })).toMatchInlineSnapshot(
`
[
"apfel",
"manzana",
"apple",
"<i>Neolamarckia cadamba</i>",
]
`
);
});

test('adds language label when label set to true', () => {
expect(orderGlosses({ glosses, dictionaryGlossLanguages, $t, label: true }))
.toMatchInlineSnapshot(`
[
"German: apfel",
"Spanish: manzana",
"English: apple",
"other: <i>Neolamarckia cadamba</i>",
]
`);
});

test('handles an empty glosses object', () => {
expect(orderGlosses({ glosses: {}, dictionaryGlossLanguages, $t })).toMatchInlineSnapshot('[]');
});

test('example implementation with join and italics removal', () => {
expect(
orderGlosses({ glosses, dictionaryGlossLanguages, $t })
.join(', ')
.replace(/<\/?i>/g, '') + '.'
).toMatchInlineSnapshot('"apfel, manzana, apple, Neolamarckia cadamba."');
});
});

describe('orderEntryAndDictionaryGlossLanguages', () => {
test('places dictionary gloss languages first, then leftovers from gloss object but does not duplicate', () => {
expect(orderEntryAndDictionaryGlossLanguages({ es: '', en: '' }, ['en', 'de']))
.toMatchInlineSnapshot(`
[
"en",
"de",
"es",
]
`);
});
});
71 changes: 20 additions & 51 deletions packages/site/src/lib/helpers/glosses.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,27 @@
import type { IGloss } from '@living-dictionaries/types';

export function printGlosses(glosses: IGloss, t: (id: string) => string, { shorten = false } = {}) {
return Object.keys(glosses)
.filter((bcp) => glosses[bcp])
.sort() // sort by dictionary's gloss language order in the future
.map((bcp) => {
const gloss = glosses[bcp];
if (shorten) return gloss;
return `${t('gl.' + bcp)}: ${gloss}`;
});
}

if (import.meta.vitest) {
const t = (id) => (id === 'gl.en' ? 'English' : 'Spanish');
test('printGlosses', () => {
const gloss = {
en: 'apple',
es: 'arbol',
scientific: '<i>Neolamarckia cadamba</i>',
empty: '',
null: null,
};
expect(printGlosses(gloss, t, { shorten: true })).toMatchInlineSnapshot(`
[
"apple",
"arbol",
"<i>Neolamarckia cadamba</i>",
]
`);

expect(printGlosses(gloss, t)).toMatchInlineSnapshot(`
[
"English: apple",
"Spanish: arbol",
"Spanish: <i>Neolamarckia cadamba</i>",
]
`);

expect(
printGlosses(gloss, t, { shorten: true })
.join(', ')
.replace(/<\/?i>/g, '') + '.'
).toMatchInlineSnapshot('"apple, arbol, Neolamarckia cadamba."');

expect(printGlosses({}, t)).toMatchInlineSnapshot('[]');
export function orderGlosses({ glosses, dictionaryGlossLanguages, $t, label = false }:
{
glosses: IGloss;
dictionaryGlossLanguages: string[],
$t: (id: string) => string,
label?: boolean
}
) {
const sortedGlossLanguages = orderEntryAndDictionaryGlossLanguages(glosses, dictionaryGlossLanguages);
const glossLanguagesWithGloss = sortedGlossLanguages.filter((bcp) => glosses[bcp])
return glossLanguagesWithGloss.map((bcp) => {
const gloss = glosses[bcp];
if (label) return `${$t('gl.' + bcp)}: ${gloss}`;
return gloss;
});
}

export function showEntryGlossLanguages(
entryGlosses: { [key: string]: string },
dictionaryLanguages: string[]
export function orderEntryAndDictionaryGlossLanguages(
glosses: IGloss,
dictionaryGlossLanguages: string[]
) {
if (entryGlosses) {
return [...new Set([...dictionaryLanguages, ...Object.keys(entryGlosses)])];
}
return [...new Set(dictionaryLanguages)];
const combinedGlossingLanguages = [...dictionaryGlossLanguages, ...Object.keys(glosses || {})]
const deduplicatedGlossingLanguages = [...new Set(combinedGlossingLanguages)]
return deduplicatedGlossingLanguages;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
import AddImage from '../AddImage.svelte';
import { page } from '$app/stores';
import type { IEntry } from '@living-dictionaries/types';
import { printGlosses } from '$lib/helpers/glosses';
import { orderGlosses, orderEntryAndDictionaryGlossLanguages } from '$lib/helpers/glosses';
import { minutesAgo } from '$lib/helpers/time';
import { deleteImage } from '$lib/helpers/delete';
import ShowHide from 'svelte-pieces/functions/ShowHide.svelte';
import { showEntryGlossLanguages } from '$lib/helpers/glosses';
import { dictionary } from '$lib/stores';
import sanitize from 'xss';
export let entry: IEntry,
canEdit = false,
videoAccess = false;
$: glosses = printGlosses(entry.gl, $t, {
shorten: $dictionary.id === 'jewish-neo-aramaic',
$: glosses = orderGlosses({
glosses: entry.gl,
dictionaryGlossLanguages: $dictionary.glossLanguages,
$t,
label: $dictionary.id !== 'jewish-neo-aramaic',
}).join(', ');
</script>

Expand Down Expand Up @@ -75,7 +77,7 @@
{entry.xs.vn}
</p>{/if}
{#if entry.xs}
{#each showEntryGlossLanguages(entry.gl, $dictionary.glossLanguages) as bcp}
{#each orderEntryAndDictionaryGlossLanguages(entry.gl, $dictionary.glossLanguages) as bcp}
{#if entry.xs[bcp]}
<p>
<span class="font-semibold"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { t } from 'svelte-i18n';
import { StandardPrintFields, type IEntry } from '@living-dictionaries/types';
import { semanticDomains } from '$lib/mappings/semantic-domains';
import { orderGlosses } from '$lib/helpers/glosses';
import { dictionary } from '$lib/stores';
import QrCode from './QrCode.svelte';
import sanitize from 'xss';
import { defaultPrintFields } from './printFields';
Expand Down Expand Up @@ -32,10 +34,13 @@
{entry.ph && selectedFields.ph ? `/${entry.ph}/` : ''}
<i>{entry.ps && selectedFields.ps ? entry.ps : ''}</i>
{#if entry.gl && selectedFields.gloss}
{#each Object.entries(entry.gl) as gloss, index}
<span>{@html sanitize(gloss[1])}</span>
{index < Object.entries(entry.gl).length - 1 ? ' - ' : ''}
{/each}
<span>
{@html sanitize(orderGlosses({
glosses: entry.gl,
dictionaryGlossLanguages: $dictionary.glossLanguages,
$t,
}).join(' - '))}
</span>
{/if}
<b>{entry.xv && selectedFields.example_sentence ? entry.xv : ''}</b>
{#if entry.xs && selectedFields.example_sentence}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import { deleteEntry } from '$lib/helpers/delete';
import { saveUpdateToFirestore } from '$lib/helpers/entry/update';
import SeoMetaTags from '$lib/components/SeoMetaTags.svelte';
import { printGlosses } from '$lib/helpers/glosses';
import { showEntryGlossLanguages } from '$lib/helpers/glosses';
import { orderGlosses, orderEntryAndDictionaryGlossLanguages } from '$lib/helpers/glosses';
import EntryDisplay from './EntryDisplay.svelte';
import type { PageData } from './$types';
Expand Down Expand Up @@ -67,17 +66,32 @@ bg-white pt-1 -mt-1">
{entry}
videoAccess={$dictionary.videoAccess || $admin > 0}
canEdit={$canEdit}
glossingLanguages={showEntryGlossLanguages(entry.gl, $dictionary.glossLanguages)}
glossingLanguages={orderEntryAndDictionaryGlossLanguages(entry.gl, $dictionary.glossLanguages)}
alternateOrthographies={$dictionary.alternateOrthographies || []}
on:valueupdate={(e) => saveUpdateToFirestore(e, entry.id, $dictionary.id)} />

<SeoMetaTags
title={entry.lx}
description={`${entry.lo ? entry.lo : ''} ${entry.lo2 ? entry.lo2 : ''} ${entry.lo3 ? entry.lo3 : ''}
${entry.ph ? '[' + entry.ph + ']' : ''} ${entry.ps ? typeof entry.ps !=='string' && entry.ps.length > 1 ? entry.ps.join(', ') + '.' : entry.ps + '.' : ''}
${printGlosses(entry.gl, $t)
.join(', ')
.replace(/<\/?i>/g, '') + '.'}
description={`${entry.lo ? entry.lo : ''} ${entry.lo2 ? entry.lo2 : ''} ${
entry.lo3 ? entry.lo3 : ''
}
${entry.ph ? '[' + entry.ph + ']' : ''} ${
entry.ps
? typeof entry.ps !== 'string' && entry.ps.length > 1
? entry.ps.join(', ') + '.'
: entry.ps + '.'
: ''
}
${
orderGlosses({
glosses: entry.gl,
dictionaryGlossLanguages: $dictionary.glossLanguages,
$t,
label: true,
})
.join(', ')
.replace(/<\/?i>/g, '') + '.'
}
${entry.di ? entry.di : ''}`.replace(/(?<!\w)\n/gm, '')}
dictionaryName={$dictionary.name}
lat={$dictionary.coordinates?.latitude}
Expand Down
1 change: 0 additions & 1 deletion packages/site/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const config: UserConfig = {
'import.meta.vitest': false,
'import.meta.env.VERCEL_ANALYTICS_ID': JSON.stringify(process.env.VERCEL_ANALYTICS_ID),
},
// @ts-ignore
test: {
// plugins: [svelte({ hot: !process.env.VITEST })],
globals: true,
Expand Down

1 comment on commit 80d973e

@vercel
Copy link

@vercel vercel bot commented on 80d973e Jan 20, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.