mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
Merge pull request #9 from wwelling/i18n-translate
I18n translate: this connects to #3
This commit is contained in:
@@ -53,6 +53,7 @@
|
|||||||
"js.clone": "0.0.3",
|
"js.clone": "0.0.3",
|
||||||
"methods": "~1.1.2",
|
"methods": "~1.1.2",
|
||||||
"morgan": "^1.7.0",
|
"morgan": "^1.7.0",
|
||||||
|
"ng2-translate": "~4.0.1",
|
||||||
"preboot": "~4.5.2",
|
"preboot": "~4.5.2",
|
||||||
"rxjs": "5.0.0-beta.12",
|
"rxjs": "5.0.0-beta.12",
|
||||||
"webfontloader": "^1.6.26",
|
"webfontloader": "^1.6.26",
|
||||||
@@ -77,6 +78,7 @@
|
|||||||
"awesome-typescript-loader": "^2.2.4",
|
"awesome-typescript-loader": "^2.2.4",
|
||||||
"codelyzer": "1.0.0-beta.3",
|
"codelyzer": "1.0.0-beta.3",
|
||||||
"cookie-parser": "^1.4.3",
|
"cookie-parser": "^1.4.3",
|
||||||
|
"copy-webpack-plugin": "~4.0.1",
|
||||||
"express-interceptor": "^1.2.0",
|
"express-interceptor": "^1.2.0",
|
||||||
"iltorb": "^1.0.13",
|
"iltorb": "^1.0.13",
|
||||||
"imports-loader": "^0.6.5",
|
"imports-loader": "^0.6.5",
|
||||||
|
13
resources/i18n/en.json
Normal file
13
resources/i18n/en.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"title": "DSpace Universal",
|
||||||
|
|
||||||
|
"nav": {
|
||||||
|
"home": "Home"
|
||||||
|
},
|
||||||
|
|
||||||
|
"example": {
|
||||||
|
"with": {
|
||||||
|
"data": "{{greeting}}, {{recipient}}!"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,9 +1,11 @@
|
|||||||
<h3>DSpace</h3>
|
<h3>{{ 'title' | translate }}</h3>
|
||||||
<nav>
|
<nav>
|
||||||
<a routerLinkActive="router-link-active" routerLink="home">Home</a>
|
<a routerLinkActive="router-link-active" routerLink="home">{{ 'nav.home' | translate }}</a>
|
||||||
</nav>
|
</nav>
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<main>
|
<main>
|
||||||
|
<p>{{ 'example.with.data' | translate:data }}</p>
|
||||||
|
<p>{{ example }}</p>
|
||||||
<router-outlet></router-outlet>
|
<router-outlet></router-outlet>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -1,11 +1,42 @@
|
|||||||
import { Component, ChangeDetectionStrategy, ViewEncapsulation } from '@angular/core';
|
import { Component, ChangeDetectionStrategy, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
|
||||||
|
|
||||||
|
import { TranslateService } from 'ng2-translate';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
changeDetection: ChangeDetectionStrategy.Default,
|
changeDetection: ChangeDetectionStrategy.Default,
|
||||||
encapsulation: ViewEncapsulation.Emulated,
|
encapsulation: ViewEncapsulation.Emulated,
|
||||||
selector: 'ds-app',
|
selector: 'ds-app',
|
||||||
templateUrl: './app.component.html',
|
templateUrl: './app.component.html',
|
||||||
styleUrls: [ './app.component.scss' ]
|
styleUrls: ['./app.component.scss']
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent implements OnDestroy, OnInit {
|
||||||
|
|
||||||
|
example: string;
|
||||||
|
|
||||||
|
data: any = {
|
||||||
|
greeting: 'Hello',
|
||||||
|
recipient: 'World'
|
||||||
|
}
|
||||||
|
|
||||||
|
private registerSubscription: any;
|
||||||
|
|
||||||
|
constructor(public translate: TranslateService) {
|
||||||
|
// this language will be used as a fallback when a translation isn't found in the current language
|
||||||
|
translate.setDefaultLang('en');
|
||||||
|
// the lang to use, if the lang isn't available, it will use the current loader to get them
|
||||||
|
translate.use('en');
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
this.registerSubscription = this.translate.get('example.with.data', { greeting: 'Hello', recipient: 'DSpace' }).subscribe((translation: string) => {
|
||||||
|
this.example = translation;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnDestroy() {
|
||||||
|
if (this.registerSubscription) {
|
||||||
|
this.registerSubscription.unsubscribe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,9 @@ import { NgModule, ModuleWithProviders } from '@angular/core';
|
|||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { RouterModule } from '@angular/router';
|
import { RouterModule } from '@angular/router';
|
||||||
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
|
||||||
|
|
||||||
|
import { TranslateModule } from 'ng2-translate/ng2-translate';
|
||||||
|
|
||||||
import { ApiService } from './api.service';
|
import { ApiService } from './api.service';
|
||||||
import { ModelService } from './model/model.service';
|
import { ModelService } from './model/model.service';
|
||||||
|
|
||||||
@@ -9,6 +12,7 @@ const MODULES = [
|
|||||||
// Do NOT include UniversalModule, HttpModule, or JsonpModule here
|
// Do NOT include UniversalModule, HttpModule, or JsonpModule here
|
||||||
CommonModule,
|
CommonModule,
|
||||||
RouterModule,
|
RouterModule,
|
||||||
|
TranslateModule,
|
||||||
FormsModule,
|
FormsModule,
|
||||||
ReactiveFormsModule
|
ReactiveFormsModule
|
||||||
];
|
];
|
||||||
|
@@ -1,9 +1,12 @@
|
|||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
|
import { Http } from '@angular/http';
|
||||||
import { FormsModule } from '@angular/forms';
|
import { FormsModule } from '@angular/forms';
|
||||||
import { RouterModule } from '@angular/router';
|
import { RouterModule } from '@angular/router';
|
||||||
import { UniversalModule, isBrowser, isNode, AUTO_PREBOOT } from 'angular2-universal/browser'; // for AoT we need to manually split universal packages
|
import { UniversalModule, isBrowser, isNode, AUTO_PREBOOT } from 'angular2-universal/browser'; // for AoT we need to manually split universal packages
|
||||||
import { IdlePreload, IdlePreloadModule } from '@angularclass/idle-preload';
|
import { IdlePreload, IdlePreloadModule } from '@angularclass/idle-preload';
|
||||||
|
|
||||||
|
import { TranslateLoader, TranslateModule, TranslateStaticLoader } from 'ng2-translate';
|
||||||
|
|
||||||
import { AppModule, AppComponent } from './app/app.module';
|
import { AppModule, AppComponent } from './app/app.module';
|
||||||
import { SharedModule } from './app/shared/shared.module';
|
import { SharedModule } from './app/shared/shared.module';
|
||||||
import { CacheService } from './app/shared/cache.service';
|
import { CacheService } from './app/shared/cache.service';
|
||||||
@@ -14,6 +17,10 @@ import { Meta } from './angular2-meta';
|
|||||||
|
|
||||||
// import * as LRU from 'modern-lru';
|
// import * as LRU from 'modern-lru';
|
||||||
|
|
||||||
|
export function createTranslateLoader(http: Http) {
|
||||||
|
return new TranslateStaticLoader(http, './assets/i18n', '.json');
|
||||||
|
}
|
||||||
|
|
||||||
export function getLRU(lru?: any) {
|
export function getLRU(lru?: any) {
|
||||||
// use LRU for node
|
// use LRU for node
|
||||||
// return lru || new LRU(10);
|
// return lru || new LRU(10);
|
||||||
@@ -33,9 +40,14 @@ export function getResponse() {
|
|||||||
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
|
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
bootstrap: [ AppComponent ],
|
bootstrap: [AppComponent],
|
||||||
imports: [
|
imports: [
|
||||||
// MaterialModule.forRoot() should be included first
|
TranslateModule.forRoot({
|
||||||
|
provide: TranslateLoader,
|
||||||
|
useFactory: (createTranslateLoader),
|
||||||
|
deps: [Http]
|
||||||
|
}),
|
||||||
|
|
||||||
UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
|
UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
|
||||||
|
|
||||||
FormsModule,
|
FormsModule,
|
||||||
|
@@ -1,8 +1,11 @@
|
|||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
|
import { Http } from '@angular/http';
|
||||||
import { FormsModule } from '@angular/forms';
|
import { FormsModule } from '@angular/forms';
|
||||||
import { RouterModule } from '@angular/router';
|
import { RouterModule } from '@angular/router';
|
||||||
import { UniversalModule, isBrowser, isNode } from 'angular2-universal/node'; // for AoT we need to manually split universal packages
|
import { UniversalModule, isBrowser, isNode } from 'angular2-universal/node'; // for AoT we need to manually split universal packages
|
||||||
|
|
||||||
|
import { TranslateLoader, TranslateModule, TranslateStaticLoader } from 'ng2-translate';
|
||||||
|
|
||||||
import { AppModule, AppComponent } from './app/app.module';
|
import { AppModule, AppComponent } from './app/app.module';
|
||||||
import { SharedModule } from './app/shared/shared.module';
|
import { SharedModule } from './app/shared/shared.module';
|
||||||
import { CacheService } from './app/shared/cache.service';
|
import { CacheService } from './app/shared/cache.service';
|
||||||
@@ -11,6 +14,10 @@ import { CacheService } from './app/shared/cache.service';
|
|||||||
// see https://github.com/angular/angular/pull/12322
|
// see https://github.com/angular/angular/pull/12322
|
||||||
import { Meta } from './angular2-meta';
|
import { Meta } from './angular2-meta';
|
||||||
|
|
||||||
|
export function createTranslateLoader(http: Http) {
|
||||||
|
return new TranslateStaticLoader(http, './assets/i18n', '.json');
|
||||||
|
}
|
||||||
|
|
||||||
export function getLRU() {
|
export function getLRU() {
|
||||||
return new Map();
|
return new Map();
|
||||||
}
|
}
|
||||||
@@ -25,9 +32,14 @@ export function getResponse() {
|
|||||||
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
|
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
bootstrap: [ AppComponent ],
|
bootstrap: [AppComponent],
|
||||||
imports: [
|
imports: [
|
||||||
// MaterialModule.forRoot() should be included first
|
TranslateModule.forRoot({
|
||||||
|
provide: TranslateLoader,
|
||||||
|
useFactory: (createTranslateLoader),
|
||||||
|
deps: [Http]
|
||||||
|
}),
|
||||||
|
|
||||||
UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
|
UniversalModule, // BrowserModule, HttpModule, and JsonpModule are included
|
||||||
|
|
||||||
FormsModule,
|
FormsModule,
|
||||||
@@ -63,9 +75,9 @@ export class MainModule {
|
|||||||
universalCache[CacheService.KEY] = JSON.stringify(this.cache.dehydrate());
|
universalCache[CacheService.KEY] = JSON.stringify(this.cache.dehydrate());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear the cache after it's rendered
|
* Clear the cache after it's rendered
|
||||||
*/
|
*/
|
||||||
universalAfterDehydrate = () => {
|
universalAfterDehydrate = () => {
|
||||||
// comment out if LRU provided at platform level to be shared between each user
|
// comment out if LRU provided at platform level to be shared between each user
|
||||||
this.cache.clear();
|
this.cache.clear();
|
||||||
|
@@ -2,6 +2,7 @@ var webpack = require('webpack');
|
|||||||
var path = require('path');
|
var path = require('path');
|
||||||
var clone = require('js.clone');
|
var clone = require('js.clone');
|
||||||
var webpackMerge = require('webpack-merge');
|
var webpackMerge = require('webpack-merge');
|
||||||
|
let CopyWebpackPlugin = require('copy-webpack-plugin');
|
||||||
|
|
||||||
export var commonPlugins = [
|
export var commonPlugins = [
|
||||||
new webpack.ContextReplacementPlugin(
|
new webpack.ContextReplacementPlugin(
|
||||||
@@ -13,6 +14,11 @@ export var commonPlugins = [
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
|
|
||||||
|
new CopyWebpackPlugin([{
|
||||||
|
from: path.join(__dirname, 'resources', 'i18n'),
|
||||||
|
to: path.join('assets', 'i18n')
|
||||||
|
}]),
|
||||||
|
|
||||||
// Loader options
|
// Loader options
|
||||||
new webpack.LoaderOptionsPlugin({
|
new webpack.LoaderOptionsPlugin({
|
||||||
|
|
||||||
@@ -24,7 +30,7 @@ export var commonConfig = {
|
|||||||
devtool: 'source-map',
|
devtool: 'source-map',
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: ['.ts', '.js', '.json'],
|
extensions: ['.ts', '.js', '.json'],
|
||||||
modules: [ root('node_modules') ]
|
modules: [root('node_modules')]
|
||||||
},
|
},
|
||||||
context: __dirname,
|
context: __dirname,
|
||||||
output: {
|
output: {
|
||||||
@@ -34,10 +40,10 @@ export var commonConfig = {
|
|||||||
module: {
|
module: {
|
||||||
rules: [
|
rules: [
|
||||||
// TypeScript
|
// TypeScript
|
||||||
{ test: /\.ts$/, use: ['awesome-typescript-loader', 'angular2-template-loader'] },
|
{ test: /\.ts$/, use: ['awesome-typescript-loader', 'angular2-template-loader'] },
|
||||||
{ test: /\.html$/, use: 'raw-loader' },
|
{ test: /\.html$/, use: 'raw-loader' },
|
||||||
{ test: /\.css$/, use: 'raw-loader' },
|
{ test: /\.css$/, use: 'raw-loader' },
|
||||||
{ test: /\.scss$/, use: ['raw-loader', 'sass-loader'] },
|
{ test: /\.scss$/, use: ['raw-loader', 'sass-loader'] },
|
||||||
{ test: /\.json$/, use: 'json-loader' }
|
{ test: /\.json$/, use: 'json-loader' }
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user