The integration of ngx-translate with an HTTP interceptor is currently experiencing difficulties

Using ngxtranslate for application translation has been seamless so far. However, I am facing an issue with the HttpInterceptor. This interceptor attempts to retrieve data from local storage at the start and displays a dialog prompting you to either load the last changes or dismiss them.
The problem lies in the fact that the text within this dialog is not being translated, unlike elsewhere in the application where translation works perfectly fine.
HTTPInterceptor file

import { TranslateService } from '@ngx-translate/core';

        @Injectable()
        export class CacheInterceptor implements HttpInterceptor {
        
          constructor(private cacheService: HttpCacheService, private dialog: MatDialog, private translate: TranslateService) {}
        
          intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
            return from(this.handle(req, next));
          }
         .......
            const cachedResponse: HttpResponse<any> = 
             this.cacheService.get(reqId);
             if (cachedResponse) {
              console.log('cache hit');

              const dialogResult = await this.openDialog();
              if (dialogResult) {
              console.log('Loading item from cache');
              // return modifyied response
            .........
               async openDialog() {
                  const dialogData = new ConfirmationDialogModel(
                  await this.translate.instant('some.text'),
                  await this.translate.instant('some.anotherText')
               );
             ....

The output only shows some.text and some.anotherText. How can I resolve this issue and make it work properly?

Answer №1

It appears that you are attempting to use the this.translate.instant function, which returns a string and not a promise, in conjunction with await. This is not the correct usage. You should either utilize the instant function without await, or consider using the

this.translate.get('xyz').toPromise()
function instead. The latter option returns an observable, which can then be converted to a promise for await functionality.

Answer №2

Have you given it a shot?

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
openPopup()
return from(this.executeRequest(req, next));
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for creating an HTML page with the dimensions of an A4 paper sheet?

My goal is to display the HTML page in a browser while containing the content within the dimensions of an A4 size page. Additionally, when printed, the HTML page should be formatted to fit onto A4-sized paper pages. <div classNa ...

Is it possible to transfer files using Angular 2?

Currently, I am utilizing Angular2 and TypeScript to transmit a file in conjunction with JSON Data to a designated server. HTML Code <input type="file" class="form-control" name="avatar" id="uploadyour" name="uploadyour" #uploadyour="ngModel" [(ngMode ...

Using jQuery to pass dynamic values to a plugin function

I'm currently utilizing the JSONTable plugin and attempting to dynamically pass values to the 'head' and 'json' parameters by extracting them from an array object. For instance, I aim to load a new json file, convert it to a JavaSc ...

Obtain the selected type from a tuple after filtering

I have a tuple with multiple objects stored in it. const repos = [ { name: 'react', type: 'JS' }, { name: 'angular', type: 'TS' }, ] as const const RepoTypes = typeof repos const jsRepoTypes = FilterRepos<&a ...

The data retrieved from the $.ajax() request in Vue.js is not properly syncing with the table

After setting up an $.ajax() function and ensuring the data binding is correctly configured, I expected the data to append to a table on page load without any issues. However, the data is not appearing as expected. Is there something that I might be overlo ...

Creating an Interactive Menu System with Nested Options in Angular 2

I have a JSON structure that I need to transform into a menu with sub-menus based on the value of the sub-menu_location field. Here's an example: { "data": [ { "menu_name": "Primary Operations", "enabled": true, "sub-menu_lo ...

What is the best way to include 'SCSS' in the 'rollup' project that I have developed?

I am embarking on the creation of a React UI library and have chosen 'rollup' as my build tool. To enhance the project, I plan to incorporate TypeScript and utilize SCSS for styling. How can I implement SCSS within this setup? You can find the s ...

Error Message in Terminal When Launching React Application Using Webpack

I am encountering several errors in the node terminal while attempting to launch my react-app [at-loader] ./node_modules/@types/webpack/index.d.ts:23:16 TS2665: The module name in augmentation is invalid. Module 'webpack/lib/dependencies/HarmonyE ...

AngularJS $scope variable issue

After searching online, I found this code snippet that I tried to implement, but unfortunately, it's not displaying any data. Below is the HTML code: <html data-ng-app=""> <head> <title>Example 4: Using AngularJS Directives an ...

What are the steps to execute a module designed for NodeJS v6 LTS ES2015 in Meteor 1.4.x?

While I understand that Meteor includes NodeJS as a dependency, I'm facing an issue with a module written in ES6 that has a default argument value set within one of the Class methods. The problem arises when using Meteor v1.4.3.2: (STDERR) packages/m ...

Issues with compiling Vuetify SASS in Nuxt for incorrect output

I am trying to integrate Vuetify with Nuxt using a Plugin but I am encountering the following issue: https://i.sstatic.net/KZbCN.png Error Message: ERROR in ./node_modules/vuetify/src/components/VProgressLinear/VProgressLinear.sass ...

Guide on retrieving data from an axios promise in JavaScript

I am struggling to manage the output of multiple lists retrieved through an axios API call made in JavaScript. I want to know how to effectively log the results and save them for future use, particularly for creating a data visualization. Here is my curre ...

Determine if a radio button is selected using Jquery

I am currently working on a script that should uncheck a radio button if it is checked, and vice versa. However, I'm facing an issue where the script always registers the radio button as checked even when it's not. Below is the code snippet in q ...

Steps to fix the issue of 'Property 'foo' lacks an initializer and is not definitely assigned in the constructor' while utilizing the @Input decorator

What is the proper way to initialize a property with an @Input decorator without compromising strict typing? The code snippet below is triggering a warning: @Input() bar: FormGroup = new FormGroup(); ...

Filtering the JSON data shown according to an array in Angular 7

After receiving JSON data from the backend using a service, I am displaying it through a loop in main.html. There is an array that contains the values of a column being displayed. For example, if the array looks like this: colors=[red,blue], then only reco ...

Correctly executed $.Ajax and $.Post requests consistently yield errors when sent from C#

I'm struggling to create a cross-domain web API method in C# that will return valid jsonp to Javascript. Despite returning valid JSON data, I keep encountering failure messages when trying to debug with F12 dev tools or Firebug. Here is my current co ...

Using TypeScript to incorporate JS web assembly into your project

I have been attempting to incorporate wasm-clingo into my TypeScript React project. I tried creating my own .d.ts file for the project: // wasm-clingo.d.ts declare module 'wasm-clingo' { export const Module: any; } and importing it like this: ...

Convert JSON response date format to a format specified by the user

The following code snippet is currently returning the dates for $("#dob") and $("#anniversery") as 2014-04-01T00:00:00 This is my current code: <script> $(function() { function log(message) { $("<div>").text(message).p ...

Developing an Angular project may result in an overflow of memory

After attempting to construct an Angular project on a Linux server with the -aot flag, I encountered an error. Despite upgrading my instance to a higher CPU core, increasing RAM, and enabling a SWAP partition, the error persisted and even occurred more rap ...

Which codec strings are considered valid for the web codecs API?

I am searching for a definitive list of valid strings that can be passed as the `codec` parameter in `VideoEncoder.isConfigSupported({ codec, width, height })`, but I have been unable to find a clear answer so far: The TS declaration file states that it m ...