-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support automatic i18n string translation to pseudo code for debugging (
#1512) * support automatic i18n string translation to pseudo code for debugging * support automatic i18n string translation to pseudo code for debugging * added script to fetch all i18n strings from components * added script to fetch all i18n strings from components * Pseudo-translate I18n string placeholder values * Update pseudo-translation toggle in docs * write i18n token data to src-docs * new package scripts * List all I18n tokens on their own docs page * Pull i18n tokens out during build, validate
- Loading branch information
1 parent
d491d31
commit 17cab33
Showing
24 changed files
with
1,178 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
const babel = require('@babel/core'); | ||
const babelOptions = require('../../.babelrc'); | ||
const fs = require('fs'); | ||
const { promisify } = require('util'); | ||
const { basename, join, relative } = require('path'); | ||
const glob = require('glob'); | ||
const asyncGlob = promisify(glob); | ||
|
||
const rootDir = join(__dirname, '..', '..'); | ||
const srcDir = join(rootDir, 'src'); | ||
|
||
const tokenMappings = []; | ||
|
||
function getCodeForExpression(expressionNode) { | ||
return babel.transformFromAst(babel.types.program([ | ||
babel.types.expressionStatement( | ||
babel.types.removeComments(babel.types.cloneDeep(expressionNode)) | ||
) | ||
])).code; | ||
} | ||
|
||
function handleJSXPath(path) { | ||
if (path.node.name.name === 'EuiI18n') { | ||
const symbols = []; | ||
|
||
const attributes = path.node.attributes.reduce( | ||
(attributes, node) => { | ||
attributes[node.name.name] = node.value; | ||
return attributes; | ||
}, | ||
{} | ||
); | ||
|
||
if (attributes.hasOwnProperty('token') && attributes.hasOwnProperty('default')) { | ||
const tokenNode = attributes.token; | ||
const defStringNode = attributes.default; | ||
|
||
let defString; | ||
let highlighting; | ||
if (defStringNode.type === 'StringLiteral') { | ||
defString = defStringNode.value; | ||
highlighting = 'string'; | ||
} else if (defStringNode.type === 'JSXExpressionContainer') { | ||
defString = getCodeForExpression(defStringNode.expression); | ||
highlighting = 'code'; | ||
} | ||
symbols.push({ | ||
token: tokenNode.value, | ||
defString, | ||
highlighting, | ||
loc: path.node.loc | ||
}); | ||
} | ||
|
||
return symbols; | ||
} | ||
} | ||
|
||
function traverseFile(filepath) { | ||
const source = fs.readFileSync(filepath); | ||
const ast = babel.parse( | ||
source, | ||
{ | ||
...babelOptions, | ||
filename: basename(filepath), | ||
ast: true | ||
} | ||
); | ||
|
||
babel.traverse( | ||
ast, | ||
{ | ||
JSXOpeningElement(path) { | ||
if (path.node.name.name === 'EuiI18n') { | ||
const symbols = handleJSXPath(path); | ||
for (let i = 0; i < symbols.length; i++) { | ||
tokenMappings.push( | ||
{ ...symbols[i], filepath: relative(rootDir, filepath) } | ||
); | ||
} | ||
} | ||
} | ||
} | ||
); | ||
} | ||
|
||
(async () => { | ||
const files = (await asyncGlob( | ||
'**/*.@(js|ts|tsx)', | ||
{ cwd: srcDir, realpath: true }, | ||
)).filter(filepath => { | ||
if (filepath.endsWith('index.d.ts')) return false; | ||
if (filepath.endsWith('test.ts')) return false; | ||
if (filepath.endsWith('test.tsx')) return false; | ||
if (filepath.endsWith('test.js')) return false; | ||
|
||
return true; | ||
}); | ||
|
||
// extract tokens from source files | ||
files.forEach(filename => traverseFile(filename)); | ||
|
||
// validate tokens | ||
tokenMappings.reduce( | ||
(mappings, symbol) => { | ||
const { token, defString } = symbol; | ||
|
||
if (mappings.hasOwnProperty(token)) { | ||
if (mappings[token] !== defString) { | ||
console.error(`Token ${token} has two differing defaults:\n${defString}\n${mappings[token]}`); | ||
process.exit(1); | ||
} | ||
} else { | ||
mappings[token] = defString; | ||
} | ||
|
||
return mappings; | ||
}, | ||
{} | ||
); | ||
|
||
fs.writeFileSync( | ||
join(rootDir, 'src-docs', 'src', 'i18ntokens.json'), | ||
JSON.stringify(tokenMappings, null, 2) | ||
); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,7 @@ export default keyMirror({ | |
|
||
// Theme actions | ||
TOGGLE_THEME: null, | ||
|
||
// Locale actions | ||
TOGGLE_LOCALE: null, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
export { | ||
toggleTheme, | ||
} from './theme_actions'; | ||
|
||
export { | ||
toggleLocale, | ||
} from './locale_actions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import ActionTypes from './action_types'; | ||
|
||
export const toggleLocale = locale => ({ | ||
type: ActionTypes.TOGGLE_LOCALE, | ||
data: { | ||
locale, | ||
}, | ||
}); |
46 changes: 46 additions & 0 deletions
46
src-docs/src/components/guide_locale_selector/guide_locale_selector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { | ||
EuiSwitch, | ||
} from '../../../../src/components'; | ||
|
||
export class GuideLocaleSelector extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
isLocalePopoverOpen: false, | ||
}; | ||
} | ||
|
||
onLocaleButtonClick = () => { | ||
this.setState({ | ||
isLocalePopoverOpen: !this.state.isLocalePopoverOpen, | ||
}); | ||
}; | ||
|
||
closeLocalePopover = () => { | ||
this.setState({ | ||
isLocalePopoverOpen: false, | ||
}); | ||
}; | ||
|
||
render() { | ||
const otherLocale = this.props.selectedLocale === 'en' ? 'en-xa' : 'en'; | ||
|
||
return ( | ||
<EuiSwitch | ||
label="pseudo translation" | ||
checked={this.props.selectedLocale === 'en-xa'} | ||
onChange={() => this.props.onToggleLocale(otherLocale)} | ||
compressed={true} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
GuideLocaleSelector.propTypes = { | ||
onToggleLocale: PropTypes.func.isRequired, | ||
selectedLocale: PropTypes.string.isRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { GuideLocaleSelector } from './guide_locale_selector'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.