Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: fix language setting on the public interface #634

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions projects/public-holdings-items/src/app/app-initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
*/

import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { TranslateService } from '@rero/ng-core';
import { AppSettingsService, UserService } from '@rero/shared';
import { AppConfigService } from './app-config-service.service';

@Injectable({
providedIn: 'root'
Expand All @@ -29,27 +30,37 @@ export class AppInitializerService {
* @param _translateService - TranslateService
* @param _userService - UserService
* @param _appSettingsService - AppSettingsService
* @param _appConfigService - AppConfigService
*/
constructor(
private _translateService: TranslateService,
private _userService: UserService,
private _appSettingsService: AppSettingsService
private _appSettingsService: AppSettingsService,
private _appConfigService: AppConfigService
) { }

/** Load */
load(): Promise<boolean> {
return new Promise((resolve) => {
this._initiliazeObservable();
this._userService.loaded$.subscribe(() => {
this.initTranslateService();
});
this._userService.load();
resolve(true);
});
}

/** initialize observable */
private _initiliazeObservable(): void {
// Set current language interface
this._userService.loaded$.subscribe(() => {
this._translateService.use(this._appSettingsService.currentLanguage);
});
/** Initialize Translate Service */
private initTranslateService(): void {
const language = this._appSettingsService.settings.language;
if (language) {
this._translateService.setLanguage(language);
} else {
const browserLang = this._translateService.getBrowserLang();
this._translateService.setLanguage(
browserLang.match(this._appConfigService.languages.join('|')) ?
browserLang : this._appConfigService.defaultLanguage
);
}
}
}
10 changes: 8 additions & 2 deletions projects/public-search/src/app/app-initializer.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@

import { HttpClientTestingModule } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';

import { TranslateModule } from '@ngx-translate/core';
import { BsLocaleService } from 'ngx-bootstrap/datepicker';
import { AppInitializerService } from './app-initializer.service';


describe('AppInitializerService', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
HttpClientTestingModule,
TranslateModule.forRoot()
],
providers: [
BsLocaleService
]
}));

Expand Down
29 changes: 27 additions & 2 deletions projects/public-search/src/app/app-initializer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/

import { Injectable } from '@angular/core';
import { UserService } from '@rero/shared';
import { TranslateService } from '@rero/ng-core';
import { AppSettingsService, UserService } from '@rero/shared';
import { AppConfigService } from './app-config.service';
import { RouteCollectionService } from './routes/route-collection.service';

@Injectable({
Expand All @@ -28,19 +30,42 @@ export class AppInitializerService {
* Constructor
* @param _routeCollectionService - RouteCollectionService
* @param _userService - UserService
* @param _appSettingsService - AppSettingsService
* @param _translateService - TranslateService
* @param _appConfigService - AppConfigService
*/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc for params is missing

constructor(
private _routeCollectionService: RouteCollectionService,
private _userService: UserService
private _userService: UserService,
private _appSettingsService: AppSettingsService,
private _translateService: TranslateService,
private _appConfigService: AppConfigService

) { }

/** load */
load(): Promise<boolean> {
return new Promise((resolve) => {
this._userService.loaded$.subscribe(() => {
this.initTranslateService();
});
this._userService.load();
this._routeCollectionService.load();
resolve(true);
});
}

/** Initialize Translate Service */
private initTranslateService(): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be one day in the shared or ng-core

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the next refactoring ;-)

const language = this._appSettingsService.settings.language;
if (language) {
this._translateService.setLanguage(language);
} else {
const browserLang = this._translateService.getBrowserLang();
this._translateService.setLanguage(
browserLang.match(this._appConfigService.languages.join('|')) ?
browserLang : this._appConfigService.defaultLanguage
);
}
}
}
11 changes: 2 additions & 9 deletions projects/public-search/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,12 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Component, OnInit, Injector } from '@angular/core';
import { TranslateService } from '@rero/ng-core';
import { Component } from '@angular/core';

@Component({
selector: 'public-search-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
lang: string = document.documentElement.lang;
export class AppComponent {

constructor(private injector: Injector) { }

ngOnInit(): void {
this.injector.get<TranslateService>(TranslateService).setLanguage(this.lang);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ <h4 class="card-title mb-1">
<li *ngFor="let publication of provisionActivityPublications">{{ publication.value }}</li>
</ul>

<div *ngIf="record.metadata.type !== 'ebook'">
<i class="fa fa-circle text-{{ record.metadata.available ? 'success' : 'danger' }}" aria-hidden="true"></i>&nbsp;
<span translate *ngIf="record.metadata.available">available</span>
<span translate *ngIf="!record.metadata.available">not available</span>
</div>
<!-- explanation -->
<div *ngIf="record.explanation">
<a class="badge badge-info collapsed"
data-toggle="collapse" href="#{{'score'+record.metadata.pid }}"
Expand All @@ -66,7 +62,16 @@ <h4 class="card-title mb-1">
</a>
<pre class="collapse border border-secondary mt-1" id="{{'score'+record.metadata.pid }}">{{record.explanation|json}}</pre>
</div>

<!-- part of -->
<shared-part-of [record]="record" [viewcode]="viewcode" [isPublicView]="true"></shared-part-of>

<!-- availability -->
<div *ngIf="record.metadata.type !== 'ebook'">
<i class="fa fa-circle text-{{ record.metadata.available ? 'success' : 'danger' }}" aria-hidden="true"></i>&nbsp;
<span translate *ngIf="record.metadata.available">available</span>
<span translate *ngIf="!record.metadata.available">not available</span>
</div>
</article>
</div>
</article>
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
</ng-template>
<ng-container *ngFor="let type of record.metadata.type">
<ng-container *ngIf="type.subtype; else mainTypeBlock">
<figcaption class="figure-caption text-left" translate>{{ type.subtype }}</figcaption>
<figcaption class="figure-caption text-center" translate>{{ type.subtype }}</figcaption>
</ng-container>
<ng-template #mainTypeBlock>
<figcaption class="figure-caption text-left" translate>{{ type.main_type }}</figcaption>
<figcaption class="figure-caption text-center" translate>{{ type.main_type }}</figcaption>
</ng-template>
</ng-container>
</figure>
Expand Down