<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Micha</title>
    <description>The latest articles on DEV Community by Micha (@srcmgra).</description>
    <link>https://dev.to/srcmgra</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F772032%2F70d78203-8808-47c5-93cc-f111dc6279eb.jpg</url>
      <title>DEV Community: Micha</title>
      <link>https://dev.to/srcmgra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/srcmgra"/>
    <language>en</language>
    <item>
      <title>use ngx-translate in angular apps</title>
      <dc:creator>Micha</dc:creator>
      <pubDate>Fri, 03 Jun 2022 09:14:06 +0000</pubDate>
      <link>https://dev.to/srcmgra/use-ngx-translate-in-angular-apps-56gl</link>
      <guid>https://dev.to/srcmgra/use-ngx-translate-in-angular-apps-56gl</guid>
      <description>&lt;p&gt;This is a small introduction to multi language support in angular by npm package ngx-translate.&lt;br&gt;
First install ngx-translate in your project:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install @ngx-translate/core --save&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;use in your package.json this entries: works at test-time with versions below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"dependencies": {
 "@ngx-translate/core": "^11.0.1",
 "@ngx-translate/http-loader": "^4.0.0"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you need to add and configure the TranslateModule to your app.module.ts to load the i18n files. Add Import Statments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpClientModule, HttpClient} from '@angular/common/http';
import { TranslateModule, TranslateLoader, TranslatePipe } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import {TranslateService} from "@ngx-translate/core";
import {AppComponent} from './app';

