') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); GitHub - svastven/ACKLocalization: Localize your Cocoa apps from Google Spreadsheet · GitHub
Skip to content

Repository files navigation

TestsVersionLicense

ACKLocalization

Simply localize your app with translations stored in Google Spreadsheet.

Installation

Cocoapods

  1. Add ACKLocalization to your Podfile
pod 'ACKLocalization`
  1. Install pods
pod install

Manually

Just download binary from Github release

Usage

You can use ACKLocalization in two ways:

  1. safer and recommended use with Service Account
  2. use with API key

Use with Service Account

This method is safe in the way that you can fully control who has access to your spreadsheet. You create a service account, invite it as viewer to your spreadsheet and you're ready to go.

Create a Service Account

You need to have a Google project in Google Dev console, there you go to Credentials ➡️ create credentials ➡️ service account key. Then you need to enable Google Sheets API in the console.

Invite Service Account to your spreadsheet

The Service Account has associated email address, you need to invited that address to your spreadsheet, so it is available to read the document.

Now you can skip to getting your spreadsheet identifier

Use with API key

You need to have a Google project in Google Dev console, there you go to Credentials ➡️ create credentials ➡️ API Key. This will create unrestricted API key, we recommend to restrict it as much as possible - application restrictions are unluckily not possible to use - maybe if you intend to run the script on CI, then you can restrict it to certain IPs. What you can do is to restrict the API key to Google Sheets API (you might need to enable this API first in the console).

Get your Google Spreadsheet identifier

You get the spreadsheet identifier from its URL:

https://docs.google.com/spreadsheets/d/<YOUR_SPREADSHEET_ID>/edit#gid=0

Now you can build your configuration file

The file is named localization.json. This is how example file looks like, you can find the Swift representation here:

{
"defaultFileName": "Localizable",
"destinations": {
"Localizable": "Framework/Resources",
"InfoPlist": "App/Resources"
},
"keyColumnName": "iOS",
"languageMapping": {
"CZ": "cs"
},
"serviceAccount": "Resources/ServiceAccount.json",
"spreadsheetID": "<GOOGLE_SPREADSHEET_ID>",
}

Attributes documentation:

NameRequiredNote
defaultFileNameName of default strings(dict) file
destinationsDictionary of destinations for all generated files (at least entry with defaultFileName is required), if you use plist.<filename>. prefix in your sheet, you might wanna add those entries as well, otherwise they will be generated alongside the default file
keyColumnNameName of column that contains keys to be localized
languageMappingMapping of language column names to app languages, you specify how columns in spreasheet should be mapped to app languages
apiKeyAPI key that will be used to communicate with Google Sheets API, apiKey or serviceAccount has to be provided
serviceAccountPath to service account file that will be used to access spreadsheet, apiKey or serviceAccount has to be provided
spreadsheetIDIdentifier of spreadsheet that should be downloaded
spreadsheetTabNameName of spreadsheet tab to be fetched, if nothing is specified, we will use the first tab in spreadsheet

The file has to be in the same directory where you call ACKLocalization.

To be able to communicate with Google Sheets API, you need to provide either apiKey or serviceAccount parameter or use environment variable. If both are provided, then serviceAccount will be used.

Environment variables

Do you want to share secrets across multiple projects, or do you not want to keep secrets in the project repository? We have the solution for you. Just set one of the environment variables below.

ACKLOCALIZATION_SERVICE_ACCOUNT_PATH - Path to service account file that will be used to access spreadsheet ACKLOCALIZATION_API_KEY - API key that will be used to communicate with Google Sheets API

apiKey or serviceAccount defined in localization.json have higher priority than environment values. If both are provided then ACKLOCALIZATION_SERVICE_ACCOUNT_PATH will be used.

Calling ACKLocalization

Just call the binary, remember that the configuration file has to be in the same directory where you call ACKLocalization.

Pods/ACKLocalization/Localization

Example

We love to call ACKLocalization from Xcode (we have a separate aggregate target which calls the script) so I'll stick with that with this example.

Project structure

This is example folder structure of the project

|-localization.json
|-Podfile
|-Project.xcodeproj
|-ServiceAccount.json
|-App
|---Resources
|------en.lproj
|----------InfoPlist.strings
|------cs.lproj
|----------InfoPlist.strings
|-Framework
|---Resources
|------en.lproj
|----------Localizable.strings
|----------Localizable.stringsDict
|------cs.lproj
|----------Localizable.strings
|----------Localizable.stringsDict

Spreadsheet structure

This is example structure of the spreadsheet with translations

key_iosENCS
helloHelloAhoj
plist.InfoPlist.NSCameraUsageDescriptionCammera usage descriptionPopis využití kamery

Example config file for this case would be

This is the example config file:

{
"defaultFileName": "Localizable",
"destinations": {
"Localizable": "Framework/Resources",
"InfoPlist": "App/Resources"
},
"keyColumnName": "key_ios",
"languageMapping": {
"CS": "cs",
"EN": "en"
},
"serviceAccount": "Resources/ServiceAccount.json",
"spreadsheetID": "<GOOGLE_SPREADSHEET_ID>",
"spreadsheetTabName": "Localizations"
}

Plist keys

ACKLocalization supports localizing plist files. Simply prefix the key with plist.InfoPlist, where InfoPlist is the name of plist you need to localize and ACKLocalization will automatically generate respective InfoPlist.strings files. Please note that InfoPlist name is case-sensitive.

Example keys:

plist.InfoPlist.NSPhotoLibraryAddUsageDescription
plist.InfoPlist.NSUserTrackingUsageDescription

InfoPlist.strings result:

"NSPhotoLibraryAddUsageDescription" = "Your photo library usage message.";
"NSUserTrackingUsageDescription" = "Your tracking permission message.";

Plural keys

To add plurals to the spreadsheet, you need to specify the translation key and the plural type using the following native convention:

translation.key##{zero}
translation.key##{one}
translation.key##{two}
translation.key##{few}
translation.key##{many}
translation.key##{other}

This will be automatically generated into Localizable.stringsDict and the key won't be presented in Localizable.strings.

Example:

ackee|ACKLocalization

Author

Ackee team

License

ACKLocalization is available under the MIT license. See the LICENSE file for more info.

About

Localize your Cocoa apps from Google Spreadsheet

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages