properties语言包字数统计

今天需要做个预算,需要统计语言包中要翻译的中文文字数,文字有汉字和Unicode码,想了想还是动动手写点代码来统计。

先取一下目录下所有的文件。扔到Excel里。

这里写图片描述

然后运行下列代码:
注意修改final HSSFSheet sheet = wb.getSheetAt(4); 中的sheet页:4取的是第5个sheet。
System.out.println(a);会输出文件的字数。复制到sheet中方便统计。

代码如下:
package com.hoperun.eesite.modules.listener;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.hoperun.eesite.common.utils.StringUtils;

public class getContentSize {
public static void main(String[]args) throws FileNotFoundException, IOException{  

final File file = new File("E:\\Project\\SD-WAN\\语言包统计.xls");


final ArrayList<ArrayList<Object>> rowList = new ArrayList<ArrayList<Object>>();
ArrayList<Object> colList;
final HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
final HSSFSheet sheet = wb.getSheetAt(4);
HSSFRow row;
HSSFCell cell;
   // for (int i = sheet.getFirstRowNum(), rowCount = 0; rowCount < sheet.getPhysicalNumberOfRows(); i++)
   for (int i = sheet.getFirstRowNum(), rowCount = 0; rowCount < 192; i++)
    {
    row = sheet.getRow(i);
    colList = new ArrayList<Object>();
    if (row == null)
    {
        //当读取行为空时
        if (i != sheet.getPhysicalNumberOfRows())
        {//判断是否是最后一行
            rowList.add(colList);
        }
        continue;
    }
    else
    {
        rowCount++;
    }


    cell = row.getCell(1);
    String fileNamde = cell.getStringCellValue();
    int a =  getContent(fileNamde);
   System.out.println(a);

} 

}


public static int getContent(String path) throws IOException{

    //获取文件对应的BufferedReader
    BufferedReader br = new BufferedReader(new FileReader(new File(path)));

    String tempstr; //临时字符串
    int num_of_words = 0; //总汉字数
    //int num_of_wordsAndPunctuation = 0; //汉字+标点
    //int num_blank = 0; //空格字符

    int linenull = 0;

    Pattern pattern =  Pattern.compile("([\u4e00-\u9fa5]{1})"); //定义匹配模式:1个汉字
   // Pattern pattern2 = Pattern.compile("([\u4e00-\u9fa5,,.。、/<>??;;'‘’:\"【】{}]{1})"); //定义匹配模式:汉字或标点符号
   // Pattern pattern3 = Pattern.compile("[\\s]");


   // while((tempstr = br.readLine()) != null && tempstr != ""){
    while( linenull< 5){
        tempstr = br.readLine();
        if(!StringUtils.isBlank(tempstr)&&tempstr.indexOf("\\u")>0) {
            tempstr = unicode2String(tempstr);
        }
        //汉字匹配,统计字数
        if (tempstr!=null  && !tempstr.isEmpty()){
        Matcher matcher = pattern.matcher(tempstr);
        while(matcher.find()) num_of_words++;
        }
//
//            //汉字标点匹配,统计字数
//            Matcher matcher2 = pattern2.matcher(tempstr);
//            while(matcher2.find()) num_of_wordsAndPunctuation++;
//
//            //空格匹配,统计字数
//            Matcher matcher3 = pattern3.matcher(tempstr);
//            while(matcher3.find()) num_blank++;
       // System.out.println("tempstr:/" + tempstr+"/"+tempstr.length());
        if (tempstr==null || tempstr.isEmpty()){
            linenull++;
           // System.out.println("linenull:" + linenull);
        }
        tempstr = "";
    }

    br.close(); //关闭文件
//
    return num_of_words;
}


public static String unicode2String(String unicode){  
    if(StringUtils.isBlank(unicode))return null;

    StringBuilder sb = new StringBuilder();  
    int i = -1;  
    int pos = 0;  

    while((i=unicode.indexOf("\\u", pos)) != -1){  
        sb.append(unicode.substring(pos, i));  
        if(i+5 < unicode.length()){  
            pos = i+6;  
            sb.append((char)Integer.parseInt(unicode.substring(i+2, i+6), 16));  
        }  
    }  

    return sb.toString();  
} 


}
首先,你需要安装 `@ckeditor/ckeditor5-vue` 和 `@ckeditor/ckeditor5-build-classic` 两个依赖包。 ``` npm install --save @ckeditor/ckeditor5-vue @ckeditor/ckeditor5-build-classic ``` 然后在你的 Vue 组件中引入 CKEditor 组件: ```vue <template> <div> <ckeditor :editor="editor" v-model="content" :config="editorConfig"></ckeditor> </div> </template> <script> import ClassicEditor from '@ckeditor/ckeditor5-build-classic' import CKEditor from '@ckeditor/ckeditor5-vue' export default { components: { ckeditor: CKEditor.component }, data() { return { content: '', editorConfig: { // 配置项 }, editor: ClassicEditor } } } </script> ``` 在上面的代码中,我们引入了 `ClassicEditor`,它是一个预先配置好的编辑器,包含了常用的插件,如加粗、斜体、链接等。我们也可以自定义 `editorConfig`,来配置编辑器。 下面是一个常用的配置项示例: ```js editorConfig: { toolbar: { items: [ 'bold', 'italic', 'link', '|', 'bulletedList', 'numberedList', '|', 'imageUpload', 'blockQuote', 'insertTable', 'undo', 'redo' ] }, image: { toolbar: [ 'imageTextAlternative', '|', 'imageStyle:full', 'imageStyle:side', '|', 'imageResize', '|', 'imageUpload', 'imageUpload', 'imageUpload', 'imageUpload' ], styles: [ 'full', 'side' ] }, language: 'zh-cn', table: { contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells', 'tableCellProperties', 'tableProperties' ] }, licenseKey: '', simpleUpload: { uploadUrl: '/your/upload/url', headers: { 'X-CSRF-TOKEN': 'CSRF-Token' } } } ``` 上面的配置中,我们开启了图片上传功能,并且添加了表格插入、图片上传、撤销、重做等常用功能。同时也设置了语言为中。 如果需要添加额外的插件,可以使用 `@ckeditor/ckeditor5-*` 的包名,比如添加字数统计插件: ``` npm install --save @ckeditor/ckeditor5-word-count ``` 然后在 `editorConfig` 中添加: ```js import WordCount from '@ckeditor/ckeditor5-word-count/src/wordcount'; editorConfig: { plugins: [WordCount], toolbar: [ 'wordCount' ] } ``` 这样就可以在编辑器中添加字数统计功能了。 我们也可以自定义上传图片的方法,需要在配置项中添加 `simpleUpload` 选项: ```js editorConfig: { simpleUpload: { uploadUrl: '/your/upload/url', headers: { 'X-CSRF-TOKEN': 'CSRF-Token' }, // 自定义上传方法 async upload(file) { // 这里写上传逻辑 const formData = new FormData(); formData.append('file', file); const response = await axios.post('/your/upload/url', formData, { headers: { 'Content-Type': 'multipart/form-data', 'X-CSRF-TOKEN': 'CSRF-Token' } }); return { default: response.data.url }; } } } ``` 在上面的代码中,我们使用了 axios 发送了一个 `multipart/form-data` 的请求,然后返回上传的图片地址。 希望以上内容能够帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值