(js) Dynamically load localizable strings

pull/229/merge
Francis Lachapelle 2018-08-02 17:59:52 -04:00
parent 30a81528a2
commit fccbdb3076
5 changed files with 93 additions and 8 deletions

View File

@ -28,6 +28,7 @@
#import <SOGo/NSArray+Utilities.h>
#import <SOGo/NSDictionary+Utilities.h>
#import <SOGo/NSString+Utilities.h>
#import <SOGo/SOGoUser.h>
#import <SOGo/SOGoUserSettings.h>
#import <SOGo/SOGoSystemDefaults.h>
@ -213,8 +214,9 @@
/* page based JavaScript */
- (NSString *) _stringsForFramework: (NSString *) framework
- (NSDictionary *) _stringsForFramework: (NSString *) framework
{
NSDictionary *moreStrings;
NSString *language, *frameworkName;
NSMutableDictionary* strings;
SOGoUserDefaults *ud;
@ -235,10 +237,13 @@
strings = [NSMutableDictionary dictionaryWithDictionary: table];
if (!framework)
if (framework)
{
moreStrings = [NSDictionary dictionaryWithObjectsAndKeys: [NSArray arrayWithObject: framework], @"_loadedFrameworks", nil];
}
else
{
// Add strings from Locale
NSDictionary *moreStrings;
// AM/PM
moreStrings = [NSDictionary dictionaryWithObjects: [locale objectForKey: NSAMPMDesignation]
@ -253,16 +258,17 @@
// Short month names
moreStrings = [NSDictionary dictionaryWithObjects: [locale objectForKey: NSShortMonthNameArray]
forKeys: [UIxComponent abbrMonthLabelKeys]];
[strings addEntriesFromDictionary: moreStrings];
}
[strings addEntriesFromDictionary: moreStrings];
/* table is not really an NSDictionary but a hackish variation thereof */
return [strings jsonRepresentation];
return strings;
}
- (NSString *) commonLocalizableStrings
{
return [NSString stringWithFormat: @"var clabels = %@;", [self _stringsForFramework: nil]];
return [NSString stringWithFormat: @"var clabels = %@;",
[[self _stringsForFramework: nil] jsonRepresentation]];
}
- (NSString *) productLocalizableStrings
@ -271,7 +277,32 @@
frameworkName = [[context page] frameworkName];
return [NSString stringWithFormat: @"var labels = %@;", [self _stringsForFramework: frameworkName]];
return [NSString stringWithFormat: @"var labels = %@;",
[[self _stringsForFramework: frameworkName] jsonRepresentation]];
}
- (WOResponse *) labelsAction
{
WOResponse *response;
NSDictionary *params, *data;
NSString *frameworkName;
params = [[[context request] contentAsString] objectFromJSONString];
frameworkName = [params objectForKey: @"framework"];
if (frameworkName)
{
data = [NSDictionary dictionaryWithObject: [self _stringsForFramework: frameworkName]
forKey: @"labels"];
response = [self responseWithStatus: 200 andJSONRepresentation: data];
}
else
{
data = [NSDictionary dictionaryWithObject: @"Missing framework name"
forKey: @"message"];
response = [self responseWithStatus: 400 andJSONRepresentation: data];
}
return response;
}
- (NSString *) angularModule

View File

@ -71,6 +71,15 @@
};
};
};
SOGoUserFolder = {
methods = {
labels = {
protectedBy = "Access Contents Information";
pageName = "UIxPageFrame";
actionName = "labels";
};
};
};
SOGoParentFolder = {
methods = {
createFolder = {

View File

@ -535,6 +535,11 @@ static SoProduct *commonProduct = nil;
/* labels */
- (NSString *) framework
{
return [[context page] frameworkName];
}
- (NSString *) labelForKey: (NSString *) _str
{
WOResourceManager *rm;

View File

@ -12,7 +12,8 @@
nv-file-drop="nv-file-drop"
nv-file-over="nv-file-over"
over-class="sg-over-dropzone"
uploader="editor.uploader">
uploader="editor.uploader"
var:sg-labels="framework">
<form name="messageForm">
<md-toolbar>
<div class="md-toolbar-tools">

View File

@ -0,0 +1,39 @@
/* -*- Mode: javascript; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
(function() {
'use strict';
/*
* sgLabels - Load the localizable strings of the specified framework.
* @memberof SOGo.Common
* @restrict attribute
* @param {object} sgLabels - the framework name
* @ngInject
* @example:
<md-dialog sg-labels="MailerUI"><!-- .. --></md-dialog>
*/
sgLabels.$inject = ['sgSettings', 'Resource', '$window'];
function sgLabels(Settings, Resource, $window) {
return {
restrict: 'A',
link: sgLabelsLink
};
function sgLabelsLink(scope, element, attrs) {
var framework = attrs.sgLabels;
var resource = new Resource(Settings.activeUser('folderURL'), Settings.activeUser());
if (!_.includes($window.labels._loadedFrameworks, framework)) {
resource.post('labels', null, { framework: framework }).then(function(data) {
var loadedFrameworks = $window.labels._loadedFrameworks;
angular.extend($window.labels, data.labels);
$window.labels._loadedFrameworks = _.concat($window.labels._loadedFrameworks, loadedFrameworks);
});
}
}
}
angular
.module('SOGo.Common')
.directive('sgLabels', sgLabels);
})();