|
|
@@ -4,8 +4,10 @@ import { GraphQLError } from 'graphql-request/dist/src/types';
|
|
|
import * as i18next from 'i18next';
|
|
|
import { TranslationFunction } from 'i18next';
|
|
|
import * as i18nextMiddleware from 'i18next-express-middleware';
|
|
|
+import * as ICU from 'i18next-icu';
|
|
|
import * as Backend from 'i18next-node-fs-backend';
|
|
|
import * as path from 'path';
|
|
|
+import { ConfigService } from '../service/config.service';
|
|
|
import { I18nError } from './i18n-error';
|
|
|
|
|
|
export interface I18nRequest extends Request {
|
|
|
@@ -24,17 +26,19 @@ export interface WrappedGraphQLError extends GraphQLError {
|
|
|
*/
|
|
|
@Injectable()
|
|
|
export class I18nService {
|
|
|
- constructor() {
|
|
|
+ constructor(private configService: ConfigService) {
|
|
|
i18next
|
|
|
.use(i18nextMiddleware.LanguageDetector)
|
|
|
.use(Backend)
|
|
|
+ .use(ICU)
|
|
|
.init({
|
|
|
preload: ['en', 'de'],
|
|
|
+ fallbackLng: 'en',
|
|
|
detection: {
|
|
|
lookupQuerystring: 'lang',
|
|
|
},
|
|
|
backend: {
|
|
|
- loadPath: path.join(__dirname, 'translations/{{lng}}.json'),
|
|
|
+ loadPath: path.join(__dirname, 'messages/{{lng}}.json'),
|
|
|
jsonIndent: 2,
|
|
|
},
|
|
|
});
|
|
|
@@ -49,7 +53,13 @@ export class I18nService {
|
|
|
const originalError = error.originalError;
|
|
|
if (req && req.t && originalError instanceof I18nError) {
|
|
|
const t: TranslationFunction = req.t;
|
|
|
- error.message = t(originalError.message, originalError.variables);
|
|
|
+ let translation = originalError.message;
|
|
|
+ try {
|
|
|
+ translation = t(originalError.message, originalError.variables);
|
|
|
+ } catch (e) {
|
|
|
+ translation += ` (Translation format error: ${e.message})`;
|
|
|
+ }
|
|
|
+ error.message = translation;
|
|
|
}
|
|
|
|
|
|
return error;
|