Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Multilanguage Support #67

Merged
merged 13 commits into from
Oct 16, 2023
Merged

Add Multilanguage Support #67

merged 13 commits into from
Oct 16, 2023

Conversation

agmmnn
Copy link
Contributor

@agmmnn agmmnn commented Oct 15, 2023

Summary of Changes:

  • On first extension installation, the browser's language is detected and the extension language is set accordingly. (If exist otherwise fallback to english)
  • Created translation files with standart chrome localization format in _locales folder.
  • Added Language Select UI and tabs on options page.
  • Also dynamically import version from manifest in popup and options page.

Adding new language:

  • Append the new language to the _locales/list.json file.
  • Create a new folder with the language code (e.g., en) within the _locales directory.
  • Inside the language folder, create a messages.json file containing translations for the new language.

Using i18n interface:

Using i18n interface in TypeScript/JavaScript and HTML files as demonstrated below:

  • In TypeScript/JavaScript:
import { createTranslationProxy, getTranslation} from './util'

const langData = await getTranslation() //load translation data from file
const getString = createTranslationProxy(langData) //create i18n interface

getString('ext_description')
// Output: "Search for text on a webpage and replace it with different text."
  • In HTML:
<!-- In your HTML file, use the data-locale attribute to localize elements: -->
<a class="nav-link" href="#type" data-locale="next_vs_all"></a>
// Output: Replace Next vs. Replace All

<!-- Include the localization script at the end of your HTML file -->
<script src="../help.js"></script>
</body>
// help.ts
import { localizeElements } from './util'

window.addEventListener('DOMContentLoaded', async function () {
	...
    // Localize HTML elements, this will automatically update elements containing the 'data-locale' attribute. 
    localizeElements(langData) // Add to end of the function
})

More About the i18n Interface:

// User's selected language is Spanish

// If key exist in language file:
getString('saved_rules') // Output: Reglas guardadas

// If the key doesnt exist in language file, but exist in fallback language "english":
getString('saved_rules') // Output: Saved Rules

// If the key also doesnt exist in fallback language file, it returns key itself:
getString('saved_rules') // Output: saved_rules

If there is any problem you can open a new Issue and tag me. 🙏

Copy link
Owner

@forgetso forgetso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@forgetso forgetso merged commit b62ffe3 into forgetso:master Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants