You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

  1. Tweak your translation module configuration (usually in app.module.ts or your core module):
    Create a custom handler that returns the original key directly, then hook it into the TranslateModule setup.

    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 {}
    
  2. Test it out: If you use a key like 'User not found' that doesn't exist in your translation .json files, 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:

  1. Locate and update the MessageSource bean (typically in WebConfigurer.java or a dedicated I18nConfig.java class):
    Set the setUseCodeAsDefaultMessage property to true to 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;
    }
    
  2. 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-found placeholder 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 provideTranslateModule instead of TranslateModule.forRoot, but the core idea of the MissingTranslationHandler remains identical.

内容的提问来源于stack exchange,提问作者VendettaV

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:53:17