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

Set route with country/lang code in url #141

Open
psmyda opened this issue Aug 24, 2018 · 4 comments
Open

Set route with country/lang code in url #141

psmyda opened this issue Aug 24, 2018 · 4 comments

Comments

@psmyda
Copy link

psmyda commented Aug 24, 2018

Here is my problem. I have to set up routing in Angular so that in URL there will be a country code and a language. for example

https://example.com/usa/en

The tricky part is that this localization parts can change so I have a button to switch country and language so it will be /gb/en or /usa/fr.

How can I get it working like this using localize-router and ngx-translate.

Any help much appreciated.

@jrmcdona
Copy link

jrmcdona commented Sep 4, 2018

@psmyda ever figure out an approach? I need locales in my urls such as

example.com/en-US
example.com/fr-FR

@psmyda
Copy link
Author

psmyda commented Sep 18, 2018

@jrmcdona Hi. Finally I have ended manipulating APP_BASE_HREF for setting country code. Its because its an Universal app.

@ghost
Copy link

ghost commented Sep 20, 2018

@psmyda I am also stuck on this, will it be possible to provide some details how you manipulated it?

@psmyda
Copy link
Author

psmyda commented Sep 21, 2018

Ok so APP_BASE_HREF, in this case, depends on the request URL.
If the request includes it (and its one of the allowed) it will be set otherwise I'm setting default one.
Here is a code for my AppConfigService:

import { Injectable, Injector, Inject, PLATFORM_ID } from "@angular/core";
import { isPlatformServer } from "@angular/common";
import { REQUEST } from "@nguniversal/express-engine/tokens";

@Injectable()
export class AppConfigService {
	public config: any = {
		appBaseHref: "/",
		country: "",
		currency: ""
	};

	constructor(
		private _injector: Injector,
		@Inject(PLATFORM_ID) private platformId: Object
	) {
		if (isPlatformServer(this.platformId)) {
			// -> server rendered
			const req = this._injector.get(REQUEST);
			let parseURL = req.originalUrl.split("/");

			this.setInitialData(parseURL[1]);
		} else {
			let parseURL = location.href.split("/");
			this.setInitialData(parseURL[3]);
		}
	}

	setInitialData(url) {
		if (url == "") {
			this.config.appBaseHref = "/us";
		} else if (url == "gb") {
			this.config.appBaseHref = "/gb";
		} else if (url == "us") {
			this.config.appBaseHref = "/us";
		}
	}

	set(k: string, v: any): void {
		this.config[k] = v;
	}

	get(k: string): any {
		return k ? this.config[k] : this.config;
	}
}

Then in AppModule I'm using it like this:

{
      provide: APP_BASE_HREF,
      useFactory: (config: AppConfigService) => {
        return config.get('appBaseHref')
      },
      deps: [ AppConfigService ]
    },

this is in Providers section for @NgModule

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants