From fccbdb30769921fb8b1f89a17f3d11c91fc864e5 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Thu, 2 Aug 2018 17:59:52 -0400 Subject: [PATCH] (js) Dynamically load localizable strings --- UI/Common/UIxPageFrame.m | 45 ++++++++++++++++--- UI/Common/product.plist | 9 ++++ UI/SOGoUI/UIxComponent.m | 5 +++ UI/Templates/MailerUI/UIxMailEditor.wox | 3 +- .../js/Common/sgLabels.directive.js | 39 ++++++++++++++++ 5 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 UI/WebServerResources/js/Common/sgLabels.directive.js diff --git a/UI/Common/UIxPageFrame.m b/UI/Common/UIxPageFrame.m index 9e46bda52..dd2cd4e94 100644 --- a/UI/Common/UIxPageFrame.m +++ b/UI/Common/UIxPageFrame.m @@ -28,6 +28,7 @@ #import #import +#import #import #import #import @@ -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 diff --git a/UI/Common/product.plist b/UI/Common/product.plist index c8b7a62b2..208818681 100644 --- a/UI/Common/product.plist +++ b/UI/Common/product.plist @@ -71,6 +71,15 @@ }; }; }; + SOGoUserFolder = { + methods = { + labels = { + protectedBy = "Access Contents Information"; + pageName = "UIxPageFrame"; + actionName = "labels"; + }; + }; + }; SOGoParentFolder = { methods = { createFolder = { diff --git a/UI/SOGoUI/UIxComponent.m b/UI/SOGoUI/UIxComponent.m index b04cab702..1d6c4e28b 100644 --- a/UI/SOGoUI/UIxComponent.m +++ b/UI/SOGoUI/UIxComponent.m @@ -535,6 +535,11 @@ static SoProduct *commonProduct = nil; /* labels */ +- (NSString *) framework +{ + return [[context page] frameworkName]; +} + - (NSString *) labelForKey: (NSString *) _str { WOResourceManager *rm; diff --git a/UI/Templates/MailerUI/UIxMailEditor.wox b/UI/Templates/MailerUI/UIxMailEditor.wox index b2189632b..cd5260234 100644 --- a/UI/Templates/MailerUI/UIxMailEditor.wox +++ b/UI/Templates/MailerUI/UIxMailEditor.wox @@ -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">
diff --git a/UI/WebServerResources/js/Common/sgLabels.directive.js b/UI/WebServerResources/js/Common/sgLabels.directive.js new file mode 100644 index 000000000..dcc78455e --- /dev/null +++ b/UI/WebServerResources/js/Common/sgLabels.directive.js @@ -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: + + + */ + 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); +})();