// Translation Loader Factory
export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, '../assets/i18n/', '.json');
}
@NgModule({
        declarations: [AppComponent],
  imports: [BrowserModule, MatIconModule, HttpClientModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: createTranslateLoader,
        deps: [HttpClient],
      }
    })],
  exports: [ TranslateModule, TranslatePipe],
  bootstrap: [AppComponent]
})
// &amp;gt;&amp;gt;&amp;gt; add TranslateService to constructor
 export class AppComponent {
             constructor(translate: TranslateService) {
                 translate.setDefaultLang('en');
                 translate.use('en');
             }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use the TranslateService in this way in your component and set the default language:&lt;/p&gt;

&lt;p&gt;In app.component.ts add this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Component } from '@angular/core';
import {TranslateService} from "@ngx-translate/core";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor(private translate: TranslateService) {
    translate.setDefaultLang('en');
    translate.use('en');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It make sense to get the default language from your device. Replace the code in constructor with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const _defLang = window.navigator.language;
translate.setDefaultLang(_defLang);
translate.use(_defLang);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your code use TrasnlateService like this:&lt;br&gt;
&lt;code&gt;translate.get('demo.title').subscribe((res: string) =&amp;gt; { ... });&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In your HTML-view use pipe:&lt;br&gt;
&lt;code&gt;{{ 'demo.title' | translate }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create your JSON translation file in assets-folder.&lt;br&gt;
Each language is stored in a separate JSON file. &lt;/p&gt;

&lt;p&gt;Add the English translation to file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;assets/i18n/en-EN.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Use a translation ID for each text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "demo.title": "Translation demo",
  "demo.text": "This is a simple demonstration app for ngx-translate"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also add a file for german language:&lt;br&gt;
 &lt;code&gt;assets/i18n/de-DE.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "demo.title": "Übersetzungs demo",
  "demo.text": "Das ist eine einfache Beispiel App für ngx-translate."
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this Translation IDs in your components to get the text for the set default language.&lt;/p&gt;

&lt;p&gt;I hope this short documentation helps to get in ngx-translate within angular a little bit faster.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;refer to:&lt;/em&gt;&lt;br&gt;
&lt;a href="https://phrase.com/blog/posts/angular-localization-i18n/"&gt;https://phrase.com/blog/posts/angular-localization-i18n/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.codeandweb.com/babeledit/tutorials/how-to-translate-your-angular-app-with-ngx-translate"&gt;https://www.codeandweb.com/babeledit/tutorials/how-to-translate-your-angular-app-with-ngx-translate&lt;/a&gt;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language&lt;/a&gt;&lt;br&gt;
&lt;a href="https://simpleen.io/translate-angular-i18n"&gt;https://simpleen.io/translate-angular-i18n&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>webdev</category>
      <category>trasnlation</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>use chart.js with node</title>
      <dc:creator>Micha</dc:creator>
      <pubDate>Tue, 31 May 2022 10:13:44 +0000</pubDate>
      <link>https://dev.to/srcmgra/use-chartjs-with-node-1e2k</link>
      <guid>https://dev.to/srcmgra/use-chartjs-with-node-1e2k</guid>
      <description>&lt;p&gt;With nodeJS you can build a web server which can serve a service or a web app. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var http = require('http');

//create a server object:
http.createServer(function (req, res) {
  res.write('Hello World!'); //write a response to the client
  res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Chartjs makes it easy to get a graph of some values. Here a example in HTML embedded:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;canvas id="myChart" style="width:100%;max-width:700px"&amp;gt;&amp;lt;/canvas&amp;gt;

&amp;lt;script&amp;gt;
var xyValues = [
  {x:50, y:7},
  {x:60, y:8},
  {x:70, y:8},
  {x:80, y:9},
  {x:140, y:14},
  {x:150, y:15}
];

new Chart("myChart", {
  type: "scatter",
  data: {
    datasets: [{
      pointRadius: 4,
      pointBackgroundColor: "rgb(0,0,255)",
      data: xyValues
    }]
  },
  options: {
    legend: {display: false},
    scales: {
      xAxes: [{ticks: {min: 40, max:160}}],
      yAxes: [{ticks: {min: 6, max:16}}],
    }
  }
});
&amp;lt;/script&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can dtermine the scale of the axis by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; scales: {
      xAxes: [{ticks: {min: 40, max:160}}],
      yAxes: [{ticks: {min: 6, max:16}}],
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can be usefull to bring the chart feature to nodeJS for evaluations.&lt;br&gt;
You can integrate this in a nodeJS-server by using a string(_html) for the chart-Call it self.&lt;/p&gt;

&lt;p&gt;showChart.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs');
var express = require('express');
var app = express();
var path = require('path');
var Chart = require('chart.js');
var result =[3,6,9];
const port = 3000;

app.get('/', function(req, res){
    let _resLine = 'Events: ' + result;
    console.log('show chart:');
    console.log(_resLine);

    _html = "&amp;lt;script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js'&amp;gt;&amp;lt;/script&amp;gt;"+
    "&amp;lt;canvas id='bar-chart' width='800' height='450'&amp;gt;&amp;lt;/canvas&amp;gt;"+
    "&amp;lt;script&amp;gt;"+
    "var logChart = new Chart(document.getElementById('bar-chart'), {"+
    "type: 'horizontalBar',"+
    "data: {"+
      "labels: ['res1', 'res2', 'res3'],"+
      "datasets: ["+
        "{"+
          "label: 'calls',"+
          "backgroundColor: ['#3e95cd', '#8e5ea2','#3cba9f'],"+
          "data: ["+result[0]+","+result[1]+","+result[2]+"]"+
        "}"+
      "]"+
      "},"+
    "options: {"+
      "legend: { display: false },"+
    "scales: {"+
      "xAxes: [{ticks: {min: 0, max:10}}],"+
      "yAxes: [{ticks: {min: 0, max:5}}],"+
     "},"+
      "title: {"+
        "display: true,"+
        "text: 'Events '"+
      "}"+
    "}"+
    "});"+
    "&amp;lt;/script&amp;gt;";

    res.send(_html);
});

app.listen(port);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;run it with:&lt;/em&gt;&lt;br&gt;
node showChart.js&lt;/p&gt;

&lt;p&gt;check it on:&lt;br&gt;
&lt;a href="http://localhost:3000/"&gt;http://localhost:3000/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;refer to:&lt;br&gt;
&lt;a href="https://www.w3schools.com/js/tryit.asp?filename=tryai_chartjs_scatter"&gt;https://www.w3schools.com/js/tryit.asp?filename=tryai_chartjs_scatter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://nodejs.org/api/synopsis.html"&gt;https://nodejs.org/api/synopsis.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>chart</category>
    </item>
    <item>
      <title>Ionic 4 angular router + NavigationExtras interface</title>
      <dc:creator>Micha</dc:creator>
      <pubDate>Fri, 27 May 2022 08:52:27 +0000</pubDate>
      <link>https://dev.to/srcmgra/ionic-4-angular-router-navigationextras-interface-41m3</link>
      <guid>https://dev.to/srcmgra/ionic-4-angular-router-navigationextras-interface-41m3</guid>
      <description>&lt;p&gt;&lt;strong&gt;Example to pass data between components in Ionic 4/angular:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;using NavigationExtras interface&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Especially when passing data in dialogs there is often no parent/child-relation ship. So you can't pass data in angular way (parent -&amp;gt; child). In Ionic 4/angular you have this feature to pass data comfortable within the router by the interface &lt;em&gt;NavigationExtras&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;page1 pass data to page2&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;page1.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {NavigationExtras, Router} from '@angular/router';
import {Component} from '@angular/core';

@Component({
    selector: 'page1',
    templateUrl: 'page1.html',
    styleUrls: ['page1.scss'],
})
export class Page1 {
    public data: string;
    public value: string;

    constructor(private router: Router) {
    }
    // route page2 and set params in NavigationExtras
    public routePage2() {
        const navigationExtras: NavigationExtras = {
            state: {
                data: this.data,
                value: this.value
            }
        };
        this.router.navigate(['/page2'], navigationExtras);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;page2.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {ActivatedRoute, Router} from '@angular/router';
import {Component} from '@angular/core';
@Component({
    selector: 'page2',
    templateUrl: 'page2.html',
    styleUrls: ['page2.scss'],
})
export class Page2 {
    public data: string;
    public value: string;

    constructor(private route: ActivatedRoute, private router: Router) {
        this.route.queryParams.subscribe(params =&amp;gt; {
            if (this.router.getCurrentNavigation().extras.state) {
                this.data = this.router.getCurrentNavigation().extras.state.data;
                this.value = this.router.getCurrentNavigation().extras.state.value;
            }
        });
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;refer to:&lt;/strong&gt; point 4&lt;br&gt;
&lt;a href="https://ngrefs.com/router/navigationextras"&gt;https://ngrefs.com/router/navigationextras&lt;/a&gt;&lt;br&gt;
&lt;a href="https://angular.io/api/router/NavigationExtras"&gt;https://angular.io/api/router/NavigationExtras&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ionic</category>
      <category>angular</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
