Skip to content

Commit

Permalink
feat: vue named slot syntax (#3327)
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneBalabai authored Apr 15, 2024
1 parent 6cbf012 commit d904085
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/vue/src/T.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
TranslateProps,
TranslationKey,
} from '@tolgee/web';
import { defineComponent, PropType } from 'vue';
import { defineComponent, PropType, SetupContext, computed } from 'vue';
import { useTranslateInternal } from './useTranslateInternal';

export const T = defineComponent({
Expand All @@ -20,14 +20,22 @@ export const T = defineComponent({
ns: { type: String as PropType<NsType> },
language: { type: String as PropType<string> },
},
setup() {
setup(props, context: SetupContext) {
const { slots } = context;
const { t } = useTranslateInternal();
return { t };
const assignedParams = computed(() => {
const slotsParams = {};
Object.keys(slots).forEach((key) => {
slotsParams[key] = slots[key]();
});
return Object.assign({}, props.params, slotsParams);
});
return { t, assignedParams };
},
render() {
const params: TranslateProps = {
key: this.$props.keyName,
params: this.$props.params,
params: this.assignedParams,
defaultValue: this.$props.defaultValue,
noWrap: this.$props.noWrap,
ns: this.$props.ns,
Expand Down
12 changes: 12 additions & 0 deletions packages/vue/src/__integration/T.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ const TestComponent = {
<div data-testid="with_language_prop">
<T keyName="hello_world" language="en" />
</div>
<div data-testid="peter_dogs_slot">
<T keyName="peter_dogs">
<template #dogsCount><span class='test_class'>5</span></template>
</T>
</div>
</div>`,
};

Expand Down Expand Up @@ -111,6 +116,13 @@ describe('T component integration', () => {
);
});

it('works with slots', () => {
expect(screen.queryByTestId('peter_dogs_slot').innerHTML).toContain(
'Petr má <span class="test_class">5</span> psů.'
);
expect(screen.queryByTestId('peter_dogs')).toHaveAttribute('_tolgee');
});

describe('language switch', () => {
beforeEach(async () => {
await tolgee.changeLanguage('en');
Expand Down
14 changes: 14 additions & 0 deletions testapps/vue/src/TranslationMethods.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@
</div>
</div>

<div>
<h1>T component with Slots syntax</h1>
<div>
<T keyName="this_is_a_key_with_params">
<template #key>
<span class="custom_class">value</span>
</template>
<template #key2>
<a href="#">value2</a>
</template>
</T>
</div>
</div>

<div>
<h1>t function without default</h1>
<div>{{ $t('this_is_a_key') }}</div>
Expand Down

0 comments on commit d904085

Please sign in to comment.