Merge pull request #25 from gnustep/import_xliff

This commit is contained in:
Gregory Casamento 2023-07-17 17:32:33 -04:00 committed by GitHub
commit dba7b74b48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 757 additions and 362 deletions

View file

@ -9,19 +9,21 @@
"createSubclass:",
"endTesting:",
"exportStrings:",
"exportXLIFFDocument:",
"groupSelectionInBox:",
"groupSelectionInMatrix:",
"groupSelectionInScrollView:",
"groupSelectionInSplitView:",
"groupSelectionInView:",
"guideline:",
"importXLIFFDocument:",
"inspector:",
"instantiateClass:",
"loadClass:",
"loadImage:",
"loadPalette:",
"loadSound:",
"exportXLIFFDocument:",
"newAction:",
"orderFrontFontPanel:",
"palettes:",
"preferencesPanel:",
@ -105,7 +107,10 @@
"loadClass:",
"addAttributeToClass:",
"remove:",
"exportXLIFFDocument:"
"exportXLIFFDocument:",
"importXLIFFDocument:",
"exportStrings:",
"translate:"
);
Outlets = (
gormMenu,

View file

@ -85,7 +85,10 @@
- (IBAction) loadPalette: (id) sender;
// Translation
- (IBAction) importXLIFFDocument: (id)sender;
- (IBAction) exportXLIFFDocument: (id)sender;
- (IBAction) translate: (id)sender;
- (IBAction) exportStrings: (id)sender;
// Print
- (IBAction) print: (id)sender;

View file

@ -33,6 +33,12 @@
#import "GormAppDelegate.h"
#import "GormLanguageViewController.h"
@interface GormDocument (Private)
- (NSMutableArray *) _collectAllObjects;
@end
@implementation GormAppDelegate
// App delegate...
@ -572,7 +578,71 @@
}
// Translation
- (IBAction) exportXLIFFDocument: (id)sender;
- (IBAction) importXLIFFDocument: (id)sender
{
NSArray *fileTypes = [NSArray arrayWithObjects: @"xliff", nil];
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
int result;
[oPanel setAllowsMultipleSelection: NO];
[oPanel setCanChooseFiles: YES];
[oPanel setCanChooseDirectories: NO];
result = [oPanel runModalForDirectory: nil
file: nil
types: fileTypes];
if (result == NSOKButton)
{
GormDocument *doc = (GormDocument *)[self activeDocument];
NSMutableArray *allObjects = [doc _collectAllObjects];
NSString *filename = [oPanel filename];
NSEnumerator *en = nil;
id obj = nil;
BOOL result = NO;
NS_DURING
{
GormXLIFFDocument *xd = [GormXLIFFDocument xliffWithGormDocument: doc];
result = [xd importXLIFFDocumentWithName: filename];
}
NS_HANDLER
{
NSString *message = [localException reason];
NSRunAlertPanel(_(@"Problem loading XLIFF"),
message, nil, nil, nil);
}
NS_ENDHANDLER;
// If actual translation was done, then refresh the objects...
if (result == YES)
{
[doc touch]; // mark the document as modified...
// change to translated values.
en = [allObjects objectEnumerator];
while((obj = [en nextObject]) != nil)
{
if([obj isKindOfClass: [NSView class]])
{
[obj setNeedsDisplay: YES];
}
// redisplay/flush, if the object is a window.
if([obj isKindOfClass: [NSWindow class]])
{
NSWindow *w = (NSWindow *)obj;
[w setViewsNeedDisplay: YES];
[w disableFlushWindow];
[[w contentView] setNeedsDisplay: YES];
[[w contentView] displayIfNeeded];
[w enableFlushWindow];
[w flushWindowIfNeeded];
}
}
}
}
}
- (IBAction) exportXLIFFDocument: (id)sender
{
NSSavePanel *savePanel = [NSSavePanel savePanel];
NSBundle *bundle = [NSBundle bundleForClass: [GormLanguageViewController class]];
@ -602,9 +672,11 @@
if (NSModalResponseOK == result)
{
NSString *filename = [[savePanel URL] path];
[doc exportXLIFFDocumentWithName: filename
withSourceLanguage: [_vc sourceLanguageIdentifier]
andTargetLanguage: [_vc targetLanguageIdentifier]];
GormXLIFFDocument *xd = [GormXLIFFDocument xliffWithGormDocument: doc];
[xd exportXLIFFDocumentWithName: filename
withSourceLanguage: [_vc sourceLanguageIdentifier]
andTargetLanguage: [_vc targetLanguageIdentifier]];
}
}
}
@ -624,8 +696,93 @@
return filename;
}
// Print
/**
* This method is used to translate all of the strings in the file from one language
* into another. This is helpful when attempting to translate an application for use
* in different locales.
*/
- (IBAction) translate: (id)sender
{
NSArray *fileTypes = [NSArray arrayWithObjects: @"strings", nil];
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
int result;
[oPanel setAllowsMultipleSelection: NO];
[oPanel setCanChooseFiles: YES];
[oPanel setCanChooseDirectories: NO];
result = [oPanel runModalForDirectory: nil
file: nil
types: fileTypes];
if (result == NSOKButton)
{
GormDocument *doc = (GormDocument *)[self activeDocument];
NSMutableArray *allObjects = [doc _collectAllObjects];
NSString *filename = [oPanel filename];
NSEnumerator *en = nil;
id obj = nil;
NS_DURING
{
[doc importStringsFromFile: filename];
}
NS_HANDLER
{
NSString *message = [localException reason];
NSRunAlertPanel(_(@"Problem loading strings"),
message, nil, nil, nil);
}
NS_ENDHANDLER;
[doc touch]; // mark the document as modified...
// change to translated values.
en = [allObjects objectEnumerator];
while((obj = [en nextObject]) != nil)
{
if([obj isKindOfClass: [NSView class]])
{
[obj setNeedsDisplay: YES];
}
// redisplay/flush, if the object is a window.
if([obj isKindOfClass: [NSWindow class]])
{
NSWindow *w = (NSWindow *)obj;
[w setViewsNeedDisplay: YES];
[w disableFlushWindow];
[[w contentView] setNeedsDisplay: YES];
[[w contentView] displayIfNeeded];
[w enableFlushWindow];
[w flushWindowIfNeeded];
}
}
}
}
/**
* This method is used to export all strings in a document to a file for Language
* translation. This allows the user to see all of the strings which can be translated
* and allows the user to provide a translateion for each of them.
*/
- (IBAction) exportStrings: (id)sender
{
NSSavePanel *sp = [NSSavePanel savePanel];
int result;
[sp setRequiredFileType: @"strings"];
[sp setTitle: _(@"Save strings file as...")];
result = [sp runModalForDirectory: NSHomeDirectory()
file: nil];
if (result == NSOKButton)
{
NSString *filename = [sp filename];
GormDocument *doc = (GormDocument *)[self activeDocument];
[doc exportStringsToFile: filename];
}
}
// Print
- (IBAction) print: (id) sender
{
[[NSApp keyWindow] print: sender];

View file

@ -1,4 +1,26 @@
/* All rights reserved */
/* GormLanguageViewController.h
*
* Copyright (C) 2023 Free Software Foundation, Inc.
*
* Author: Gregory John Casamento <greg_casamento@yahoo.com>
* Date: 2023
*
* This file is part of GNUstep.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
*/
#ifndef GormLanguageViewController_H_INCLUDE
#define GormLanguageViewController_H_INCLUDE

View file

@ -1,5 +1,26 @@
/* All rights reserved */
/* GormLanguageViewController.m
*
* Copyright (C) 2023 Free Software Foundation, Inc.
*
* Author: Gregory John Casamento <greg_casamento@yahoo.com>
* Date: 2023
*
* This file is part of GNUstep.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
*/
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>

View file

@ -113,6 +113,11 @@ GormCore_HEADER_FILES = \
GormPrefController.h \
GormPrefs.h \
GormShelfPref.h \
GormXLIFFDocument.h \
GormXLIFFDocument.h \
GormXLIFFDocument.h \
GormXLIFFDocument.h \
GormXLIFFDocument.h \
GormCore_OBJC_FILES = \
GormAbstractDelegate.m \
@ -184,6 +189,7 @@ GormCore_OBJC_FILES = \
GormPluginsPref.m \
GormPrefController.m \
GormShelfPref.m \
GormXLIFFDocument.m \
GormCore_RESOURCE_FILES = \
Plugins/Gorm/Gorm.plugin \

View file

@ -96,6 +96,7 @@ FOUNDATION_EXPORT const unsigned char GormCoreVersionString[];
#import <GormCore/GormWindowTemplate.h>
#import <GormCore/GormWrapperBuilder.h>
#import <GormCore/GormWrapperLoader.h>
#import <GormCore/GormXLIFFDocument.h>
#import <GormCore/NSCell+GormAdditions.h>
#import <GormCore/NSColorWell+GormExtensions.h>
#import <GormCore/NSFontManager+GormExtensions.h>

View file

@ -57,7 +57,7 @@
}
@end
@interface GormDocument : NSDocument <IBDocuments, GSNibContainer, NSCoding>
@interface GormDocument : NSDocument <IBDocuments, GSNibContainer, NSCoding, NSXMLParserDelegate>
{
GormClassManager *classManager;
GormFilesOwner *filesOwner;
@ -213,36 +213,15 @@
/* Language translation */
/**
* Load a given file into the reciever using `filename`.
* Load a given file into the reciever using `filename'.
*/
- (void) importStringsFromFile: (NSString *)filename;
/**
* This method is used to translate all of the strings in the file from one language
* into another. This is helpful when attempting to translate an application for use
* in different locales.
*/
- (void) translate: (id)sender;
/**
* Export the strings from receiver to the file indicated by 'filename'.
*/
- (void) exportStringsToFile: (NSString *)filename;
/**
* Bring up a save panel to export the strings from the receiver.
*/
- (void) exportStrings: (id)sender;
/**
* Exports XLIFF file for CAT. This method starts the process and calls
* another method that recurses through the objects in the model and pulls
* any translatable elements.
*/
- (BOOL) exportXLIFFDocumentWithName: (NSString *)name
withSourceLanguage: (NSString *)slang
andTargetLanguage: (NSString *)tlang;
/* Managing classes */
/**
* Shared class manager

View file

@ -28,32 +28,33 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
*/
#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#include <InterfaceBuilder/InterfaceBuilder.h>
#import <InterfaceBuilder/InterfaceBuilder.h>
#include <GNUstepGUI/GSGormLoading.h>
#import <GNUstepGUI/GSGormLoading.h>
#include "GormPrivate.h"
#include "GormClassManager.h"
#include "GormCustomView.h"
#include "GormOutlineView.h"
#include "GormFunctions.h"
#include "GormFilePrefsManager.h"
#include "GormViewWindow.h"
#include "NSView+GormExtensions.h"
#include "GormSound.h"
#include "GormImage.h"
#include "GormResourceManager.h"
#include "GormClassEditor.h"
#include "GormSoundEditor.h"
#include "GormImageEditor.h"
#include "GormObjectEditor.h"
#include "GormWrapperBuilder.h"
#include "GormWrapperLoader.h"
#include "GormDocumentWindow.h"
#include "GormDocumentController.h"
#import "GormPrivate.h"
#import "GormClassManager.h"
#import "GormCustomView.h"
#import "GormOutlineView.h"
#import "GormFunctions.h"
#import "GormFilePrefsManager.h"
#import "GormViewWindow.h"
#import "NSView+GormExtensions.h"
#import "GormSound.h"
#import "GormImage.h"
#import "GormResourceManager.h"
#import "GormClassEditor.h"
#import "GormSoundEditor.h"
#import "GormImageEditor.h"
#import "GormObjectEditor.h"
#import "GormWrapperBuilder.h"
#import "GormWrapperLoader.h"
#import "GormDocumentWindow.h"
#import "GormDocumentController.h"
#import "GormXLIFFDocument.h"
@interface GormDisplayCell : NSButtonCell
@end
@ -3187,68 +3188,6 @@ static void _real_close(GormDocument *self,
}
}
/**
* This method is used to translate all of the strings in the file from one language
* into another. This is helpful when attempting to translate an application for use
* in different locales.
*/
- (void) translate: (id)sender
{
NSArray *fileTypes = [NSArray arrayWithObjects: @"strings", nil];
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
int result;
[oPanel setAllowsMultipleSelection: NO];
[oPanel setCanChooseFiles: YES];
[oPanel setCanChooseDirectories: NO];
result = [oPanel runModalForDirectory: nil
file: nil
types: fileTypes];
if (result == NSOKButton)
{
NSMutableArray *allObjects = [self _collectAllObjects];
NSString *filename = [oPanel filename];
NSEnumerator *en = nil;
id obj = nil;
NS_DURING
{
[self importStringsFromFile: filename];
}
NS_HANDLER
{
NSString *message = [localException reason];
NSRunAlertPanel(_(@"Problem loading strings"),
message, nil, nil, nil);
}
NS_ENDHANDLER;
[self touch]; // mark the document as modified...
// change to translated values.
en = [allObjects objectEnumerator];
while((obj = [en nextObject]) != nil)
{
if([obj isKindOfClass: [NSView class]])
{
[obj setNeedsDisplay: YES];
}
// redisplay/flush, if the object is a window.
if([obj isKindOfClass: [NSWindow class]])
{
NSWindow *w = (NSWindow *)obj;
[w setViewsNeedDisplay: YES];
[w disableFlushWindow];
[[w contentView] setNeedsDisplay: YES];
[[w contentView] displayIfNeeded];
[w enableFlushWindow];
[w flushWindowIfNeeded];
}
}
}
}
- (void) exportStringsToFile: (NSString *)filename
{
NSMutableArray *allObjects = [self _collectAllObjects];
@ -3296,240 +3235,6 @@ static void _real_close(GormDocument *self,
}
}
/**
* This method is used to export all strings in a document to a file for Language
* translation. This allows the user to see all of the strings which can be translated
* and allows the user to provide a translateion for each of them.
*/
- (void) exportStrings: (id)sender
{
NSSavePanel *sp = [NSSavePanel savePanel];
int result;
[sp setRequiredFileType: @"strings"];
[sp setTitle: _(@"Save strings file as...")];
result = [sp runModalForDirectory: NSHomeDirectory()
file: nil];
if (result == NSOKButton)
{
NSString *filename = [sp filename];
[self exportStringsToFile: filename];
}
}
- (void) _collectObjectsFromObject: (id)obj
withNode: (NSXMLElement *)node
{
NSString *name = [self nameForObject: obj];
if (name != nil)
{
NSString *className = NSStringFromClass([obj class]);
NSXMLElement *group = [NSXMLNode elementWithName: @"group"];
NSXMLNode *attr = [NSXMLNode attributeWithName: @"ib:object-id" stringValue: name];
[group addAttribute: attr];
if ([obj isKindOfClass: [GormObjectProxy class]] ||
[obj respondsToSelector: @selector(className)])
{
className = [obj className];
}
attr = [NSXMLNode attributeWithName: @"ib:class" stringValue: className];
[group addAttribute: attr];
[node addChild: group];
// If the object responds to "title" get that and add it to the XML
if ([obj respondsToSelector: @selector(title)])
{
NSString *title = [obj title];
if (title != nil)
{
NSXMLElement *transunit = [NSXMLNode elementWithName: @"trans-unit"];
NSString *objId = [NSString stringWithFormat: @"%@.title", name];
attr = [NSXMLNode attributeWithName: @"ib:key-path-category"
stringValue: @"string"];
[transunit addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"ib:key-path" stringValue: @"title"];
[transunit addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"id" stringValue: objId];
[transunit addAttribute: attr];
[group addChild: transunit];
NSXMLElement *source = [NSXMLNode elementWithName: @"source"];
[source setStringValue: title];
[transunit addChild: source];
}
}
// For each different class, recurse through the structure as needed.
if ([obj isKindOfClass: [NSMenu class]] ||
[obj isKindOfClass: [NSPopUpButton class]])
{
NSArray *items = [obj itemArray];
NSEnumerator *en = [items objectEnumerator];
id item = nil;
while ((item = [en nextObject]) != nil)
{
[self _collectObjectsFromObject: item
withNode: group];
}
}
else if ([obj isKindOfClass: [NSMenuItem class]])
{
NSMenu *sm = [obj submenu];
if (sm != nil)
{
[self _collectObjectsFromObject: sm
withNode: group];
}
}
else if ([obj isKindOfClass: [NSWindow class]])
{
[self _collectObjectsFromObject: [obj contentView]
withNode: group];
}
else if ([obj isKindOfClass: [NSView class]])
{
NSArray *subviews = [obj subviews];
NSEnumerator *en = [subviews objectEnumerator];
id v = nil;
while ((v = [en nextObject]) != nil)
{
[self _collectObjectsFromObject: v
withNode: group];
}
}
}
}
- (void) _buildXLIFFDocumentWithParentNode: (NSXMLElement *)parentNode
{
NSEnumerator *en = [topLevelObjects objectEnumerator];
id o = nil;
[self deactivateEditors];
while ((o = [en nextObject]) != nil)
{
[self _collectObjectsFromObject: o
withNode: parentNode];
}
[self reactivateEditors];
}
/**
* Exports XLIFF file for CAT. This method starts the process and calls
* another method that recurses through the objects in the model and pulls
* any translatable elements.
*/
- (BOOL) exportXLIFFDocumentWithName: (NSString *)name
withSourceLanguage: (NSString *)slang
andTargetLanguage: (NSString *)tlang
{
BOOL result = NO;
id delegate = [NSApp delegate];
if (slang != nil)
{
NSString *toolId = @"gnu.gnustep.Gorm";
NSString *toolName = @"Gorm";
NSString *toolVersion = [NSString stringWithFormat: @"%d", [GormFilePrefsManager currentVersion]];
if ([delegate isInTool])
{
toolName = @"gormtool";
toolId = @"gnu.gnustep.gormtool";
}
// Build root element...
NSXMLElement *rootElement = [NSXMLNode elementWithName: @"xliff"];
NSXMLNode *attr = [NSXMLNode attributeWithName: @"xmlns"
stringValue: @"urn:oasis:names:tc:xliff:document:1.2"];
[rootElement addAttribute: attr];
NSXMLNode *xsi_ns = [NSXMLNode namespaceWithName: @"xsi"
stringValue: @"http://www.w3.org/2001/XMLSchema-instance"];
[rootElement addNamespace: xsi_ns];
NSXMLNode *ib_ns = [NSXMLNode namespaceWithName: @"ib"
stringValue: @"com.apple.InterfaceBuilder3"];
[rootElement addNamespace: ib_ns];
attr = [NSXMLNode attributeWithName: @"version" stringValue: @"1.2"];
[rootElement addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"xsi:schemaLocation"
stringValue: @"urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd"];
[rootElement addAttribute: attr];
// Build header...
NSXMLElement *header = [NSXMLNode elementWithName: @"header"];
NSXMLElement *tool = [NSXMLNode elementWithName: @"tool"];
attr = [NSXMLNode attributeWithName: @"tool-id" stringValue: toolId];
[tool addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"tool-name" stringValue: toolName];
[tool addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"tool-version" stringValue: toolVersion];
[tool addAttribute: attr];
[header addChild: tool];
// Build "file" element...
NSString *filename = [[self fileName] lastPathComponent];
NSXMLElement *file = [NSXMLNode elementWithName: @"file"];
GormDocumentController *dc = [GormDocumentController sharedDocumentController];
NSString *type = [dc typeFromFileExtension: [filename pathExtension]];
attr = [NSXMLNode attributeWithName: @"original" stringValue: filename];
[file addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"datatype" stringValue: type]; // we will have the plugin return a datatype...
[file addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"tool-id" stringValue: toolId];
[file addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"source-language" stringValue: slang];
[file addAttribute: attr];
if (tlang != nil)
{
attr = [NSXMLNode attributeWithName: @"target-language" stringValue: tlang];
[file addAttribute: attr];
}
[rootElement addChild: file];
// Set up document...
NSXMLDocument *xliffDocument = [NSXMLNode documentWithRootElement: rootElement];
[file addChild: header];
NSXMLElement *body = [NSXMLNode elementWithName: @"body"];
NSXMLElement *group = [NSXMLNode elementWithName: @"group"];
attr = [NSXMLNode attributeWithName: @"ib_member-type" stringValue: @"objects"]; // not sure why generates ib_1 when using a colon
[group addAttribute: attr];
[body addChild: group];
// add body to document...
[file addChild: body];
// Recursively build the XLIFF document from the GormDocument...
[self _buildXLIFFDocumentWithParentNode: group];
NSData *data = [xliffDocument XMLDataWithOptions: NSXMLNodePrettyPrint | NSXMLNodeCompactEmptyElement ];
NSString *xmlString = [[NSString alloc] initWithBytes: [data bytes] length: [data length] encoding: NSUTF8StringEncoding];
NSString *fixedString = [xmlString stringByReplacingOccurrencesOfString: @"ib_member-type"
withString: @"ib:member-type"];
// "fixedString" corrects a rather confusing problem where adding the
// ib:member-type attribute, for some reason causes the NSXMLNode to
// create a repeated declaration of the "ib" namespace. I don't understand
// why this is happening, but this fixes it in the output for now.
AUTORELEASE(xmlString);
result = [fixedString writeToFile: name atomically: YES];
}
else
{
NSLog(@"Source language not specified");
}
return result;
}
/**
* Arrange views in front or in back of one another.
*/

