Jhipster翻译配置:未找到翻译时显示原始消息的设置方法
Absolutely! You can totally configure JHipster to show the original message instead of that annoying translation-not-found[xxx] placeholder when a translation key is missing. Let's cover both the frontend (Angular with ngx-translate) and backend (Spring Boot) since JHipster relies on both for localization:
Frontend (Angular + ngx-translate)
JHipster uses ngx-translate under the hood for frontend localization. To make it fall back to the original key when no translation exists, you'll need to set up a custom missing translation handler. Here's the straightforward way to do it:
Tweak your translation module configuration (usually in
app.module.tsor your core module):
Create a custom handler that returns the original key directly, then hook it into theTranslateModulesetup.import { TranslateModule, TranslateLoader, MissingTranslationHandler } from '@ngx-translate/core'; import { TranslateHttpLoader } from '@ngx-translate/http-loader'; import { HttpClient } from '@angular/common/http'; // Custom handler to return the original key when translation is missing export class CustomMissingTranslationHandler implements MissingTranslationHandler { handle(params: any) { return params.key; } } // Standard HTTP loader factory for translation files export function HttpLoaderFactory(http: HttpClient) { return new TranslateHttpLoader(http, './i18n/', '.json'); } @NgModule({ imports: [ TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: HttpLoaderFactory, deps: [HttpClient] }, // Attach our custom handler here missingTranslationHandler: { provide: MissingTranslationHandler, useClass: CustomMissingTranslationHandler } }) ] }) export class AppModule {}Test it out: If you use a key like
'User not found'that doesn't exist in your translation.jsonfiles, it will now display the key text instead of the default placeholder.
Backend (Spring Boot)
For backend messages (like those returned by REST APIs), JHipster uses Spring's MessageSource. To make it fall back to the original key when no translation is found, adjust the MessageSource bean configuration:
Locate and update the
MessageSourcebean (typically inWebConfigurer.javaor a dedicatedI18nConfig.javaclass):
Set thesetUseCodeAsDefaultMessageproperty totrueto enable this behavior.@Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasenames("classpath:i18n/messages"); messageSource.setDefaultEncoding("UTF-8"); // This line makes the message source return the key itself when no translation exists messageSource.setUseCodeAsDefaultMessage(true); return messageSource; }How it works: When your backend calls
messageSource.getMessage("User not found", null, locale)and there's no translation for that key, it will return the string"User not found"instead of throwing an exception or returning a generic placeholder.
Quick Notes
- Be mindful of using this in development: While great for production to avoid confusing users, the default
translation-not-foundplaceholder helps catch typos or missing translations during development. You might want to toggle this behavior based on your environment. - For newer JHipster versions using Angular standalone components, the configuration will use
provideTranslateModuleinstead ofTranslateModule.forRoot, but the core idea of theMissingTranslationHandlerremains identical.
内容的提问来源于stack exchange,提问作者VendettaV

