Skip to content

Commit

Permalink
Add new Locale widget which enables switching locales within an app…
Browse files Browse the repository at this point in the history
…lication.
  • Loading branch information
tmcgee committed Aug 7, 2016
1 parent ea1d486 commit 6ee0cc6
Show file tree
Hide file tree
Showing 17 changed files with 498 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This JavaScript web app can be easily configured or used as a boilerplate/starti
- Identify
- Layer Control (Table of Contents)
- Legend
- Locale (Change the Country + Language)
- Locate Button (Geolocation)
- MapInfo
- Measure
Expand Down
3 changes: 3 additions & 0 deletions viewer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
</div>
</div>
<script type="text/javascript">
var s = window.location.search, q = s.match(/locale=([^&]*)/i);
var locale = (q && q.length > 0) ? q[1] : null;
window.dojoConfig = {
locale: locale,
async: true
};
</script>
Expand Down
1 change: 1 addition & 0 deletions viewer/js/config/nls/es/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ define({
measure: 'Medición',
layerControl: 'Capas',
legend: 'Leyenda',
locale: 'Lugar',
print: 'Imprimir',
streetview: 'Google Street View'
}
Expand Down
1 change: 1 addition & 0 deletions viewer/js/config/nls/fr/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ define({
measure: 'Mesures',
layerControl: 'Couches d\'information',
legend: 'Légende',
locale: 'Lieu',
print: 'Impression',
streetview: 'Google Street View'
}
Expand Down
1 change: 1 addition & 0 deletions viewer/js/config/nls/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ define({
measure: 'Measurement',
layerControl: 'Layers',
legend: 'Legend',
locale: 'Locale',
print: 'Print',
streetview: 'Google Street View'
}
Expand Down
1 change: 1 addition & 0 deletions viewer/js/config/nls/pt-br/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ define({
measure: 'Medir',
layerControl: 'Camadas',
legend: 'Legendas',
locale: 'Localidade',
print: 'Imprimir',
streetview: 'Google Street View'
}
Expand Down
1 change: 1 addition & 0 deletions viewer/js/config/nls/pt-pt/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ define({
measure: 'Medir',
layerControl: 'Camadas',
legend: 'Legendas',
locale: 'Localidade',
print: 'Imprimir',
streetview: 'Google Street View'
}
Expand Down
14 changes: 14 additions & 0 deletions viewer/js/config/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,20 @@ define([
mapRightClickMenu: true
}
},
locale: {
include: true,
id: 'locale',
//type: 'titlePane',
//position: 0,
//open: true,
type: 'domNode',
srcNodeRef: 'geocodeDijit',
path: 'gis/dijit/Locale',
title: i18n.viewer.widgets.locale,
options: {
style: 'margin-left: 30px;'
}
},
help: {
include: true,
id: 'help',
Expand Down
187 changes: 187 additions & 0 deletions viewer/js/gis/dijit/Locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
define([
'dojo/_base/declare',
'dijit/_WidgetBase',
'dijit/_TemplatedMixin',

'dojo/_base/lang',
'dojo/on',
'dojo/dom-style',
'dojo/_base/array',
'dojo/_base/kernel',
'dojo/io-query',

'dijit/form/DropDownButton',
'dijit/DropDownMenu',
'dijit/MenuItem',

'./Locale/countries',

'dojo/text!./Locale/templates/Locale.html',
'dojo/i18n!./Locale/nls/resource',

'xstyle/css!https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/2.3.1/css/flag-icon.min.css',
'xstyle/css!./Locale/css/Locale.css'
], function (
declare,
_WidgetBase,
_TemplatedMixin,

lang,
on,
domStyle,
array,
kernel,
ioQuery,

DropDownButton,
DropDownMenu,
MenuItem,

countries,

template,
i18n
) {

return declare([_WidgetBase, _TemplatedMixin], {
templateString: template,
i18n: i18n,
baseClass: 'cmvLocaleDijit',

currentLocale: null,

includeFlag: true,
includeCountry: true,
includeLanguage: true,

languages: {
'en': 'English',
'es': 'Español',
'fr': 'Français',
'pt': 'Português'
},

locales: [
'es-ar',
'es-bo',
'pt-br',
'en-ca',
'fr-ca',
'es-cl',
'es-co',
'es-cr',
'es-do',
'es-ec',
'es-sv',
'fr-FR',
'es-gt',
'fr-ht',
'es-hn',
'en-in',
'es-mx',
'es-pa',
'es-pe',
'es-pr',
'pt-pt',
'es-py',
'es-es',
'en-gb',
'en-us',
'es-us',
'es-uy',
'es-ve'
],

postCreate: function () {
this.inherited(arguments);

this.currentLocale = kernel.locale;

if (this.parentWidget) {
if (this.parentWidget.toggleable) {
domStyle.set(this.localeLabelContainer, 'display', 'block');
}
}

var menu = new DropDownMenu({
baseClass: 'localeMenu'
});

array.forEach(this.locales, lang.hitch(this, function (locale) {
var vals = locale.split('-');

// only include supported languages
var language = this.languages[vals[0]];
if (language) {
var label = '', country = null, icon = null;
if (vals[1]) {
if (this.includeFlag) {
icon = 'flag-icon flag-icon-' + vals[1].toLowerCase();
}
country = countries[vals[1].toUpperCase()];
if (country && this.includeCountry) {
label = country;
}
}
if (this.includeLanguage) {
if (label.length > 0) {
label += ' - ';
}
label += language;
}
var menuItem = new MenuItem({
id: locale,
label: label,
iconClass: icon,
onClick: lang.hitch(this, 'switchLocale', locale)
});
menu.addChild(menuItem);
}
}));
menu.startup();

var vals = this.currentLocale.split('-');
var language = this.languages[vals[0]];
var label = '', country = null;
if (vals[1]) {
if (this.includeFlag) {
label = '<div class="flag-icon flag-icon-' + vals[1].toLowerCase() + '"></div>';
}
country = countries[vals[1].toUpperCase()];
if (country && this.includeCountry) {
label += country;
}
if (this.includeLanguage) {
if (country && this.includeCountry) {
label += ' - ';
}
label += language;
}
}

var button = new DropDownButton({
label: label,
dropDown: menu
});

this.localeDropDownContainer.appendChild(button.domNode);
},

switchLocale: function (newLocale) {
if (newLocale !== this.currentLocale) {
var uri = window.location.href, qsObj = {};
if (uri.indexOf('?') > -1) {
var qs = uri.substring(uri.indexOf('?') + 1, uri.length);
qsObj = ioQuery.queryToObject(qs);
}

// set the new locale
qsObj.locale = newLocale;

// reload the page
window.location = window.location.pathname + '?' + ioQuery.objectToQuery(qsObj);

}
}
});
});
Loading

0 comments on commit 6ee0cc6

Please sign in to comment.