Localization is all the rage. Maybe that's a stretch...but it should be. I don't think there's any doubt of it's importance. It gives your site and content further ability to reach the entire connected world. Making that happen though is another story. Thankfully Laravel has a great localization class for this...but what's the best way to implement it?
I have site with two main localization objectives: detect the user's default browser language to know what language to display your content in...and provide a global dropdown giving the user the ability to translate your content on the fly.
So first...in my
config/languages.php I have an array for the languages I'm currently supporting:
'available' => [ 'en' => 'English', 'zh' => 'Chinese',
I had a scenerio in my Ionic/Angular application where I wanted to translate only a certain block of text seperate from the rest of the text in my Application. Meaning I didn't want the whole app to translate itself when I translate said block of text. So how can this be done? A quick look into the documentation for
$translate you'll notice a 4th optional parameter: forceLanguage.
So instead of using
$translate.use(key) (which would set the locale for my whole application) I used forceLanguage in a simple function. First I created my language dropdown:
<select id="shareSelect" ng-model="optionSelected" ng-change="onSelectLangChange(optionSelected)"> <option value="default" ng-selected="selected">Select a Language</option>