Search in sources :

Example 1 with Language

use of net.heartsome.cat.common.locale.Language in project translationstudio8 by heartsome.

the class LanguageConfiger method getAllLanguage.

/**
	 * 返回所有语言列表,key:code,value:<code>Language</code>
	 */
public Map<String, Language> getAllLanguage() {
    Map<String, Language> result = new HashMap<String, Language>();
    AutoPilot tempAp = new AutoPilot(vu.getVTDNav());
    String codeAttr = "code";
    String bidiAttr = "bidi";
    String imgAttr = "image";
    String bidiYes = "Yes";
    try {
        tempAp.selectXPath("/languages/lang");
        while (tempAp.evalXPath() != -1) {
            Map<String, String> attrs = vu.getCurrentElementAttributs();
            String code = attrs.get(codeAttr);
            String bidi = attrs.get(bidiAttr);
            String img = attrs.get(imgAttr);
            String langName = vu.getElementPureText();
            boolean isBidi = false;
            if (code != null && langName != null) {
                if (bidi != null && bidi.equals(bidiYes)) {
                    isBidi = true;
                }
                result.put(code, new Language(code, langName, img == null ? "" : img, isBidi));
            }
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
    } catch (NavException e) {
        LOGGER.error("", e);
    }
    return result;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) Language(net.heartsome.cat.common.locale.Language) HashMap(java.util.HashMap) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException)

Example 2 with Language

use of net.heartsome.cat.common.locale.Language in project translationstudio8 by heartsome.

the class LanguageConfiger method getLanguageByCode.

/**
	 * 返回Code对应的Language对象,如果在配置中找到则返回null
	 * @param code
	 * @return ;
	 */
public Language getLanguageByCode(String code) {
    AutoPilot tempAp = new AutoPilot(vu.getVTDNav());
    String bidiAttr = "bidi";
    String imgAttr = "image";
    String bidiYes = "Yes";
    try {
        tempAp.selectXPath("/languages/lang[@code='" + code + "']");
        if (tempAp.evalXPath() != -1) {
            Map<String, String> attrs = vu.getCurrentElementAttributs();
            String bidi = attrs.get(bidiAttr);
            String img = attrs.get(imgAttr);
            String langName = vu.getElementPureText();
            boolean isBidi = false;
            if (code != null && langName != null) {
                if (bidi != null && bidi.equals(bidiYes)) {
                    isBidi = true;
                }
                return new Language(code, langName, img == null ? "" : img, isBidi);
            }
        }
    } catch (XPathParseException e) {
        LOGGER.error("", e);
    } catch (XPathEvalException e) {
        LOGGER.error("", e);
    } catch (NavException e) {
        LOGGER.error("", e);
    }
    return null;
}
Also used : XPathParseException(com.ximpleware.XPathParseException) Language(net.heartsome.cat.common.locale.Language) AutoPilot(com.ximpleware.AutoPilot) XPathEvalException(com.ximpleware.XPathEvalException) NavException(com.ximpleware.NavException)

Example 3 with Language

use of net.heartsome.cat.common.locale.Language in project translationstudio8 by heartsome.

the class TermBaseSearchHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    if (!isEnabled()) {
        return null;
    }
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    if (editor instanceof IXliffEditor) {
        IXliffEditor xliffEditor = (IXliffEditor) editor;
        IProject project = ((FileEditorInput) editor.getEditorInput()).getFile().getProject();
        ProjectConfiger projectConfig = ProjectConfigerFactory.getProjectConfiger(project);
        List<DatabaseModelBean> lstDatabase = projectConfig.getTermBaseDbs(false);
        if (lstDatabase == null || lstDatabase.size() == 0) {
            MessageDialog.openInformation(HandlerUtil.getActiveShell(event), Messages.getString("handler.TermBaseSearchHandler.msgTitle"), Messages.getString("handler.TermBaseSearchHandler.msg"));
            return null;
        }
        String selectText = xliffEditor.getSelectPureText();
        if ((selectText == null || selectText.equals("")) && xliffEditor.getSelectedRowIds().size() == 1) {
            selectText = xliffEditor.getXLFHandler().getSrcPureText(xliffEditor.getSelectedRowIds().get(0));
            selectText = resetCeanString(selectText);
        }
        String srcLang = xliffEditor.getSrcColumnName();
        String tgtLang = xliffEditor.getTgtColumnName();
        TermBaseSearchDialog dialog = new TermBaseSearchDialog(editor.getSite().getShell(), project, srcLang, tgtLang, selectText.trim());
        Language srcLangL = LocaleService.getLanguageConfiger().getLanguageByCode(srcLang);
        Language tgtLangL = LocaleService.getLanguageConfiger().getLanguageByCode(tgtLang);
        dialog.open();
        if (srcLangL.isBidi() || tgtLangL.isBidi()) {
            dialog.getShell().setOrientation(SWT.RIGHT_TO_LEFT);
        }
        if (selectText != null && !selectText.trim().equals("")) {
            dialog.search();
        }
    }
    return null;
}
Also used : Language(net.heartsome.cat.common.locale.Language) ProjectConfiger(net.heartsome.cat.ts.core.file.ProjectConfiger) DatabaseModelBean(net.heartsome.cat.common.bean.DatabaseModelBean) TermBaseSearchDialog(net.heartsome.cat.database.ui.tb.dialog.TermBaseSearchDialog) IEditorPart(org.eclipse.ui.IEditorPart) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) IProject(org.eclipse.core.resources.IProject)

Example 4 with Language

