Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Cesium
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
clients
Cesium-grp
Cesium
Commits
bd9c6c32
Commit
bd9c6c32
authored
8 years ago
by
Benoit Lavenier
Browse files
Options
Downloads
Patches
Plain Diff
- fix browser locale detection - fix #317
parent
e6db10a9
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
www/js/app.js
+7
-30
7 additions, 30 deletions
www/js/app.js
www/js/controllers/settings-controllers.js
+13
-8
13 additions, 8 deletions
www/js/controllers/settings-controllers.js
www/js/services/settings-services.js
+79
-35
79 additions, 35 deletions
www/js/services/settings-services.js
with
99 additions
and
73 deletions
www/js/app.js
+
7
−
30
View file @
bd9c6c32
...
...
@@ -407,29 +407,9 @@ angular.module('cesium', ['ionic', 'ionic-material', 'ngMessages', 'pascalprecht
}
});
var
onLanguageChange
=
function
()
{
var
lang
=
$translate
.
use
();
console
.
debug
(
'
[app] Locale [
'
+
lang
+
'
]
'
);
// config moment lib
try
{
moment
.
locale
(
lang
.
substr
(
0
,
2
));
}
catch
(
err
)
{
moment
.
locale
(
'
en
'
);
console
.
warn
(
'
[app] Unknown local for moment lib. Using default [en]
'
);
}
// config numeral lib
try
{
numeral
.
language
(
lang
.
substr
(
0
,
2
));
}
catch
(
err
)
{
numeral
.
language
(
'
en
'
);
console
.
warn
(
'
[app] Unknown local for numeral lib. Using default [en]
'
);
}
// Set some translation need by filters
// Update some translations, when locale changed
function
onLocaleChange
()
{
console
.
debug
(
'
[app] Loading cached translations for locale [{0}]
'
.
format
(
$translate
.
use
()));
$translate
([
'
COMMON.DATE_PATTERN
'
,
'
COMMON.DATE_SHORT_PATTERN
'
,
'
COMMON.UD
'
])
.
then
(
function
(
translations
)
{
$rootScope
.
translations
=
$rootScope
.
translations
||
{};
...
...
@@ -446,17 +426,14 @@ angular.module('cesium', ['ionic', 'ionic-material', 'ngMessages', 'pascalprecht
$rootScope
.
translations
.
UD
=
'
UD
'
;
}
});
};
// Set up moment translation
$rootScope
.
$on
(
'
$translateChangeSuccess
'
,
onLanguageChange
);
}
csSettings
.
api
.
locale
.
on
.
changed
(
$rootScope
,
onLocaleChange
,
this
);
// start plugin
PluginService
.
start
();
//
set locale to vendor lib
onL
anguag
eChange
();
//
Force at least on call
onL
ocal
eChange
();
})
;
...
...
This diff is collapsed.
Click to expand it.
www/js/controllers/settings-controllers.js
+
13
−
8
View file @
bd9c6c32
...
...
@@ -43,12 +43,14 @@ function SettingsController($scope, $q, $ionicPopup, $timeout, $translate, csHtt
// Fill locales
$scope
.
locales
=
angular
.
copy
(
csSettings
.
locales
);
var
locale
=
_
.
findWhere
(
$scope
.
locales
,
{
id
:
csSettings
.
defaultSettings
.
locale
.
id
});
// Apply settings
angular
.
merge
(
$scope
.
formData
,
csSettings
.
data
);
$scope
.
formData
.
locale
=
locale
;
if
(
csSettings
.
data
.
locale
&&
csSettings
.
data
.
locale
.
id
)
{
$scope
.
formData
.
locale
=
_
.
findWhere
(
$scope
.
locales
,
{
id
:
csSettings
.
data
.
locale
.
id
});
}
// Make sure to use full locale object (id+name)
$scope
.
formData
.
locale
=
(
csSettings
.
data
.
locale
&&
csSettings
.
data
.
locale
.
id
&&
_
.
findWhere
(
$scope
.
locales
,
{
id
:
csSettings
.
data
.
locale
.
id
}))
||
_
.
findWhere
(
$scope
.
locales
,
{
id
:
csSettings
.
defaultSettings
.
locale
.
id
});
$scope
.
loading
=
false
;
$timeout
(
function
()
{
...
...
@@ -63,9 +65,12 @@ function SettingsController($scope, $q, $ionicPopup, $timeout, $translate, csHtt
$scope
.
actionsPopover
.
hide
();
}
$scope
.
pendingSaving
=
true
;
csSettings
.
reset
();
angular
.
merge
(
$scope
.
formData
,
csSettings
.
data
);
csSettings
.
reset
()
.
then
(
function
()
{
// reload
$scope
.
load
();
$scope
.
pendingSaving
=
false
;
});
};
$scope
.
changeLanguage
=
function
(
langKey
)
{
...
...
This diff is collapsed.
Click to expand it.
www/js/services/settings-services.js
+
79
−
35
View file @
bd9c6c32
angular
.
module
(
'
cesium.settings.services
'
,
[
'
ngResource
'
,
'
ngApi
'
,
'
cesium.config
'
,
'
cesium.device.services
'
])
.
factory
(
'
csSettings
'
,
function
(
$q
,
Api
,
localStorage
,
$translate
,
csConfig
,
Device
)
{
.
factory
(
'
csSettings
'
,
function
(
$rootScope
,
$q
,
Api
,
localStorage
,
$translate
,
csConfig
,
Device
)
{
'
ngInject
'
;
function
Factory
()
{
...
...
@@ -30,13 +30,22 @@ angular.module('cesium.settings.services', ['ngResource', 'ngApi', 'cesium.confi
// If another locale exists with the same root: use it
var
similarLocale
=
_
.
find
(
locales
,
function
(
l
)
{
return
String
.
prototype
.
startsWith
.
call
(
l
,
locale
);
return
String
.
prototype
.
startsWith
.
call
(
l
.
id
,
locale
);
});
if
(
similarLocale
)
return
similarLocale
;
if
(
similarLocale
)
return
similarLocale
.
id
;
return
fallbackLocale
;
}
// Convert browser locale to app locale (fix #140)
function
fixLocaleWithLog
(
locale
)
{
var
fixedLocale
=
fixLocale
(
locale
);
if
(
locale
!=
fixedLocale
)
{
console
.
debug
(
'
[settings] Fix locale [{0}] -> [{1}]
'
.
format
(
locale
,
fixedLocale
));
}
return
fixedLocale
;
}
var
constants
=
{
STORAGE_KEY
:
'
CESIUM_SETTINGS
'
...
...
@@ -77,11 +86,11 @@ angular.module('cesium.settings.services', ['ngResource', 'ngApi', 'cesium.confi
notificationReadTime
:
0
},
locale
:
{
id
:
fixLocale
(
csConfig
.
defaultLanguage
||
$translate
.
use
())
// use config locale if set, or browser default
id
:
fixLocale
WithLog
(
csConfig
.
defaultLanguage
||
$translate
.
use
())
// use config locale if set, or browser default
}
},
csConfig
),
data
=
angular
.
copy
(
defaultSettings
)
,
data
=
{}
,
previousData
,
api
=
new
Api
(
this
,
"
csSettings
"
),
...
...
@@ -90,8 +99,10 @@ angular.module('cesium.settings.services', ['ngResource', 'ngApi', 'cesium.confi
_
.
keys
(
data
).
forEach
(
function
(
key
){
delete
data
[
key
];
});
angular
.
merge
(
data
,
defaultSettings
);
api
.
data
.
raisePromise
.
reset
(
data
)
applyData
(
defaultSettings
);
return
api
.
data
.
raisePromise
.
reset
(
data
)
.
then
(
store
);
},
...
...
@@ -129,7 +140,32 @@ angular.module('cesium.settings.services', ['ngResource', 'ngApi', 'cesium.confi
.
then
(
emitChangedEvent
);
},
restore
=
function
(
first
)
{
applyData
=
function
(
newData
)
{
var
localeChanged
=
false
;
if
(
newData
.
locale
&&
newData
.
locale
.
id
)
{
// Fix previously stored locale (could use bad format)
newData
.
locale
.
id
=
fixLocale
(
newData
.
locale
.
id
);
localeChanged
=
!
data
.
locale
||
newData
.
locale
.
id
!==
data
.
locale
.
id
||
newData
.
locale
.
id
!==
$translate
.
use
();
}
// Apply stored settings
angular
.
merge
(
data
,
newData
);
// Always force the usage of deffault settings
// This is a workaround for DEV (TODO: implement edition in settings ?)
data
.
timeWarningExpire
=
defaultSettings
.
timeWarningExpire
;
data
.
timeWarningExpireMembership
=
defaultSettings
.
timeWarningExpireMembership
;
data
.
cacheTimeMs
=
defaultSettings
.
cacheTimeMs
;
data
.
timeout
=
defaultSettings
.
timeout
;
// Apply the new locale (only if need)
if
(
localeChanged
)
{
$translate
.
use
(
fixLocale
(
data
.
locale
.
id
));
// will produce an event cached by onLocaleChange();
}
},
restore
=
function
()
{
var
now
=
new
Date
().
getTime
();
return
$q
(
function
(
resolve
,
reject
){
console
.
debug
(
"
[settings] Loading from local storage...
"
);
...
...
@@ -142,12 +178,9 @@ angular.module('cesium.settings.services', ['ngResource', 'ngApi', 'cesium.confi
// No settings stored
if
(
!
storedData
)
{
console
.
debug
(
"
[settings] No settings in local storage
"
);
if
(
data
.
locale
.
id
!==
$translate
.
use
())
{
console
.
debug
(
"
[settings] Changing locale to [{0}]...
"
.
format
(
data
.
locale
.
id
));
$translate
.
use
(
data
.
locale
.
id
);
console
.
debug
(
"
[settings] No settings in local storage. Using defaults.
"
);
applyData
(
defaultSettings
);
finishRestore
();
}
return
;
}
...
...
@@ -176,37 +209,48 @@ angular.module('cesium.settings.services', ['ngResource', 'ngApi', 'cesium.confi
delete
storedData
.
DUNITER_NODE_ES
;
}
var
localeChanged
=
false
;
if
(
storedData
.
locale
&&
storedData
.
locale
.
id
)
{
// Fix previously stored bad locale
storedData
.
locale
.
id
=
fixLocale
(
storedData
.
locale
.
id
);
localeChanged
=
(
storedData
.
locale
.
id
!==
data
.
locale
.
id
||
storedData
.
locale
.
id
!==
$translate
.
use
());
}
// Apply stored data
applyData
(
storedData
);
// Apply stored settings
angular
.
merge
(
data
,
storedData
);
console
.
debug
(
'
[settings] Loaded from local storage in
'
+
(
new
Date
().
getTime
()
-
now
)
+
'
ms
'
);
finishRestore
();
});
},
// Always force the usage of deffault settings
// This is a workaround for DEV (TODO: implement edition in settings ?)
data
.
timeWarningExpire
=
defaultSettings
.
timeWarningExpire
;
data
.
timeWarningExpireMembership
=
defaultSettings
.
timeWarningExpireMembership
;
data
.
cacheTimeMs
=
defaultSettings
.
cacheTimeMs
;
data
.
timeout
=
defaultSettings
.
timeout
;
// Detect locale sucessuf changes, then apply to vendor libs
onLocaleChange
=
function
()
{
var
locale
=
$translate
.
use
();
console
.
debug
(
'
[settings] Locale [
'
+
locale
+
'
]
'
);
// Apply the new locale (only if need)
if
(
localeChanged
)
{
$translate
.
use
(
fixLocale
(
data
.
locale
.
id
));
// config moment lib
try
{
moment
.
locale
(
locale
.
substr
(
0
,
2
));
}
catch
(
err
)
{
moment
.
locale
(
'
en
'
);
console
.
warn
(
'
[settings] Unknown local for moment lib. Using default [en]
'
);
}
console
.
debug
(
'
[settings] Loaded from local storage in
'
+
(
new
Date
().
getTime
()
-
now
)
+
'
ms
'
);
finishRestore
();
});
// config numeral lib
try
{
numeral
.
language
(
locale
.
substr
(
0
,
2
));
}
catch
(
err
)
{
numeral
.
language
(
'
en
'
);
console
.
warn
(
'
[settings] Unknown local for numeral lib. Using default [en]
'
);
}
// Emit event
api
.
locale
.
raise
.
changed
(
locale
);
};
$rootScope
.
$on
(
'
$translateChangeSuccess
'
,
onLocaleChange
);
api
.
registerEvent
(
'
data
'
,
'
reset
'
);
api
.
registerEvent
(
'
data
'
,
'
changed
'
);
api
.
registerEvent
(
'
data
'
,
'
store
'
);
api
.
registerEvent
(
'
data
'
,
'
ready
'
);
api
.
registerEvent
(
'
locale
'
,
'
changed
'
);
return
{
data
:
data
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment