i18n extract
v0.6.7
通過靜態分析來管理本地化。
npm install --save-dev i18n-extract該模塊對密鑰用法進行靜態分析代碼,例如i18n.t('some.key') ,以:
例如,此模塊與以下結合效果很好:
marker: 'polyglot.t', )marker: 'i18n', ) i18n ( 'key.static' ) i18n ( 'key.' + 'concat' ) i18n ( `key.template` ) i18n ( `key. ${ dynamic } ` ) /* i18n-extract key.comment */ 解析code以提取I18N( key )的調用參數。
code應該是字符串。 import { extractFromCode } from 'i18n-extract' ;
const keys = extractFromCode ( "const followMe = i18n('b2b.follow');" , {
marker : 'i18n' ,
} ) ;
// keys = ['b2b.follow']解析files以提取I18N( key )的調用參數。
files可以是字符串數組或字符串。您也可以使用地球。 import { extractFromFiles } from 'i18n-extract' ;
const keys = extractFromFiles ( [
'*.jsx' ,
'*.js' ,
] , {
marker : 'i18n' ,
} ) ;marker :國際化字符串標記功能的名稱。默認為i18n 。keyLoc :一個整數,指示鍵在參數中的位置。默認為0 。負數,例如-1 ,表示相對於參數列表末尾的位置。parser :枚舉表明要使用的解析器可以是typescript或flow 。默認為flow 。babelOptions :Babel配置對象,允許在掃描I18N鍵之前應用自定義轉換或插件。使用所有巴比倫插件啟用了默認為配置。報告缺少的鍵。這些密鑰可能應該翻譯。
locale應該是包含翻譯的對象。keysUsed應該是一個數組。包含源代碼中使用的密鑰。可以用extractFromFiles我們的extractFromCode檢索。 import { findMissing } from 'i18n-extract' ;
const missing = findMissing ( {
key1 : 'key 1' ,
} , [ 'key1' , 'key2' ] ) ;
/**
* missing = [{
* type: 'MISSING',
* key: 'key2',
* }];
*/ 報告未使用的密鑰。這些密鑰可能應被刪除。
locale應該是包含翻譯的對象。keysUsed應該是一個數組。包含源代碼中使用的密鑰。可以用extractFromFiles我們的extractFromCode檢索。 import { findUnused } from 'i18n-extract' ;
const unused = findUnused ( {
key1 : 'key 1' ,
key2 : 'key 2' ,
} , [ 'key1' ] ) ;
/**
* unused = [{
* type: 'UNUSED',
* key: 'key2',
* }];
*/報告重複的密鑰。這些鑰匙可能應該相互合作。默認threshold是1,它將報告任何重複的翻譯。
locale應該是包含翻譯的對象。keysUsed應該是一個數組。包含源代碼中使用的密鑰。可以用extractFromFiles我們的extractFromCode檢索。options應為對象。您可以提供一個threshold屬性,以更改重複值的數量,然後再添加到報告中。 import { findDuplicated } from 'i18n-extract' ;
const duplicated = findDuplicated ( {
key1 : 'Key 1' ,
key2 : 'Key 2' ,
key3 : 'Key 2' ,
} ) ;
/**
* unused = [{
* type: 'DUPLICATED',
* keys: [
* 'key2',
* 'key3',
* ],
* value: 'Key 2',
* }];
*/報告任何動態密鑰。使用動態密鑰可以說更危險。他們可能會破裂。
locale應該是包含翻譯的對象。keysUsed應該是一個數組。包含源代碼中使用的密鑰。可以用extractFromFiles我們的extractFromCode檢索。 import { forbidDynamic } from 'i18n-extract' ;
const forbidDynamic = forbidDynamic ( { } , [ 'key.*' ] ) ;
/**
* forbidDynamic = [{
* type: 'FORBID_DYNAMIC',
* key: 'key.*',
* }];
*/弄平物體。
object應該是對象。 import { flatten } from 'i18n-extract' ;
const flattened = flatten ( {
key2 : 'Key 2' ,
key4 : {
key41 : 'Key 4.1' ,
key42 : {
key421 : 'Key 4.2.1' ,
} ,
} ,
} ) ;
/**
* flattened = {
* key2: 'Key 2',
* 'key4.key41': 'Key 4.1',
* 'key4.key42.key421': 'Key 4.2.1',
* };
*/輸出一個新的PO文件,僅包含messages中的消息。如果在poInput中已經存在消息,我們將保留翻譯。如果不存在消息,我們添加了一個新的空翻譯。
messages應該是一個數組。poInput應該是字符串。poOutput應該是字符串。 import { mergeMessagesWithPO } from 'i18n-extract' ;
const messages = [ 'Message 1' , 'Message 2' ] ;
mergeMessagesWithPO ( messages , 'messages.po' , 'messages.output.po' ) ;
/**
* Will output :
* > messages.output.po has 812 messages.
* > We have added 7 messages.
* > We have removed 3 messages.
*/ 麻省理工學院