use of net.heartsome.cat.common.locale.Language in project translationstudio8 by heartsome.

the class ProjectSettingTBPage method checkDbHashMatch.

/**
	 * 需要调用Database模块 检查当前项目在库中是否有语言对的匹配
	 * @param dbModel
	 *            数据库信息;
	 */
public void checkDbHashMatch(DatabaseModelBean dbModel) {
    Language srcLang = super.projectInfoBean.getSourceLang();
    DBOperator dbOp = DatabaseService.getDBOperator(dbModel.toDbMetaData());
    try {
        if (dbOp != null) {
            dbOp.start();
            dbModel.setHasMatch(dbOp.checkHasMatchs(srcLang.getCode(), "B"));
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        e.printStackTrace();
        dbModel.setHasMatch(false);
    } finally {
        if (dbOp != null) {
            try {
                if (dbOp != null) {
                    dbOp.end();
                }
            } catch (SQLException e) {
                LOGGER.error("", e);
            }
        }
    }
}
Also used : Language(net.heartsome.cat.common.locale.Language) SQLException(java.sql.SQLException) DBOperator(net.heartsome.cat.database.DBOperator) SQLException(java.sql.SQLException)

Example 5 with Language

use of net.heartsome.cat.common.locale.Language in project translationstudio8 by heartsome.

the class Txt2TbxConverter method doConvert.

/**
	 * (non-Javadoc)
	 * @see net.heartsome.cat.document.converter.AbstractConverter#doConvert(java.lang.String,
	 *      org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public void doConvert(String targetFile, IProgressMonitor monitor) throws Exception {
    try {
        monitor.beginTask("", 10);
        monitor.worked(2);
        out = new FileOutputStream(new File(targetFile));
        File f = new File(txtFile);
        String encoding = FileEncodingDetector.detectFileEncoding(f);
        TxtRowReader reader = new TxtRowReader(f, encoding);
        int rowsNum = reader.getLineNumber();
        if (rowsNum < 2) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        }
        // read header
        List<String[]> h = reader.read(1);
        if (h == null || h.size() == 0) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        }
        String[] hv = h.get(0);
        if (hv == null || hv.length < 2) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        }
        Map<String, Language> defaultLanguage = LocaleService.getDefaultLanguage();
        List<String> _temp = new ArrayList<String>();
        for (String s : hv) {
            s = LanguageUtils.convertLangCode(s);
            if (_temp.contains(s)) {
                throw new Exception(Messages.getString("converter.common.vaild.duplicatelangcode.error"));
            }
            if (defaultLanguage.get(s) == null) {
                throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
            }
            _temp.add(s);
        }
        if (_temp.size() < 2) {
            throw new Exception(Messages.getString("converter.common.vaild.langcode.error"));
        }
        header = _temp.toArray(new String[] {});
        // generate header
        String s = generateTbxHeader(header[0]);
        writeString(s);
        // generate body
        int readSize = 10;
        List<String[]> rs = null;
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 8);
        subMonitor.beginTask("", rowsNum / readSize == 0 ? 1 : rowsNum / readSize);
        while ((rs = reader.read(readSize)) != null) {
            for (String[] r : rs) {
                if (r == null || r.length < 2) {
                    continue;
                }
                int loopSize = r.length < header.length ? r.length : header.length;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < loopSize; i++) {
                    String value = r[i];
                    String lang = header[i];
                    sb.append("<langSet id=\"_" + (System.currentTimeMillis() + 1) + "\" xml:lang=\"" + lang + "\">\n");
                    sb.append("<tig>\n");
                    sb.append("<term>" + value + "</term>\n");
                    sb.append("</tig>\n");
                    sb.append("</langSet>\n");
                }
                if (sb.length() != 0) {
                    writeString("<termEntry id=\"_" + System.currentTimeMillis() + "\">\n");
                    writeString(sb.toString());
                    writeString("</termEntry>\n");
                }
                subMonitor.worked(1);
            }
            if (subMonitor.isCanceled()) {
                throw new OperationCanceledException();
            }
        }
        // generate end
        s = generateTbxEnd();
        if (s != null && s.length() != 0) {
            writeString(s);
        }
        subMonitor.done();
    } finally {
        out.close();
        monitor.done();
    }
}
Also used : OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) TxtRowReader(net.heartsome.cat.document.txt.TxtRowReader) IOException(java.io.IOException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) Language(net.heartsome.cat.common.locale.Language) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

Language (net.heartsome.cat.common.locale.Language)31 ArrayList (java.util.ArrayList)10 AutoPilot (com.ximpleware.AutoPilot)6 NavException (com.ximpleware.NavException)5 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)5 XPathEvalException (com.ximpleware.XPathEvalException)4 XPathParseException (com.ximpleware.XPathParseException)4 File (java.io.File)4 IOException (java.io.IOException)4 ProjectConfiger (net.heartsome.cat.ts.core.file.ProjectConfiger)4 CoreException (org.eclipse.core.runtime.CoreException)4 GridData (org.eclipse.swt.layout.GridData)4 VTDGen (com.ximpleware.VTDGen)3 VTDNav (com.ximpleware.VTDNav)3 List (java.util.List)3 LanguageLabelProvider (net.heartsome.cat.ts.ui.composite.LanguageLabelProvider)3 IXliffEditor (net.heartsome.cat.ts.ui.editors.IXliffEditor)3 VTDUtils (net.heartsome.xml.vtdimpl.VTDUtils)3 XMLModifier (com.ximpleware.XMLModifier)2 FileOutputStream (java.io.FileOutputStream)2