Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Code Reviews

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Code Reviews Implement translatable UI in a Single Page Application when working with ngRx

I am currently working on an Angular SPA that supports multiple languages and relies on ngRx for state management. Although the application state is handled by ngRx, the current language is stored...

0 answers  ·  posted 2y ago by Alexei‭

#1: Initial revision by user avatar Alexei‭ · 2022-11-16T08:13:37Z (about 2 years ago)
Implement translatable UI in a Single Page Application when working with ngRx
I am currently working on an Angular SPA that supports multiple languages and relies on ngRx for state management. 

Although the application state is handled by ngRx, the current language is stored in the database. The translated resources come from outside the store (resource files read using the @ngx-translate library).

Relevant code parts are shown below.

### Language change

```ts
public languageChange(value: TranslationLanguage): void {
	this.store.dispatch(UpdatePreferredCultureRequestedAction({ value: value.cultureName }));
	this.translate.use(value.cultureName);
}
```

### Culture change handling

Current user culture is changed in a pessimistic way (server side must end successfully) and then all UI related components should refresh their content based on the new language:

```ts
updatePreferredCulture$ = createEffect(() => this.actions$.pipe(
	ofType(UpdatePreferredCultureRequestedAction),
	concatMap(action => {
		return this.layoutInfoService.updatePreferredCulture(action.value).pipe(
			catchError(err => {
				this.validation.handleAndDisplayError(err);
				return of(Constants.defaultCultureName);
			})
		);
	}),
	map((culture: string) =>  UpdatePreferredCultureLoadedAction({ value: culture }))
));

updatePreferredCulture(preferredCulture: string): Observable<string> {
	const http$ = this.http
		.put("/api/preferred-culture", { value: preferredCulture })
		.pipe(
			catchError(err => this.validationService.handleAndDisplayError(err)),
			map(_ => preferredCulture)
		);
	return http$;		
}
```

The application state is also altered to know about the new culture:

```ts
on(UpdatePreferredCultureLoadedAction, (state: LayoutInfoState, payload: { value: string }) => {
	return { ...state, info: {  ...state.info, preferredCulture: payload.value } };
})
```

However, each part of the application that relies on translation must be explicitly notified (by dispatching an explicit "reload" action):

```ts
questionnaireListRequested$ = createEffect(() => this.actions$.pipe(
	ofType(UpdatePreferredCultureLoadedAction),
	map(_ => QuestionnaireListRequestedAction())
));

homeHistoryRequested$ = createEffect(() => this.actions$.pipe(
	ofType(UpdatePreferredCultureLoadedAction),
	map(_ => HistoryQuestionnaireShortListRequestedAction())
));

historyRequested$ = createEffect(() => this.actions$.pipe(
	ofType(UpdatePreferredCultureLoadedAction),
	map(_ => HistoryQuestionnaireListRequestedAction())
));

// other effects are expected to be defined here as the application grows
```

This last part concerns me a little bit as it creates some coupling between translation-related effects and everything else.

Any thoughts about improving the design?