Closed ozergul closed 3 years ago
Hello,
When you mean "subdomain strategy", do you mean fr.mydomain.com
?
You can play with:
alwaysSetPrefix
: to hide default language from route
alwaysSetPrefix: false
express Request
provider: to make your extension of LocalizeParser
to order the language liste (compute lang from subdomain and put it first in lang array)
class OptionalServerDeps {
constructor(
@Optional() @Inject(REQUEST) public request: Request
) {}
}
// --------------------------------------
parser: {
provide: LocalizeParser,
useFactory: (translate, location, settings, platformId, optionalDeps) => {
const hostname = isPlatformServer(platformId)? (optionalDeps.request.headers['x-forwarded-host'] ?? optionalDeps.request.hostname) : window.location.hostname;
const languages = ['en','de',...];
// do stuff with hostname and languages
return new ManualParserLoader(translate, location, settings, languages, 'YOUR_PREFIX');
},
deps: [TranslateService, Location, LocalizeRouterSettings, PLATFORM_ID, OptionalServerDeps]
}
defaultLangFunction
: to always take first lang of array (computed on previous step)
defaultLangFunction: (locales) => {
return locales[0];
}
The result will be:
What does it mean ?
Why is it cool to have this feature and not only switch to other domain ? It allows you to switch langs at runtime. Change lang while filling out a form? No problem, no page refresh is needed.
Looks fantastic. Lets give it a try!
I tried your saids and worked like a charm. But what if I wamt to not show default language. For example: mydomain.com -> EN fr.mydomin.com -> FR pt.mydomain.com -> PT
It's simple as "no language found is EN".
I don't know what you did into the part "// do stuff with hostname and languages" but if you used a regexp like /^(\w+).mydomain.com$/
, you just have to set a default lang when none are found.
const langMatch = hostname.match(/^(\w+).mydomain.com$/);
const langFound = langMatch ? langMatch[1].toUpperCase() : 'EN';
Hello. Thanks for the great package! I wonder that this package can be used at SSR and subdomain strategy?