Git Product home page Git Product logo

acklocalization's Introduction

Tests Version License

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:

Name Required Note
defaultFileName Name of default strings(dict) file
destinations Dictionary 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
keyColumnName Name of column that contains keys to be localized
languageMapping Mapping of language column names to app languages, you specify how columns in spreasheet should be mapped to app languages
apiKey API key that will be used to communicate with Google Sheets API, apiKey or serviceAccount has to be provided
serviceAccount Path to service account file that will be used to access spreadsheet, apiKey or serviceAccount has to be provided
spreadsheetID Identifier of spreadsheet that should be downloaded
spreadsheetTabName Name 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_ios EN CS
hello Hello Ahoj
plist.InfoPlist.NSCameraUsageDescription Cammera usage description Popis 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.

acklocalization's People

Contributors

babacros avatar fortmarek avatar igorrosocha avatar janmisar avatar leinhauplk avatar lukasackee avatar lukashromadnik avatar olejnjak avatar vendulasvastal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

acklocalization's Issues

SpreadSheet Plural example

Hello ;)
Is it possible to add a spreadsheet example that has plurals ? I can't figure out how to make it work ?
English os not my native language and I am not sure to understand correctly how to use: translation.key##{zero}
Thanks in advance

Add support for position arguments

Right now ACKLocalization doesn't support position arguments. Adding position arguments to localization doc will result in Localizable.strings file, which wouldn't allow using them.

Source value: %1$s
Generated value in Localizable.strings: %%1%s
Correct: %1$@

This also applies to other formatting placeholders.

Should be just reimplementation of LocRow.normalizedValue.

Spreadsheet without plurals creates empty stringsdict file

If spreadsheet doesn't contain any plurals, stringsdict file is still created. This can break the iOS localization system.

I assume it will be the same issue if spreadsheet will contain only plurals. Check this case too, even though it will not be that frequent.

Support for config files

Configuration using arguments is not as comfortable, allow configuration using JSON/YAML/etc. file

String catalog

Hi,

are you planning to add support for the new string catalog format for XCode 15?

Thank you,
Ondrej

How to generate InfoPlist.strings?

Hello.
Thank you for ACKLocalization.
As I understand with ACKLocalization I can only generate Localizable.strings. But what about InfoPlist.strings?

Quotes in keys are not escaped

I am using English translation as keys. When a translation has a quote in it, the quote is not escaped - and therefore, the format of .strings file is invalid, and project can't build.

For |en:Say "hello"|de:Sag "hallo"|, I expect to get "Say \"hello\"" = "Sag \"hallo\"";
And what I get now is "Say "hello"" = "Sag \"hallo\"";.

Unable to fetch public sheet

It can be usual use case that user might want to fetch localizations from public spreadsheet so no API key nor service account will be provided.

Right now we fail with error but this should be allowed.

 ❌ Either apiKey or serviceAccount must be provided in localization.json

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.