html-strings-affixer adapted to JS - simplifying finding HTML string and replacing with affixed one
npm install hsa-jsSome nice string
in your text to become {{ __('Some nice string') }}
(Blade example) and etc.
resources/views), automatically identifying and reading files within.
hsa.js is a more versatile tool, designed for Node.js and web environments. It deviates from its predecessor in several key aspects:
hsa.js is built to work with strings rather than directly with files. This means it doesn't automatically scan and modify files but rather processes given string content.
hsa.js is adaptable to various environments and workflows, including but not limited to Laravel. It can be integrated into different stages of web development and content management processes, offering broader utility in diverse development contexts.
hsa.js a tool better suited for scenarios where direct file manipulation isn't desired or necessary.
npm i hsa-js
`
`
npm i hsa-js --save-dev
`
#### Via CDN
unpkg
`html
`
jsDelivr
`html
`
Configuration
HtmlStringsAffixer provides several configuration options, allowing for tailored usage according to specific requirements. Here's a detailed explanation of each configuration option:
1. prefix _(string)_: This is a string that will be prepended to each extracted string. The prefix is an essential part of the localization process, as it defines how the translated string will be identified in your code. For instance, in a Laravel Blade template, this might be {{ __(', indicating the start of a translation function call.
2. suffix _(string)_: Complementing the prefix, this string is appended to each extracted string. Continuing the Laravel Blade example, the suffix would be ') }}, closing the translation function call. The suffix, like the prefix, is crucial for correctly formatting the localized string in the code.
3. ignoreChars _(array)_: This option allows you to specify a set of characters that the HtmlStringsAffixer should ignore during the extraction process. These characters are typically ones that might interfere with the correct identification of strings to be localized. Common examples include symbols used in HTML or other markup languages, like "#" or "_".
4. warningChars _(array)_: These are characters that, when found in the extracted strings, will trigger warnings. This feature is particularly useful for identifying potential issues in your strings that might cause problems in the localization process. For instance, special characters like "%" or curly braces "{" and "}" might be part of non-translatable strings like math expressions. Provided warnings helps to manually check the strings.
5. extractions _(array)_: This configuration defines the types of extractions (And relevant replacement) that HtmlStringsAffixer will perform. It's an array that can include different extraction types, such as:
- "text": Extracts plain text from HTML or other templates.
- "placeholder": Specifically targets placeholder attributes in input elements.
- "alt": Extracts the alt text from image tags.
- "title": Targets the title attributes in HTML tags.
- "hashtag": Extracts strings that are marked with a hashtag, which is often used for specific labeling or marking of elements.
`javascript
// The configurations set as default
const affixer = new HtmlStringsAffixer({
prefix: "{{ __('", // Prefix to add before string
suffix: "') }}", // Suffix to add after string
// If one of these characters included in string, it will not be catched at all
ignore: ["#", "_", ">", "^", "*", "="],
// If on of these characters included in string, it will not replace but add in warning to manual checking
warnings: ["%", "{", "(", "}", ")", "$", "'"],
// All extraction types: regular html texts; placeholder, alt and title attribute values; Strings marked with "#" character in begining
extractions: ["text", "placeholder", "alt", "title", "hashtag"],
});
`
Usage
Basic usage example:
`javascript
import HtmlStringsAffixer from "hsa-js";
// Example of using HtmlStringsAffixer in a Laravel Blade template
const bladeTemplateContent =
;
const affixer = new HtmlStringsAffixer({
prefix: "{{ __('", // Prefix to add before string
suffix: "') }}", // Suffix to add after string
extractions: [
"text", // Since we have only texts
],
});
const localizedContent = affixer.affixIt(bladeTemplateContent);
console.log(localizedContent);
`
Check the other usage examples:
- blade template
- angular template
- react template
- vue template
- report (Returning report information)
- analyze (Returning detailed information)
Usage via CDN and HTML:
`html
`
html-examples
$3
If you need to replace only specific strings, just mark them with "#" and use "hashtag" extraction
`javascript
import HtmlStringsAffixer from "hsa-js";
// Example of using HtmlStringsAffixer in a Laravel Blade template
const bladeTemplateContent =
;
const affixer = new HtmlStringsAffixer({
prefix: "{{ __('", // Prefix to add before string
suffix: "') }}", // Suffix to add after string
extractions: [
"hashtag", // only strings starting with "#" wil be replaced
],
});
const localizedContent = affixer.affixIt(bladeTemplateContent);
console.log(localizedContent);
`
Contributing to hsa-js
We welcome contributions from the community and are delighted that you're interested in helping improve HtmlStringsAffixer! Whether you want to report a bug, request a feature, or contribute directly to the codebase, here's how you can get involved:
#### Reporting Bugs
If you encounter a bug while using HtmlStringsAffixer, we encourage you to report it. This helps us maintain the quality and reliability of the package. To report a bug:
1. Visit the Issues section of the HtmlStringsAffixer GitHub repository.
2. Before creating a new issue, please check if the bug has already been reported by another user. If so, you can add additional information to the existing report.
3. If your issue is new, click on New Issue and select Bug Report.
4. Fill in the issue template with as much detail as possible. Include steps to reproduce the bug, any error messages, and the environment (like the operating system and Node.js version) where the bug occurred.
5. Submit the issue.
#### Requesting Features
Your ideas for new features are always welcome. To request a feature:
1. Go to the Issues section of the HtmlStringsAffixer GitHub repository.
2. Check if someone else has already requested a similar feature. If so, feel free to add your thoughts or additional suggestions to that request.
3. If your feature request is unique, click on New Issue and select Feature Request.
4. Describe the feature you'd like to see, explaining how it would work and why it would be a beneficial addition to HtmlStringsAffixer.
5. Submit the feature request.
#### Contributing Code
Contributing directly to the codebase is a great way to help improve HtmlStringsAffixer. Here's how to get started:
1. Fork the Repository: Navigate to the HtmlStringsAffixer GitHub page and fork the repository to your own GitHub account.
2. Clone Your Fork: Clone your forked repository to your local machine.
3. Create a New Branch: Always create a new branch for your work. This keeps your changes organized and separate from the main codebase.
4. Make Your Changes: Implement your bug fix or feature.
5. Test Your Changes: Ensure your changes don't break existing functionality and that they align with the overall design and coding style of the project.
6. Commit Your Changes: Make concise and clear commit messages that explain your changes.
7. Submit a Pull Request: Push your changes to your fork and submit a pull request to the original repository. In your pull request, provide a clear description of the problem and your solution.
8. Code Review: Maintainers of the repository will review your code. Be open to feedback and any required changes.
#### Stay Involved
- Star the Repository: If you like HtmlStringsAffixer, consider starring the repository. This helps to grow the community and increases the visibility of the project.
- Join Discussions: Participate in discussions and provide feedback on proposed changes or features.
Your contributions, whether they're bug reports, feature requests, or code, play a significant role in the development of HtmlStringsAffixer. Together, we can build a tool that's more powerful, efficient, and user-friendly. Thank you for your support and involvement!
Support
Support Our Work? 🌟 You can help us keep the code flowing by making a small donation. Every bit of support goes a long way in maintaining and improving our open-source contributions. Click the button below to contribute. Thank you for your generosity!
Or use QR code:
---
$3
- While using hashtag ("#") extraction, it stops on " and ' characters, so #John with "double" will be replaced as {{ __('John with') }} "double"`