There is an 'Angular' approach, however, I'm not sure how useful it is over a simple Javascript solution. Regardless, you can use the $location
service to search the current URL. Assuming you have lang
as your parameter, you would use $location.search().lang
to return the value of lang
(if it exists).
You would then create a service or factory to asynchronously load Google Maps using the language parameter. You will also need to supply a callback method. That callback method would be a promise. Here is sample factory:
app.factory('GMapsFactory', function($q, $window) {
var _base = 'https://maps.googleapis.com/maps/api/js?language=';
return {
loadLocale: function(loc) {
var _defer = $q.defer();
var _tag = document.createElement('script');
_tag.src = _base + loc + '&callback=gmLocaleCallback';
$window.gmLocaleCallback = _defer.resolve;
document.body.appendChild(_tag);
return _defer.promise;
}
};
});
This is useful as a factory as it's portable from application to application. However, in a single application, it's useful once as you wouldn't call upon it multiple times.
Then, in your controller:
app.controller('MainCtrl', function($scope, $location, GMapsFactory) {
var lang = $location.search().lang || 'en';
GMapsFactory.loadLocale(lang).then(function() {
console.log(google.maps);
});
});
Unless you have a reason where you need to use Angular, the simple approach again would be plain javascript in your index:
// From SO solution: http://stackoverflow.com/a/901144/715597
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var _lang = getParameterByName('lang');
document.write('<script src="https://maps.googleapis.com/maps/api/js?language='+_lang+'">\x3C/script>');