Skip to content

Commit

Permalink
Rename languages key and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bameyrick committed Jul 20, 2023
1 parent 02329fb commit 3a7d17d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ yarn add @qntm-code/nest-translate

## Usage

Import the `TranslateModule` into your root module. The `TranslateModule` requires a configuration object with the default language and a list of languages with their translations.
Import the `TranslateModule` into your root module. The `TranslateModule` requires a configuration object with the default language and a list of namespaces with the translations for each language.

```typescript
import { Module } from '@nestjs/common';
Expand All @@ -36,7 +36,7 @@ import { TranslateModule } from '@qntm-code/nest-translate';
imports: [
TranslateModule.forRoot({
defaultLanguage: 'en',
languages: [
translations: [
{
language: 'en',
namespace: 'greetings',
Expand Down Expand Up @@ -80,7 +80,7 @@ import deGreetings from './greetings.de.json';
imports: [
TranslateModule.forRoot({
defaultLanguage: 'en',
languages: [
translations: [
{
language: 'en',
namespace: 'greetings',
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './language-config.model';
export * from './tokens';
export * from './translate-module-config.model';
export * from './translate.module';
export * from './translate.service';
export * from './translation-config.model';
4 changes: 2 additions & 2 deletions src/translate-module-config.model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LanguageConfig } from './language-config.model';
import { TranslationConfig } from './translation-config.model';

export interface TranslateModuleConfig {
defaultLanguage: string;
languages: LanguageConfig[];
translations: TranslationConfig[];
enableMissingTranslationLogging?: boolean;
missingTranslationHandler?: (language: string, key: string) => void;
}
51 changes: 26 additions & 25 deletions src/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ import { TranslateService } from './translate.service';

describe(`TranslateService`, () => {
it(`Should return a translated string`, () => {
const translateService = new TranslateService('en', [
{
language: 'en',
namespace: 'test',
values: {
hello: 'Hello',
const translateService = new TranslateService({
defaultLanguage: 'en',
translations: [
{
language: 'en',
namespace: 'test',
values: {
hello: 'Hello',
},
},
},
]);
],
});

expect(translateService.translate('en', 'test.hello')).toBe('Hello');
});

it(`Should log an error if a translated string cannot be found`, () => {
const translateService = new TranslateService(
'en',
[
const translateService = new TranslateService({
defaultLanguage: 'en',
translations: [
{
language: 'en',
namespace: 'test',
Expand All @@ -27,8 +30,8 @@ describe(`TranslateService`, () => {
},
},
],
true
);
enableMissingTranslationLogging: true,
});

const consoleError = console.error;

Expand All @@ -44,9 +47,9 @@ describe(`TranslateService`, () => {
it(`Should utilise a custom missing translation handler`, () => {
let handlerCalled = false;

const translateService = new TranslateService(
'en',
[
const translateService = new TranslateService({
defaultLanguage: 'en',
translations: [
{
language: 'en',
namespace: 'test',
Expand All @@ -55,11 +58,10 @@ describe(`TranslateService`, () => {
},
},
],
false,
() => {
missingTranslationHandler: () => {
handlerCalled = true;
}
);
},
});

expect(handlerCalled).toBe(false);

Expand All @@ -69,9 +71,9 @@ describe(`TranslateService`, () => {
});

it(`Should attempt to find a translation in the default language if the translation is not found in the requested language`, () => {
const translateService = new TranslateService(
'en',
[
const translateService = new TranslateService({
defaultLanguage: 'en',
translations: [
{
language: 'en',
namespace: 'test',
Expand All @@ -85,8 +87,7 @@ describe(`TranslateService`, () => {
values: {},
},
],
false
);
});

expect(translateService.translate('fr', 'test.hello')).toBe('Hello');
});
Expand Down
2 changes: 1 addition & 1 deletion src/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class TranslateService {
missingTranslationHandler: translateModuleConfig.missingTranslationHandler,
});

translateModuleConfig.languages.forEach(({ language, namespace, values }) =>
translateModuleConfig.translations.forEach(({ language, namespace, values: values }) =>
this.store.addLanguageNamespace(language, namespace, values)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TranslationLanguage } from '@qntm-code/translation-key-store';

export interface LanguageConfig {
export interface TranslationConfig {
language: string;
namespace: string;
values: TranslationLanguage;
Expand Down

0 comments on commit 3a7d17d

Please sign in to comment.