Move trnslation to GormXLIFFDocument for XLIFF format

This commit is contained in:
Gregory John Casamento 2023-07-17 15:20:59 -04:00
parent c093d9c281
commit b164662d3d
8 changed files with 88 additions and 311 deletions

View file

@ -600,7 +600,8 @@
NS_DURING
{
[doc importXLIFFDocumentWithName: filename];
GormXLIFFDocument *xd = [GormXLIFFDocument xliffWithGormDocument: doc];
[xd importXLIFFDocumentWithName: filename];
}
NS_HANDLER
{
@ -666,9 +667,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]];
}
}
}

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

@ -222,20 +222,6 @@
*/
- (void) exportStringsToFile: (NSString *)filename;
/**
* Import XLIFF document into the reciever using filename.
*/
- (BOOL) importXLIFFDocumentWithName: (NSString *)filename;
/**
* 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
@ -3234,263 +3235,6 @@ static void _real_close(GormDocument *self,
}
}
- (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;
}
- (void) parserDidStartDocument: (NSXMLParser *)parser
{
NSLog(@"start of document");
}
- (void) parser: (NSXMLParser *)parser
didStartElement: (NSString *)elementName
namespaceURI: (NSString *)namespaceURI
qualifiedName: (NSString *)qName
attributes: (NSDictionary *)attrs
{
NSLog(@"start element %@", elementName);
}
- (void) parser: (NSXMLParser *)parser
didEndElement: (NSString *)elementName
namespaceURI: (NSString *)namespaceURI
qualifiedName: (NSString *)qName
{
NSLog(@"end element %@", elementName);
}
- (void) parserDidEndDocument: (NSXMLParser *)parser
{
NSLog(@"end of document");
}
/**
* Import XLIFF Document withthe name filename
*/
- (BOOL) importXLIFFDocumentWithName: (NSString *)filename
{
NSData *xmlData = [NSData dataWithContentsOfFile: filename];
NSXMLParser *xmlParser =
[[NSXMLParser alloc] initWithData: xmlData];
[xmlParser setDelegate: self];
[xmlParser parse];
RELEASE(xmlParser);
return NO;
}
/**
* Arrange views in front or in back of one another.
*/

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");