View file

@ -0,0 +1,75 @@
/* GormXLIFFDocument.h
*
* Copyright (C) 2023 Free Software Foundation, Inc.
*
* Author: Gregory John Casamento <greg_casamento@yahoo.com>
* Date: 2023
*
* This file is part of GNUstep.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
*/
#ifndef GormXLIFFDocument_H_INCLUDE
#define GormXLIFFDocument_H_INCLUDE
#import <Foundation/NSObject.h>
@class NSMutableDictionary;
@class NSString;
@class NSXMLDocument;
@class GormDocument;
@interface GormXLIFFDocument : NSObject
{
GormDocument *_gormDocument;
NSString *_objectId;
NSString *_targetString;
NSString *_sourceString;
NSMutableDictionary *_translationDictionary;
BOOL _source;
BOOL _target;
}
/**
* Returns an autoreleast GormXLIFFDocument object;
*/
+ (instancetype) xliffWithGormDocument: (GormDocument *)doc;
/**
* Initialize with GormDocument object to parse the XML from or into.
*/
- (instancetype) initWithGormDocument: (GormDocument *)doc;
/**
* Exports XLIFF file for CAT. This method starts the process and calls
* another method that recurses through the objects in the model and pulls
* any translatable elements.
*/
- (BOOL) exportXLIFFDocumentWithName: (NSString *)name
withSourceLanguage: (NSString *)slang
andTargetLanguage: (NSString *)tlang;
/**
* Import XLIFF Document withthe name filename
*/
- (BOOL) importXLIFFDocumentWithName: (NSString *)filename;
@end
#endif // GormXLIFFDocument_H_INCLUDE

