Skip to content

Commit

Permalink
Allow retrieval from languageHashes using environment (DSpace#2702)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Mahnke committed Dec 7, 2023
1 parent 8ba14aa commit 2e76ba3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/config/app-config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { HomeConfig } from './homepage-config.interface';
import { MarkdownConfig } from './markdown-config.interface';
import { FilterVocabularyConfig } from './filter-vocabulary-config';
import { DiscoverySortConfig } from './discovery-sort.config';
import { LanguageHashesConfig } from './languageHashes-config.interface';

interface AppConfig extends Config {
ui: UIServerConfig;
Expand All @@ -35,6 +36,7 @@ interface AppConfig extends Config {
debug: boolean;
defaultLanguage: string;
languages: LangConfig[];
languageHashes: LanguageHashesConfig[];
browseBy: BrowseByConfig;
communityList: CommunityListConfig;
homePage: HomeConfig;
Expand Down
3 changes: 3 additions & 0 deletions src/config/default-app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { HomeConfig } from './homepage-config.interface';
import { MarkdownConfig } from './markdown-config.interface';
import { FilterVocabularyConfig } from './filter-vocabulary-config';
import { DiscoverySortConfig } from './discovery-sort.config';
import { LanguageHashesConfig } from './languageHashes-config.interface';

export class DefaultAppConfig implements AppConfig {
production = false;
Expand Down Expand Up @@ -242,6 +243,8 @@ export class DefaultAppConfig implements AppConfig {
{ code: 'uk', label: 'Yкраї́нська', active: true}
];

languageHashes: LanguageHashesConfig[] = [];

// Browse-By Pages
browseBy: BrowseByConfig = {
// Amount of years to display using jumps of one year (current year - oneYearLimit)
Expand Down
11 changes: 11 additions & 0 deletions src/config/languageHashes-config.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Config } from './config.interface';

/**
* An interface to represent a language in the configuration. A LangConfig has a code which should be the official
* language code for the language (e.g. ‘fr’), a label which should be the name of the language in that language
* (e.g. ‘Français’), and a boolean to determine whether or not it should be listed in the language select.
*/
export interface LanguageHashesConfig extends Config {
lang: string;
md5: string;
}
6 changes: 5 additions & 1 deletion src/ngx-translate-loaders/translate-browser.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export class TranslateBrowserLoader implements TranslateLoader {
if (hasValue(messages)) {
return observableOf(messages);
} else {
const translationHash: string = environment.production ? `.${(process.env.languageHashes as any)[lang + '.json5']}` : '';
let translationHash = '';
if (environment.production) {
const languageHashesConfigFromEnvironment = environment.languageHashes.filter((languageHashConfig) => languageHashConfig.lang === lang);
translationHash = `.${languageHashesConfigFromEnvironment.length > 0 ? languageHashesConfigFromEnvironment[0].md5 : (process.env.languageHashes as any)[lang + '.json5']}`;
}
// If they're not available on the transfer state (e.g. when running in dev mode), retrieve
// them using HttpClient
return this.http.get(`${this.prefix}${lang}${translationHash}${this.suffix}`, { responseType: 'text' }).pipe(
Expand Down
5 changes: 4 additions & 1 deletion src/ngx-translate-loaders/translate-server.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Observable, of as observableOf } from 'rxjs';
import { readFileSync } from 'fs';
import { TransferState } from '@angular/platform-browser';
import { NGX_TRANSLATE_STATE, NgxTranslateState } from './ngx-translate-state';
import { environment } from '../environments/environment';

/**
* A TranslateLoader for ngx-translate to parse json5 files server-side, and store them in the
Expand All @@ -23,7 +24,9 @@ export class TranslateServerLoader implements TranslateLoader {
* @param lang the language code
*/
public getTranslation(lang: string): Observable<any> {
const translationHash: string = (process.env.languageHashes as any)[lang + '.json5'];
const languageHashesConfigFromEnvironment = environment.languageHashes.filter((languageHashConfig) => languageHashConfig.lang === lang);
const translationHash = `.${languageHashesConfigFromEnvironment.length > 0 ? languageHashesConfigFromEnvironment[0].md5 : (process.env.languageHashes as any)[lang + '.json5']}`;

// Retrieve the file for the given language, and parse it
const messages = JSON.parse(readFileSync(`${this.prefix}${lang}.${translationHash}${this.suffix}`, 'utf8'));
// Store the parsed messages in the transfer state so they'll be available immediately when the
Expand Down

0 comments on commit 2e76ba3

Please sign in to comment.