Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable default value providing TG-335 #445

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions e2e/cypress/integration/react/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ context('Base test', () => {
cy.get('select').select('de');
cy.contains('Hallo Welt!');
});

it('shows default value when provided ', () => {
cy.contains('This is default').should('be.visible');
});

it('shows key as default when default not provided ', () => {
cy.contains('unknown key').should('be.visible');
});
});
94 changes: 89 additions & 5 deletions packages/core/src/Tolgee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('Tolgee', () => {
propertiesMock.mock.instances[0].config.mode = 'development';
const translated = await tolgee.translate(dummyKey, dummyParams);

expect(mockedWrap).toBeCalledWith(dummyKey, dummyParams);
expect(mockedWrap).toBeCalledWith(dummyKey, dummyParams, undefined);
expect(mockedTranslate).not.toBeCalled();
expect(translated).toEqual(wrappedDummyText);
});
Expand All @@ -165,7 +165,13 @@ describe('Tolgee', () => {

expect(translated).toEqual(translatedDummyText);
expect(mockedWrap).not.toBeCalled();
expect(mockedTranslate).toBeCalledWith(dummyKey, dummyParams);
expect(mockedTranslate).toBeCalledWith(
dummyKey,
dummyParams,
undefined,
undefined,
undefined
);
});

test('will not wrap when development is on, but noWrap is true', async () => {
Expand All @@ -181,6 +187,41 @@ describe('Tolgee', () => {
await tolgee.translate(dummyKey, dummyParams);
expect(mockedLoadTranslations).toBeCalled();
});

test('passes default value to wrap fn', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
await tolgee.translate(dummyKey, dummyParams, false, 'Default');
expect(mockedWrap).toBeCalledWith('dummyText', {}, 'Default');
});

test('passes default value to translate fn', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
await tolgee.translate(dummyKey, dummyParams, true, 'Default');
expect(mockedTranslate).toBeCalledWith(
'dummyText',
{},
undefined,
undefined,
'Default'
);
});

test('props object works correctly', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
await tolgee.translate({
key: dummyKey,
params: dummyParams,
noWrap: true,
defaultValue: 'Default',
});
expect(mockedTranslate).toBeCalledWith(
dummyKey,
dummyParams,
undefined,
undefined,
'Default'
);
});
});

describe('sync instant', () => {
Expand All @@ -189,7 +230,7 @@ describe('Tolgee', () => {
const dummyParams = {};
const translated = tolgee.instant(dummyKey, dummyParams);

expect(mockedWrap).toBeCalledWith(dummyKey, dummyParams);
expect(mockedWrap).toBeCalledWith(dummyKey, dummyParams, undefined);
expect(mockedInstant).not.toBeCalled();
expect(translated).toEqual(wrappedDummyText);
});
Expand All @@ -204,6 +245,7 @@ describe('Tolgee', () => {
dummyKey,
dummyParams,
undefined,
undefined,
undefined
);
});
Expand All @@ -214,7 +256,8 @@ describe('Tolgee', () => {
dummyKey,
dummyParams,
undefined,
true
true,
undefined
);
});

Expand All @@ -232,9 +275,50 @@ describe('Tolgee', () => {
dummyKey,
dummyParams,
undefined,
true
true,
undefined
);
});

test('passes default value to wrap fn', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
const dummyParams = {};
tolgee.instant(dummyKey, dummyParams, false, false, 'Default');

expect(mockedWrap).toBeCalledWith(dummyKey, dummyParams, 'Default');
});

test('props object works correctly', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
await tolgee.instant({
key: dummyKey,
params: dummyParams,
orEmpty: false,
noWrap: true,
defaultValue: 'Default',
});
expect(mockedInstant).toBeCalledWith(
dummyKey,
dummyParams,
undefined,
false,
'Default'
);
});
});

test('passes default value to instant fn', async () => {
propertiesMock.mock.instances[0].config.mode = 'development';
const dummyParams = {};
tolgee.instant(dummyKey, dummyParams, true, false, 'Default');

expect(mockedInstant).toBeCalledWith(
dummyKey,
dummyParams,
undefined,
false,
'Default'
);
});
});

Expand Down
76 changes: 63 additions & 13 deletions packages/core/src/Tolgee.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TolgeeConfig } from './TolgeeConfig';
import { TranslationParams } from './types';
import { InstantProps, TranslateProps, TranslationParams } from './types';
import { NodeHelper } from './helpers/NodeHelper';
import { EventEmitterImpl } from './services/EventEmitter';
import { DependencyStore } from './services/DependencyStore';
Expand Down Expand Up @@ -105,35 +105,85 @@ export class Tolgee {
);
}

translate = async (
async translate(props: TranslateProps): Promise<string>;
async translate(
key: string,
params: TranslationParams,
noWrap?: boolean,
defaultValue?: string
): Promise<string>;

async translate(
keyOrProps: string | TranslateProps,
params: TranslationParams = {},
noWrap = false
): Promise<string> => {
noWrap = false,
defaultValue: string | undefined = undefined
): Promise<string> {
const key = typeof keyOrProps === 'string' ? keyOrProps : keyOrProps.key;
if (typeof keyOrProps === 'object') {
const props = keyOrProps as TranslateProps;
// if values are not provided in props object, get them from function
// params defaults
params = props.params !== undefined ? props.params : params;
noWrap = props.noWrap !== undefined ? props.noWrap : noWrap;
defaultValue =
props.defaultValue !== undefined ? props.defaultValue : defaultValue;
}

if (this.properties.config.mode === 'development' && !noWrap) {
await this.loadScopes();
await this.translationService.loadTranslations();
return this.dependencyStore.textService.wrap(key, params);
return this.dependencyStore.textService.wrap(key, params, defaultValue);
}
return this.dependencyStore.textService.translate(key, params);
};
return this.dependencyStore.textService.translate(
key,
params,
undefined,
undefined,
defaultValue
);
}

instant = (
instant(
key: string,
params?: TranslationParams,
noWrap?: boolean,
orEmpty?: boolean,
defaultValue?: string
): string;

instant(props: InstantProps): string;

instant(
keyOrProps: string | InstantProps,
params: TranslationParams = {},
noWrap = false,
orEmpty?: boolean
): string => {
orEmpty?: boolean,
defaultValue?: string
): string {
const key = typeof keyOrProps === 'string' ? keyOrProps : keyOrProps.key;
if (typeof keyOrProps === 'object') {
const props = keyOrProps as InstantProps;
// if values are not provided in props object, get them from function
// params defaults
params = props.params !== undefined ? props.params : params;
noWrap = props.noWrap !== undefined ? props.noWrap : noWrap;
defaultValue =
props.defaultValue !== undefined ? props.defaultValue : defaultValue;
orEmpty = props.orEmpty !== undefined ? props.orEmpty : orEmpty;
}

if (this.properties.config.mode === 'development' && !noWrap) {
return this.dependencyStore.textService.wrap(key, params);
return this.dependencyStore.textService.wrap(key, params, defaultValue);
}
return this.dependencyStore.textService.instant(
key,
params,
undefined,
orEmpty
orEmpty,
defaultValue
);
};
}

public stop = () => {
this.dependencyStore.observer.stopObserving();
Expand Down
Loading