Search in sources :

Example 1 with ConversionResource

use of net.heartsome.cat.convert.ui.utils.ConversionResource in project translationstudio8 by heartsome.

the class PreviewTranslationHandler method getConverterViewModel.

/**
	 * 得到 ConverterViewModel 对象
	 * @param file
	 *            需要预览翻译的文件
	 * @return
	 * @throws ExecutionException
	 *             ;
	 */
private ConverterViewModel getConverterViewModel(IFile file) throws ExecutionException {
    Object adapter = Platform.getAdapterManager().getAdapter(file, IConversionItem.class);
    if (adapter instanceof IConversionItem) {
        IConversionItem item = (IConversionItem) adapter;
        ConverterViewModel converterViewModel = new ConverterViewModel(Activator.getContext(), // 逆向转换
        Converter.DIRECTION_REVERSE);
        converterViewModel.setConversionItem(item);
        try {
            ConversionResource resource = new ConversionResource(Converter.DIRECTION_REVERSE, item);
            String xliffpath = resource.getXliffPath();
            String targetPath = resource.getPreviewPath();
            ConversionConfigBean configBean = converterViewModel.getConfigBean();
            configBean.setSource(xliffpath);
            configBean.setTarget(targetPath);
            configBean.setTargetEncoding("UTF-8");
            // 设为预览翻译模式
            configBean.setPreviewMode(true);
            final IStatus status = converterViewModel.validateXliffFile(ConverterUtil.toLocalPath(xliffpath), new XLFHandler(), // 注:验证的过程中,会为文件类型和骨架文件的路径赋值。
            null);
            if (status != null && status.isOK()) {
                return converterViewModel;
            } else {
                Display.getDefault().syncExec(new Runnable() {

                    public void run() {
                        MessageDialog.openInformation(shell, Messages.getString("handler.PreviewTranslationHandler.msgTitle"), status.getMessage());
                    }
                });
                throw new ExecutionException(status.getMessage(), status.getException());
            }
        } catch (CoreException e) {
            throw new ExecutionException(e.getMessage(), e);
        }
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) ConversionConfigBean(net.heartsome.cat.convert.ui.model.ConversionConfigBean) ConverterViewModel(net.heartsome.cat.convert.ui.model.ConverterViewModel) ConversionResource(net.heartsome.cat.convert.ui.utils.ConversionResource) ExecutionException(org.eclipse.core.commands.ExecutionException) IConversionItem(net.heartsome.cat.convert.ui.model.IConversionItem) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 2 with ConversionResource

use of net.heartsome.cat.convert.ui.utils.ConversionResource in project translationstudio8 by heartsome.

the class ConversionWizardPage method loadFiles.

/**
	 * 加载文件数据。
	 */
private void loadFiles() {
    for (int i = 0; i < conversionConfigBeans.size(); i++) {
        ConversionConfigBean bean = conversionConfigBeans.get(i);
        bean.setIndex((i + 1) + "");
        String source = bean.getSource();
        String sourceLocalPath = ConverterUtil.toLocalPath(source);
        // 自动识别文件类型
        String format = FileFormatUtils.detectFormat(sourceLocalPath);
        if (format == null) {
            //$NON-NLS-1$
            format = "";
        }
        // 自动分析源文件编码
        String srcEncValue = EncodingResolver.getEncoding(sourceLocalPath, format);
        if (srcEncValue == null) {
            //$NON-NLS-1$
            srcEncValue = "";
        }
        bean.setEmbedSkl(FileFormatUtils.canEmbedSkl(format));
        // XLIFF 文件路径
        //$NON-NLS-1$
        String xliff = "";
        // 骨架文件路径
        //$NON-NLS-1$
        String skeleton = "";
        //XLiff 文件夹
        String xliffDir = "";
        try {
            ConversionResource resource = new ConversionResource(Converter.DIRECTION_POSITIVE, sourceLocalPath);
            xliff = resource.getXliffPath();
            skeleton = resource.getSkeletonPath();
            xliffDir = resource.getXliffDir();
        } catch (CoreException e) {
            LOGGER.error("", e);
        }
        if (!"".equals(format)) {
            //$NON-NLS-1$
            String name = getSelectedFormat(format);
            if (name != null && !"".equals(name)) {
                //$NON-NLS-1$
                // 添加类型
                converterViewModels.get(i).setSelectedType(name);
            }
        }
        bean.setFileType(format);
        bean.setSrcEncoding(srcEncValue);
        bean.setXliffDir(xliffDir);
        bean.setTarget(xliff);
        bean.setSkeleton(ConverterUtil.toLocalPath(skeleton));
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ConversionConfigBean(net.heartsome.cat.convert.ui.model.ConversionConfigBean) ConversionResource(net.heartsome.cat.convert.ui.utils.ConversionResource)

Example 3 with ConversionResource

use of net.heartsome.cat.convert.ui.utils.ConversionResource in project translationstudio8 by heartsome.

the class ReverseConversionWizardPage method loadFiles.

/**
	 * 加载文件数据。
	 */
private void loadFiles() {
    ArrayList<String> xliffs = new ArrayList<String>(conversionConfigBeans.size());
    for (int i = 0; i < conversionConfigBeans.size(); i++) {
        String xliff = conversionConfigBeans.get(i).getSource();
        xliffs.add(ResourceUtils.toWorkspacePath(xliff));
    }
    for (int i = 0; i < conversionConfigBeans.size(); i++) {
        ConversionConfigBean bean = conversionConfigBeans.get(i);
        bean.setIndex((i + 1) + "");
        // 逆转换时,source 其实是 XLIFF 文件。
        String xliff = bean.getSource();
        String local = ConverterUtil.toLocalPath(xliff);
        // 目标文件
        String target;
        try {
            ConversionResource resource = new ConversionResource(Converter.DIRECTION_REVERSE, local);
            target = resource.getTargetPath();
        } catch (CoreException e) {
            e.printStackTrace();
            //$NON-NLS-1$
            target = "";
        }
        // 目标编码
        String tgtEncValue = getTgtEncoding(local);
        if (tgtEncValue == null) {
            //$NON-NLS-1$
            tgtEncValue = "";
        }
        bean.setSource(xliff);
        bean.setTargetEncoding(tgtEncValue);
        bean.setTarget(target);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ConversionConfigBean(net.heartsome.cat.convert.ui.model.ConversionConfigBean) ArrayList(java.util.ArrayList) ConversionResource(net.heartsome.cat.convert.ui.utils.ConversionResource) Point(org.eclipse.swt.graphics.Point)

Aggregations

ConversionConfigBean (net.heartsome.cat.convert.ui.model.ConversionConfigBean)3 ConversionResource (net.heartsome.cat.convert.ui.utils.ConversionResource)3 CoreException (org.eclipse.core.runtime.CoreException)3 ArrayList (java.util.ArrayList)1 ConverterViewModel (net.heartsome.cat.convert.ui.model.ConverterViewModel)1 IConversionItem (net.heartsome.cat.convert.ui.model.IConversionItem)1 XLFHandler (net.heartsome.cat.ts.core.file.XLFHandler)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IStatus (org.eclipse.core.runtime.IStatus)1 Point (org.eclipse.swt.graphics.Point)1