开源软件名称(OpenSource Name):bitjson/s18n开源软件地址(OpenSource Url):https://github.com/bitjson/s18n开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):Semantic Localization – s18nS18n parses the content of html elements and attributes to extract text for translation. The automatically generated locale file can then be translated into multiple languages and used by s18n to localize the website or application. This can be particularly powerful for static-generated sites and applications. Gulp users interested in this use-case should see gulp-l10n, which wraps s18n. s18n vs. i18nS18n provides a simpler, automated alternative to traditional ExampleThis example uses the s18n CLI. Given the following <h1>foo</h1>
<img alt="bar">
<foo s18n>baz</foo> Using the CLI's extract command in the same directory (with default element, attribute, and directive options): $ s18n extract > 'en.json' The following {
"37b51d19": "bar",
"73feffa4": "baz",
"acbd18db": "foo"
} This locale can be translated – or for testing, s18n mapped to a simulated locale: $ s18n map 'en.json' > 'fr.json' Producing {
"37b51d19": "bár",
"73feffa4": "báz",
"acbd18db": "fóó"
} This can then be used to test localization for the original file: $ s18n 'original.html' -l 'fr.json' > 'translated.html' <h1>fóó</h1>
<img alt="bár">
<foo s18n>báz</foo> Command-Line InterfacePlease note: some CLI options are not yet implemented for the To access the CLI system-wide, s18n can be installed globally using npm: $ npm install -g s18n For CLI usage information: $ s18n
$ s18n extract --help
$ s18n localize --help
$ s18n map --help CLI tests are not currently included in test coverage. Node API$ npm install s18n Extracting the Native LocaleS18n parses html content and extracts strings from selected html elements and attributes. A hash of each string is used to identify it, and all hash-string pairs are stored in a s18n.extract(html, [extract options])The var s18n = require('s18n');
var html = '<title>foo</title>' +
'<img alt="bar">' +
'<foo s18n>baz</foo>';
var locale = s18n.extract(html); The {
"acbd18db": "foo",
"37b51d19": "bar",
"73feffa4": "baz"
} s18n.extractFiles(glob, [extract options]).then(function(locale))The var s18n = require('s18n');
var htmlFiles = 'src/**/*.html';
var locale = s18n.extractFiles(htmlFiles)
.then(function(nativeLocale){
myApp.doSomething(nativeLocale);
})
.catch(function(err){
log.error(err);
}); Extract OptionsS18n's default extraction options will be ideal for most sites and applications, but advanced customization is possible. elements
S18n will extract the full contents of all selected elements. attributes
S18n will extract the full contents of all selected attributes. directives
S18n will extract the full contents of all elements with the following attributes. To localize only elements with an attribute set to a certain value, use an By default, the W3C standard cancelers
S18n will not extract the contents of any elements with the following attributes. To cancel localization of elements with an attribute set to a certain value, use an Cancelers take precedence over any setters/selectors. excluders
S18n will completely ignore any elements with the following attributes, canceling localization of all sub-elements. To exclude localization of elements with an attribute set to a certain value, use an By default, the W3C standard attributeSetter
S18n will extract the full contents of all attributes provided in the <meta name="description" content="Friendly description." s18n-attrs="content">
<img src="img/en/1.png" title="test" alt="test2" s18n-attrs="src title alt"> attributeCanceler
S18n will not extract the contents of any attributes provided in the Attribute cancels take precedence over any attribute setters/selectors. <img src="img/2.png" title="words" alt="words2" cancel-s18n-attrs="title alt"> hashAlgorithm
S18n will use this algorithm to take the hash of each string. hashLength
S18n will truncate hashes to this length when indexing strings in the trimWhitespace
S18n will trim whitespace from each string (using javascript's output
Based on this option, the LocalizeTo localize html, s18n searches through the html for strings in the s18n(html, [options])The s18n method accepts an html string, a Localize SettingsBoth the nativeLocale
The locale or locales
The localeThe var s18n = require('s18n');
var html = '<html lang="en"><title>foo</title><img alt="bar"><foo s18n>baz</foo></html>';
var settings = {
nativeLocale: s18n.extract(html),
nativeLocalId: 'en',
locale: {
"acbd18db": "fóó",
"37b51d19": "bár",
"73feffa4": "báz"
},
localeId: 'accents'
};
var content = s18n(html, settings); The '<html lang="accents"><title>fóó</title><img alt="bár"><foo s18n>báz</foo></html>' localesThe var s18n = require('s18n');
var html = '<html lang="en"><title>foo</title><img alt="bar"><foo s18n>baz</foo></html>';
var settings = {
nativeLocale: s18n.extract(html),
nativeLocalId: 'en',
locales: {
'ac': { "acbd18db": "fóó", "37b51d19": "bár", "73feffa4": "báz" }
'a2': { "acbd18db": "fó2", "37b51d19": "bá2", "73feffa4": "bá2" }
}
};
var content = s18n(html, settings); The {
'ac': '<html lang="ac"><title>fóó</title><img alt="bár"><foo s18n>báz</foo></html>'
'a2': '<html lang="a2"><title>fó2</title><img alt="bá2"><foo s18n>bá2</foo></html>'
} rewriteLangAttribute
When <html lang="en">English speakers often use the word <code s18n-lock-lang="en">do</code>.</html>
<html lang="de">Englisch-Lautsprecher verwenden oft das Wort <code lang="en">do</code>.</html> nativeLocaleId
The language code of the native document. localeId
The language code of the locale being used to localize the document. Testing LocalizationMaps18n.map(locale, options)var s18n = require('s18n');
var locale = {
"acbd18db": "foo",
"37b51d19": "bar"
}
var test = s18n.map(locale, { dictionary: 'accents' });
// test =>
{
"acbd18db": "fóó",
"37b51d19": "bár"
} Utilitiess18n.compareLocales(localeA, localeB, [options])s18n.formatLocale(locale, [options])optionsoutput (string)Options: Default: ContributingThe default Gulp task watches all files and runs tests and code coverage. $ npm install -g gulp
$ gulp TestingThis module strives to maintain passing tests with 100% coverage in every commit, and tests are run pre-commit. If you prefer, you can always skip this check with If you're unsure or would like help writing tests or getting to 100% coverage, please don't hesitate to open up a pull request so others can help! ThanksThanks to Stephen Pair of bitpay/translations for some of the architectural inspiration behind s18n. This module builds on the idea of using truncated hashes as identifiers for translatable strings, rather than manually developed indexes. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论