Package: googlesheettranslate
Overviewsheet-localizationgenerates Android / iOS localization files from single Google Spreadsheet.Input supports:CVSCSVExcelGoogleSheetAPIGoogleSheet URLOutput supports:JSON file general forJS i18npythoni18n JSONAndroid XML fileLocalizationThis is a python script that downloads localization strings and CI color information from a publicGoogle Sheetand generates resource files for iOS and Android projects.Installationpip3.8 install googlesheettranslatePrerequisitesCreate a Google Sheet document with sheets likethese:Publish the sheeet to the web by pressingFile -> Publish to the web. SelectWhole DocumentandWebsite. This gives the script access to the public JSON API of Google Docs.Find the ID of your document by copying it from your browsers address bar.UsageCreate adata_sync.config.jsonin your project and populate it with the contents of theexample config. Replace thesheetIdwith your own. You can modify the other settings to fit the needs of your project.Then open a terminal at your project path and runl10n_sync. Look for your newly created resource files.You can also copydata_sync.pyto your project and then runpython3 data_sync.pywhen your config file is ready.ExampleHave a look into theOutput folderfor some example files generated by using the providedconfiguration file.Command line parametersYou generally runsheet-localizationlike this:python /path/to/sheet-localization/main.py /path/to/account_credentials.json SpreadSheetName TargetNameaccount_credentials.jsonis a file you get after registering Google service accountSpreadSheetNameis the name of the spreadsheet to openNote: Google service account must be given read permission to access the spreadsheet
by sharing the document with the service account's emailTargetNamecan be one ofandroid,ios, orios-swift.Targetsandroidtargetsheet-localizationgeneratesres/value-<lang>/strings.xmlhierarchy in the current directory.iostargetsheet-localizationgenerates<lang>.lproj/Localizable.stringshierarchy in the current directory.The script also generatesLocalizationConstants.h,LocalizationConstants.mfiles with
translation constants.ios-swifttargetsheet-localizationgenerates<lang>.lproj/Localizable.stringshierarchy in the current directory.The script also generatesLocalizationConstants.swiftfiles with translation constants
(actually, enum).InstallationThe scripts requirePython 2.7+.iOS generation will not work in Python 3+Linux
TODOWindows
TODOmacOSUpgradesixpackagesudo easy_install -U sixInstallpippackage managersudo easy_install pipInstallgspreadandoauth2clientpackagessudo -H pip install --upgrade gspread oauth2clientStep by step guide1. Clone sample Google spreadsheetClonesample spreadsheetto your Google Drive.The sample document contains two mandatory pages:SRCcontains translations and keysCFGcontains configurations where keys are located and what row translations start atThese pages are required by the script, so make sure to keep them.2. Create new Google API projectGo toGoogle API consoleand create a new project.3. Enable Google Drive APIEnable Google Drive API.4. Create service accountCreate service account credentials for aWeb serverto accessApplication dataName the service account and give itProject -> Editorrole.Upon account creation you should get a special JSON, which contains all necessary credentials.5. Allow service account to read the spreadsheet documentThe JSON you downloaded looks like this:{
  "type": "service_account",
  "project_id": "localization-173405",
  "private_key_id": "d37cdb95af7f817a05c",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCO8ACoDvAG0q8R\xg3bQzHYCVrEDBcBFkfJ4d8dfy9FdIS++p3XvmLOWnFyMreQTPh1\njmx7jdmDpEwZHNZrj2dYYf0Xta8A0wxdejqUmNq4CyOBqTzomqCdzu36qBp8szUk\nN1l9G9u+rLcm9J/BlinOeA==\n-----END PRIVATE KEY-----\n",
  "client_email": "localization@localization-173405.iam.gserviceaccount.com",
  "client_id": "1016040",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/localization%40localization-173405.iam.gserviceaccount.com"
}To allow the service account to read your spreadsheet, you need to giveclient emailread permissions to your document. You can do it in sharing settings.6. Generate Android localization filesGo to the directory of your choice and
generate Android localization files with the following commands:cd /path/to/android/projectpython /path/to/sheet-localization/main.py /path/to/service_account_credentials.json 'Spreadsheet Key' androidThis createsres/values-<lang>/strings.xmlfiles with contents like this:<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="history">History</string>
<string name="notifications">Notifications</string>
<string name="favorite_places">Favourite places</string>
<string name="about">About</string>
<string name="wait_map_tag">wait_map_tag</string>
<string name="main_title_order"></string>
<string name="order_detail_title"></string>
<string name="action_im_here">I'm here</string>
<string name="action_get_order">Order</string>
</resources>7. Generate iOS localization filesGo to the directory of your choice and
generate iOS localization files with the following commands:cd /path/to/android/projectpython /path/to/sheet-localization/main.py /path/to/service_account_credentials.json 'Spreadsheet Key' iosThis creates<lang>.lproj/Localizable.stringsfiles with contents like this:"Menu.Item.History" = "History";
"Menu.Item.Notifications" = "Notifications";
"Menu.Item.Favourites" = "Favourite places";
"Menu.Item.About" = "About";
"Profile.Email.Title" = "Email";
"Profile.BirthDate.Title" = "Birth date";
"Profile.ChangeAvatar.Title" = "Another photo";
"Order.Address.Here" = "I'm here";
"Details.Order" = "Order";LocalizationConstants.hfile with contents:/*!
* @b en@: History

* @b ru@: История

* @b uk@: Історія

*/
extern NSString * const trMenuItemHistory;

/*!
* @b en@: Notifications

* @b ru@: Уведомления

* @b uk@: Сповіщення

*/
extern NSString * const trMenuItemNotifications;LocalizationConstants.mfile with contents:import "LocalizableConstants.h"
NSString * const trMenuItemHistory = @"Menu.Item.History";
NSString * const trMenuItemNotifications = @"Menu.Item.Notifications";
NSString * const trMenuItemFavourites = @"Menu.Item.Favourites";
NSString * const trMenuItemAbout = @"Menu.Item.About";
NSString * const trProfilePhoneTitle = @"Profile.Phone.Title";
NSString * const trProfileEmailTitle = @"Profile.Email.Title";Localization constants help prevent string typos.8. Generate iOS localization files for SwiftIf you useios-swifttarget, you get singleLocalizationConstants.swift,
which looks like this:import Foundation

enum L10n {
    case MenuItemHistory
    case MenuItemNotifications
    case MenuItemFavourites
    case MenuItemAbout
}


extension L10n: CustomStringConvertible {
    var description: String { return self.string }

    var string: String {
        switch self {
            case .MenuItemHistory:
                return L10n.tr(key: "Menu.Item.History")
            case .MenuItemNotifications:
                return L10n.tr(key: "Menu.Item.Notifications")
            case .MenuItemFavourites:
                return L10n.tr(key: "Menu.Item.Favourites")
            case .MenuItemAbout:
                return L10n.tr(key: "Menu.Item.About")
        }
    }

    private static func tr(key: String, _ args: CVarArg...) -> String {
        let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "")
        return String(format: format, locale: Locale.current, arguments: args)
    }
}

func tr(_ key: L10n) -> String {
    return key.string
}

private final class BundleToken {}Note: file format is similar to whatSwiftGengenerates.You can later reference such a constant in code like this:self.yourTextField.text = tr(.MenuItemAbout)