Seeking a solution to implement a language switcher that seamlessly navigates users from the "en" side to the corresponding "de" page when they click on the language toggle. Currently, I am exploring the $state parameter and noticing that accessing the values I need through the "current" property is viable. However, I encounter some limitations when trying to pinpoint specific values within nested views.
I am considering an approach where, while on a URL like url/en/content, the language navigation dynamically loads destination points into a data attribute. With a custom directive, I aim to trigger a "go to" function and set the preferredLanguage value using angular-translate.
The main challenge lies in extracting the precise $state name; browsing $state reveals the desired values in the current object, but directly accessing $state.current only returns the parent state.
If anyone can suggest a more elegant Angular method (without resorting to custom cookies), I'm open to any recommendations.
Thank you!
Update! CODE SAMPLES:
Object reference of my states:
var urlStates = {
en: {
home: {
name: 'home',
url: '/en',
templateUrl: 'templates/'+lang+'/home.html',
abstract: 'true'
},
home_highlights: {
name:'home.highlights',
url: '',
templateUrl: 'templates/'+lang+'/home.highlights.html'
},
home_social:
{
name: 'home.social',
url: '/social',
templateUrl: 'templates/'+lang+'/home.social.html'
},
home_map:
{
name: 'home.map',
url: '/map',
templateUrl: 'templates/'+lang+'/home.map.html'
}
};
My States:
$stateProvider
.state(urlStates.en.home)
.state(urlStates.en.home_highlights)
.state(urlStates.en.home_social)
.state(urlStates.en.home_map);
$locationProvider.html5Mode(true);
})
Controller:
.controller('LandingPage', function($translate, $state){
this.state = $state;
this.greeting = "Hello";
});
And Lastly, the output I see in the dom:
With this.state = $state;
{
"params": {},
"current": {
"name": "home.highlights",
"url": "",
"templateUrl": "templates/en/home.highlights.html" },
"transition": null
}
With this.state = $state.current
{
"name": "",
"url": "^",
"views": null,
"abstract": true
}