Can I change the concatenation character of the String-Array regarding JHipsters 'jhiTranslate'?

我怕爱的太早我们不能终老 提交于 2021-02-11 07:10:26

问题


If I use a string array for translation in e.g. i18n/en/home.json like

     "primaryIntroduction": {
         "p1": [
             "Then Jenkins triggers a task to connect to the docker-server",
             " and calls some docker- and docker-compose commands to pull the new image",
             " and rebuilds and restarts the docker-container."
         ]
    }

jhipster concatenates the <p> on the english page like:

Then Jenkins triggers a task to connect to the docker-server, and calls some docker- and docker-compose commands to pull the new image, and rebuilds and restarts the docker-container.

(mind the commata)

Entering a new line without ending the line with a quotation mark, the json-file will brake. Without newlines, bigger paragraphs are hard to read and to translate in my opinion.

Can I change the way the sentences are concatenated on myself? For example take a whitespace instead of a comma when putting the strings of the array together? Would it be useful to introduce a control-variable for that concatenation character in the Angular system?


回答1:


This is normal, the value for your key p1 is an array and the string representation of an array is a comma separated list. See https://github.com/ngx-translate/core/issues/339 for details.

This issue was solved in ngx-translate by supporting JSON5 format but unfortunately it has not been released yet and it seems the project is no longer actively maintained as last release is from november 2018.

So you could try the pipe suggestion to support arrays, see https://github.com/ngx-translate/core/issues/339#issuecomment-360450296




回答2:


To change jhipster's Angular Translation Concatenation (Version 6.10.5, see https://stackoverflow.com/a/34272375/7773582), I added

import { TranslateDefaultParser } from '@ngx-translate/core';
import { Injectable } from '@angular/core';

@Injectable()
export class TranslateAppParser extends TranslateDefaultParser {
  getValue(target: any, key: string): any {
    target = super.getValue(target, key);
    if (target instanceof Array) {
      target = target.join(' ');
    }
    return target;
  }
}

in src/main/webapp/app/shared/translateappparser.module.ts

and added

import {MissingTranslationHandler, TranslateLoader, TranslateModule, TranslateParser} from '@ngx-translate/core';
import { TranslateAppParser } from 'app/shared/translateappparser.module';

export function createTranslateParser():any {
  return new TranslateAppParser();
}

as well as parser: { provide: TranslateParser, useFactory: createTranslateParser },

in

TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: translatePartialLoader,
        deps: [HttpClient],
      },
      parser: { provide: TranslateParser, useFactory: createTranslateParser },
      missingTranslationHandler: {
        provide: MissingTranslationHandler,
        useFactory: missingTranslationHandler,
        deps: [JhiConfigService],
      },

in file src/main/webapp/app/core/core.module.ts

That's exactly what Gaël Marziou's answer was pointing me to - today I just had to find the code snippets again while doing an jhipster-Upgrade with jhipster --with-entities



来源:https://stackoverflow.com/questions/59214773/can-i-change-the-concatenation-character-of-the-string-array-regarding-jhipsters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!