Browse Source

fix: i18n translation

- rename directories
pull/1330/head
tetuaoro 2 years ago
parent
commit
a111b1b631
  1. 24
      src/localeDetector.ts
  2. 6
      src/nuxt.config.ts
  3. 36
      src/repositories/database.ts
  4. 0
      src/repositories/system/interface.ts
  5. 0
      src/repositories/system/model.ts
  6. 0
      src/repositories/types.ts
  7. 0
      src/repositories/user/interface.ts
  8. 0
      src/repositories/user/model.ts
  9. 6
      src/server/api/account/new.post.ts
  10. 2
      src/server/utils/Database.ts
  11. 11
      src/services/database/inmemory.ts

24
src/localeDetector.ts

@ -0,0 +1,24 @@
// https://i18n.nuxtjs.org/docs/guide/server-side-translations
// Detect based on query, cookie, header
export default defineI18nLocaleDetector((event, config) => {
// try to get locale from query
const query = tryQueryLocale(event, { lang: '' }); // disable locale default value with `lang` option
if (query) {
return query.toString();
}
// try to get locale from cookie
const cookie = tryCookieLocale(event, { lang: '', name: 'i18n_locale' }); // disable locale default value with `lang` option
if (cookie) {
return cookie.toString();
}
// try to get locale from header (`accept-header`)
const header = tryHeaderLocale(event, { lang: '' }); // disable locale default value with `lang` option
if (header) {
return header.toString();
}
// If the locale cannot be resolved up to this point, it is resolved with the value `defaultLocale` of the locale config passed to the function
return config.defaultLocale;
});

6
src/nuxt.config.ts

@ -17,4 +17,10 @@ export default defineNuxtConfig({
classSuffix: '', classSuffix: '',
cookieName: 'theme', cookieName: 'theme',
}, },
i18n: {
// https://i18n.nuxtjs.org/docs/guide/server-side-translations
experimental: {
localeDetector: './localeDetector.ts',
},
},
}); });

36
src/ports/database.ts → src/repositories/database.ts

@ -8,7 +8,7 @@ import type { System } from './system/model';
* Abstract class for database operations. * Abstract class for database operations.
* Provides methods to connect, disconnect, and interact with system and user data. * Provides methods to connect, disconnect, and interact with system and user data.
* *
* *Note : Throw with `DatabaseError` to ensure API handling errors.* * **Note :** Always throw `DatabaseError` to ensure proper API error handling.
* *
*/ */
export default abstract class DatabaseProvider export default abstract class DatabaseProvider
@ -37,15 +37,43 @@ export default abstract class DatabaseProvider
abstract deleteUser(id: ID): Promise<void>; abstract deleteUser(id: ID): Promise<void>;
} }
/**
* Represents a specialized error class for database-related operations.
* This class is designed to work with internationalization (i18n) by using message keys.
* The actual error messages are expected to be retrieved using these keys from an i18n system.
*
* ### Usage:
* When throwing a `DatabaseError`, you provide an i18n key as the message.
* The key will be used by the i18n system to retrieve the corresponding localized error message.
*
* Example:
* ```typescript
* throw new DatabaseError(DatabaseError.ERROR_PASSWORD_REQ);
* ...
* if (error instanceof DatabaseError) {
* const t = await useTranslation(event);
* throw createError({
* statusCode: 400,
* statusMessage: t(error.message),
* message: error.message,
* });
* } else {
* throw createError('Something happened !');
* }
* ```
*
* ### Constructor:
* - `constructor(message: string)`: Creates a new `DatabaseError` with the provided i18n message key.
*
* @extends {Error}
*/
export class DatabaseError extends Error { export class DatabaseError extends Error {
static readonly ERROR_PASSWORD_REQ = 'errorPasswordReq'; static readonly ERROR_PASSWORD_REQ = 'errorPasswordReq';
static readonly ERROR_USER_EXIST = 'errorUserExist'; static readonly ERROR_USER_EXIST = 'errorUserExist';
static readonly ERROR_DATABASE_CONNECTION = 'errorDatabaseConn'; static readonly ERROR_DATABASE_CONNECTION = 'errorDatabaseConn';
static readonly ERROR_USERNAME_REQ = 'errorUsernameReq'; static readonly ERROR_USERNAME_REQ = 'errorUsernameReq';
constructor(messageKey: string) { constructor(message: string) {
const { t } = useI18n();
const message = t(messageKey);
super(message); super(message);
this.name = 'DatabaseError'; this.name = 'DatabaseError';
} }

0
src/ports/system/interface.ts → src/repositories/system/interface.ts

0
src/ports/system/model.ts → src/repositories/system/model.ts

0
src/ports/types.ts → src/repositories/types.ts

0
src/ports/user/interface.ts → src/repositories/user/interface.ts

0
src/ports/user/model.ts → src/repositories/user/model.ts

6
src/server/api/account/new.post.ts

@ -1,4 +1,4 @@
import { DatabaseError } from '~/ports/database'; import { DatabaseError } from '~/repositories/database';
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
setHeader(event, 'Content-Type', 'application/json'); setHeader(event, 'Content-Type', 'application/json');
@ -11,9 +11,11 @@ export default defineEventHandler(async (event) => {
return { success: true }; return { success: true };
} catch (error) { } catch (error) {
if (error instanceof DatabaseError) { if (error instanceof DatabaseError) {
const t = await useTranslation(event);
throw createError({ throw createError({
statusCode: 400, statusCode: 400,
statusMessage: error.message, statusMessage: t(error.message),
message: error.message,
}); });
} else { } else {
throw createError('Something happened !'); throw createError('Something happened !');

2
src/server/utils/Database.ts

@ -4,7 +4,7 @@
* *
*/ */
import InMemory from '~/adapters/database/inmemory'; import InMemory from '~/services/database/inmemory';
const provider = new InMemory(); const provider = new InMemory();

11
src/adapters/database/inmemory.ts → src/services/database/inmemory.ts

@ -1,13 +1,13 @@
import debug from 'debug'; import debug from 'debug';
import packageJson from '@/package.json'; import packageJson from '@/package.json';
import DatabaseProvider, { DatabaseError } from '~/ports/database'; import DatabaseProvider, { DatabaseError } from '~/repositories/database';
import { ChartType, Lang } from '~/ports/types'; import { ChartType, Lang } from '~/repositories/types';
import type { SessionConfig } from 'h3'; import type { SessionConfig } from 'h3';
import type { ID } from '~/ports/types'; import type { ID } from '~/repositories/types';
import type { System } from '~/ports/system/model'; import type { System } from '~/repositories/system/model';
import type { User } from '~/ports/user/model'; import type { User } from '~/repositories/user/model';
import { hashPassword, isPasswordStrong } from '~/server/utils/password'; import { hashPassword, isPasswordStrong } from '~/server/utils/password';
const DEBUG = debug('InMemoryDB'); const DEBUG = debug('InMemoryDB');
@ -97,6 +97,7 @@ export default class InMemory extends DatabaseProvider {
async newUserWithPassword(username: string, password: string) { async newUserWithPassword(username: string, password: string) {
DEBUG('New User'); DEBUG('New User');
if (username.length < 8) { if (username.length < 8) {
throw new DatabaseError(DatabaseError.ERROR_USERNAME_REQ); throw new DatabaseError(DatabaseError.ERROR_USERNAME_REQ);
} }
Loading…
Cancel
Save