View file

@ -0,0 +1,427 @@
/* GormXLIFFDocument.m
*
* Copyright (C) 2023 Free Software Foundation, Inc.
*
* Author: Gregory John Casamento <greg_casamento@yahoo.com>
* Date: 2023
*
* This file is part of GNUstep.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
*/
#import <Foundation/NSArray.h>
#import <Foundation/NSData.h>
#import <Foundation/NSString.h>
#import <Foundation/NSXMLDocument.h>
#import <Foundation/NSXMLNode.h>
#import <Foundation/NSXMLElement.h>
#import <Foundation/NSXMLParser.h>
#import <AppKit/NSMenu.h>
#import <AppKit/NSPopUpButton.h>
#import <AppKit/NSView.h>
#import "GormDocument.h"
#import "GormDocumentController.h"
#import "GormFilePrefsManager.h"
#import "GormProtocol.h"
#import "GormPrivate.h"
#import "GormXLIFFDocument.h"
@implementation GormXLIFFDocument
/**
* Returns an autoreleast GormXLIFFDocument object;
*/
+ (instancetype) xliffWithGormDocument: (GormDocument *)doc
{
return AUTORELEASE([[self alloc] initWithGormDocument: doc]);
}
/**
* Initialize with GormDocument object to parse the XML from or into.
*/
- (instancetype) initWithGormDocument: (GormDocument *)doc
{
self = [super init];
if (self != nil)
{
ASSIGN(_gormDocument, doc);
_objectId = nil;
_source = NO;
_target = NO;
_sourceString = nil;
_targetString = nil;
_translationDictionary = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void) dealloc
{
DESTROY(_gormDocument);
_objectId = nil;
_sourceString = nil;
_targetString = nil;
DESTROY(_translationDictionary);
[super dealloc];
}
- (void) _collectObjectsFromObject: (id)obj
withNode: (NSXMLElement *)node
{
NSString *name = [_gormDocument nameForObject: obj];
if (name != nil)
{
NSString *className = NSStringFromClass([obj class]);
NSXMLElement *group = [NSXMLNode elementWithName: @"group"];
NSXMLNode *attr = [NSXMLNode attributeWithName: @"ib:object-id" stringValue: name];
[group addAttribute: attr];
if ([obj isKindOfClass: [GormObjectProxy class]] ||
[obj respondsToSelector: @selector(className)])
{
className = [obj className];
}
attr = [NSXMLNode attributeWithName: @"ib:class" stringValue: className];
[group addAttribute: attr];
[node addChild: group];
// If the object responds to "title" get that and add it to the XML
if ([obj respondsToSelector: @selector(title)])
{
NSString *title = [obj title];
if (title != nil)
{
NSXMLElement *transunit = [NSXMLNode elementWithName: @"trans-unit"];
NSString *objId = [NSString stringWithFormat: @"%@.title", name];
attr = [NSXMLNode attributeWithName: @"ib:key-path-category"
stringValue: @"string"];
[transunit addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"ib:key-path" stringValue: @"title"];
[transunit addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"id" stringValue: objId];
[transunit addAttribute: attr];
[group addChild: transunit];
NSXMLElement *source = [NSXMLNode elementWithName: @"source"];
[source setStringValue: title];
[transunit addChild: source];
}
}
// For each different class, recurse through the structure as needed.
if ([obj isKindOfClass: [NSMenu class]] ||
[obj isKindOfClass: [NSPopUpButton class]])
{
NSArray *items = [obj itemArray];
NSEnumerator *en = [items objectEnumerator];
id item = nil;
while ((item = [en nextObject]) != nil)
{
[self _collectObjectsFromObject: item
withNode: group];
}
}
else if ([obj isKindOfClass: [NSMenuItem class]])
{
NSMenu *sm = [obj submenu];
if (sm != nil)
{
[self _collectObjectsFromObject: sm
withNode: group];
}
}
else if ([obj isKindOfClass: [NSWindow class]])
{
[self _collectObjectsFromObject: [obj contentView]
withNode: group];
}
else if ([obj isKindOfClass: [NSView class]])
{
NSArray *subviews = [obj subviews];
NSEnumerator *en = [subviews objectEnumerator];
id v = nil;
while ((v = [en nextObject]) != nil)
{
[self _collectObjectsFromObject: v
withNode: group];
}
}
}
}
- (void) _buildXLIFFDocumentWithParentNode: (NSXMLElement *)parentNode
{
NSEnumerator *en = [[_gormDocument topLevelObjects] objectEnumerator];
id o = nil;
[_gormDocument deactivateEditors];
while ((o = [en nextObject]) != nil)
{
[self _collectObjectsFromObject: o
withNode: parentNode];
}
[_gormDocument reactivateEditors];
}
/**
* Exports XLIFF file for CAT. This method starts the process and calls
* another method that recurses through the objects in the model and pulls
* any translatable elements.
*/
- (BOOL) exportXLIFFDocumentWithName: (NSString *)name
withSourceLanguage: (NSString *)slang
andTargetLanguage: (NSString *)tlang
{
BOOL result = NO;
id delegate = [NSApp delegate];
if (slang != nil)
{
NSString *toolId = @"gnu.gnustep.Gorm";
NSString *toolName = @"Gorm";
NSString *toolVersion = [NSString stringWithFormat: @"%d", [GormFilePrefsManager currentVersion]];
if ([delegate isInTool])
{
toolName = @"gormtool";
toolId = @"gnu.gnustep.gormtool";
}
// Build root element...
NSXMLElement *rootElement = [NSXMLNode elementWithName: @"xliff"];
NSXMLNode *attr = [NSXMLNode attributeWithName: @"xmlns"
stringValue: @"urn:oasis:names:tc:xliff:document:1.2"];
[rootElement addAttribute: attr];
NSXMLNode *xsi_ns = [NSXMLNode namespaceWithName: @"xsi"
stringValue: @"http://www.w3.org/2001/XMLSchema-instance"];
[rootElement addNamespace: xsi_ns];
NSXMLNode *ib_ns = [NSXMLNode namespaceWithName: @"ib"
stringValue: @"com.apple.InterfaceBuilder3"];
[rootElement addNamespace: ib_ns];
attr = [NSXMLNode attributeWithName: @"version" stringValue: @"1.2"];
[rootElement addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"xsi:schemaLocation"
stringValue: @"urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd"];
[rootElement addAttribute: attr];
// Build header...
NSXMLElement *header = [NSXMLNode elementWithName: @"header"];
NSXMLElement *tool = [NSXMLNode elementWithName: @"tool"];
attr = [NSXMLNode attributeWithName: @"tool-id" stringValue: toolId];
[tool addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"tool-name" stringValue: toolName];
[tool addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"tool-version" stringValue: toolVersion];
[tool addAttribute: attr];
[header addChild: tool];
// Build "file" element...
NSString *filename = [[_gormDocument fileName] lastPathComponent];
NSXMLElement *file = [NSXMLNode elementWithName: @"file"];
GormDocumentController *dc = [GormDocumentController sharedDocumentController];
NSString *type = [dc typeFromFileExtension: [filename pathExtension]];
attr = [NSXMLNode attributeWithName: @"original" stringValue: filename];
[file addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"datatype" stringValue: type]; // we will have the plugin return a datatype...
[file addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"tool-id" stringValue: toolId];
[file addAttribute: attr];
attr = [NSXMLNode attributeWithName: @"source-language" stringValue: slang];
[file addAttribute: attr];
if (tlang != nil)
{
attr = [NSXMLNode attributeWithName: @"target-language" stringValue: tlang];
[file addAttribute: attr];
}
[rootElement addChild: file];
// Set up document...
NSXMLDocument *xliffDocument = [NSXMLNode documentWithRootElement: rootElement];
[file addChild: header];
NSXMLElement *body = [NSXMLNode elementWithName: @"body"];
NSXMLElement *group = [NSXMLNode elementWithName: @"group"];
attr = [NSXMLNode attributeWithName: @"ib_member-type" stringValue: @"objects"]; // not sure why generates ib_1 when using a colon
[group addAttribute: attr];
[body addChild: group];
// add body to document...
[file addChild: body];
// Recursively build the XLIFF document from the GormDocument...
[self _buildXLIFFDocumentWithParentNode: group];
NSData *data = [xliffDocument XMLDataWithOptions: NSXMLNodePrettyPrint | NSXMLNodeCompactEmptyElement ];
NSString *xmlString = [[NSString alloc] initWithBytes: [data bytes] length: [data length] encoding: NSUTF8StringEncoding];
NSString *fixedString = [xmlString stringByReplacingOccurrencesOfString: @"ib_member-type"
withString: @"ib:member-type"];
// "fixedString" corrects a rather confusing problem where adding the
// ib:member-type attribute, for some reason causes the NSXMLNode to
// create a repeated declaration of the "ib" namespace. I don't understand
// why this is happening, but this fixes it in the output for now.
AUTORELEASE(xmlString);
result = [fixedString writeToFile: name atomically: YES];
}
else
{
NSLog(@"Source language not specified");
}
return result;
}
- (void) parserDidStartDocument: (NSXMLParser *)parser
{
NSDebugLog(@"start of document");
}
- (void) parser: (NSXMLParser *)parser
didStartElement: (NSString *)elementName
namespaceURI: (NSString *)namespaceURI
qualifiedName: (NSString *)qName
attributes: (NSDictionary *)attrs
{
NSDebugLog(@"start element %@", elementName);
if ([elementName isEqualToString: @"trans-unit"])
{
NSString *objId = [attrs objectForKey: @"id"];
_objectId = objId;
}
else if ([elementName isEqualToString: @"source"])
{
_source = YES;
}
else if ([elementName isEqualToString: @"target"])
{
_target = YES;
}
}
- (void) parser: (NSXMLParser *)parser
foundCharacters: (NSString *)string
{
if (_objectId != nil)
{
if (_source)
{
NSDebugLog(@"Found source string %@, current id = %@", string, _objectId);
}
if (_target)
{
[_translationDictionary setObject: string forKey: _objectId];
NSDebugLog(@"Found target string %@, current id = %@", string, _objectId);
}
}
}
- (void) parser: (NSXMLParser *)parser
didEndElement: (NSString *)elementName
namespaceURI: (NSString *)namespaceURI
qualifiedName: (NSString *)qName
{
NSDebugLog(@"end element %@", elementName);
if ([elementName isEqualToString: @"trans-unit"])
{
_objectId = nil;
}
else if ([elementName isEqualToString: @"source"])
{
_source = NO;
}
else if ([elementName isEqualToString: @"target"])
{
_target = NO;
}
}
- (void) parserDidEndDocument: (NSXMLParser *)parser
{
NSDebugLog(@"end of document");
}
/**
* Import XLIFF Document withthe name filename
*/
- (BOOL) importXLIFFDocumentWithName: (NSString *)filename
{
NSData *xmlData = [NSData dataWithContentsOfFile: filename];
NSXMLParser *xmlParser =
[[NSXMLParser alloc] initWithData: xmlData];
BOOL result = NO;
[xmlParser setDelegate: self];
[xmlParser parse];
if ([_translationDictionary count] > 0)
{
NSEnumerator *en = [_translationDictionary keyEnumerator];
NSString *oid = nil;
while ((oid = [en nextObject]) != nil)
{
NSString *target = [_translationDictionary objectForKey: oid];
NSArray *c = [oid componentsSeparatedByString: @"."];
if ([c count] == 2)
{
NSString *nm = [c objectAtIndex: 0];
NSString *kp = [c objectAtIndex: 1];
id o = nil;
NSString *capName = [kp capitalizedString];
NSString *selName = [NSString stringWithFormat: @"set%@:", capName];
SEL _sel = NSSelectorFromString(selName);
NSDebugLog(@"computed selector name = %@", selName);
// Pull the object that we want to translate and apply the target translation...
o = [_gormDocument objectForName: nm];
if ([o respondsToSelector: _sel])
{
NSDebugLog(@"performing %@, with object: %@", selName, target);
[o performSelector: _sel withObject: target];
}
}
NSDebugLog(@"target = %@, oid = %@", target, oid);
}
result = YES;
}
else
{
NSLog(@"Document contains no target translation elements");
}
RELEASE(xmlParser);
return result;
}
@end

View file

@ -352,22 +352,16 @@
{
NSString *xliffDocumentName = [opt value];
BOOL result = NO;
GormXLIFFDocument *xd = [GormXLIFFDocument xliffWithGormDocument: doc];
if (slang == nil)
{
NSLog(@"Please specify a source language");
}
/*
if (tlang == nil)
{
NSLog(@"Please specify a target language");
}
*/
result = [doc exportXLIFFDocumentWithName: xliffDocumentName
withSourceLanguage: slang
andTargetLanguage: tlang];
result = [xd exportXLIFFDocumentWithName: xliffDocumentName
withSourceLanguage: slang
andTargetLanguage: tlang];
if (result == NO)
{
NSLog(@"File not generated");