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

Karan1213 add web translation #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
The number of languages you support. Please check the README.md for more
information on column positions.
*/
var NUMBER_OF_LANGUAGES = 1;
var NUMBER_OF_LANGUAGES = 2;

/*
The script expects two columns for iOS and Android identifiers, respectively,
Expand All @@ -40,6 +40,7 @@ var IOS_INCLUDES_LOCALIZABLE_ENUM = true;

var LANGUAGE_IOS = 'iOS';
var LANGUAGE_ANDROID = 'Android';
var LANGUAGE_WEB = 'Web';
var DEFAULT_LANGUAGE = LANGUAGE_IOS;


Expand All @@ -50,6 +51,7 @@ function onOpen() {
ui.createMenu('Custom Export')
.addItem('iOS', 'exportForIos')
.addItem('Android', 'exportForAndroid')
.addItem('Web (Json)', 'exportForWeb')
.addToUi();
}

Expand All @@ -71,6 +73,15 @@ function exportForAndroid() {
exportSheet(e);
}

function exportForWeb() {
var e = {
parameter: {
language: LANGUAGE_WEB
}
};
exportSheet(e);
}

/*
Fetches the active sheet, gets all of the data and displays the
result strings.
Expand Down Expand Up @@ -159,6 +170,9 @@ function makeString(object, textIndex, options) {
case LANGUAGE_IOS:
return makeIosString(object, textIndex, options);
break;
case LANGUAGE_WEB:
return makeJsonString(object, textIndex, options);
break;
default:
break;
}
Expand Down Expand Up @@ -269,6 +283,33 @@ function makeIosString(object, textIndex, options) {
return exportString;
}

/*
Creates the Localizable.json file.
*/
function makeJsonString(object, textIndex, options) {

var exportString = ""; var prevIdentifier = "";
exportString += "{\n";

for(var i=0; i<object.length; i++) {
var o = object[i];
var identifier = o.identifierWeb;
var text = o.texts[textIndex];

if (text == undefined || text == "") {
continue;
}

if(identifier == "") {
continue;
}

exportString += '"' + identifier + '" : "' + text + "\",\n";
}
exportString += "}";
return exportString;
}


// Data fetching

Expand Down Expand Up @@ -363,7 +404,7 @@ function getObjects(data, keys) {
cellData = "";
}

if (keys[j] != "identifierIos" && keys[j] != "identifierAndroid") {
if (keys[j] != "identifierIos" && keys[j] != "identifierAndroid" && keys[j] != "identifierWeb") {
object["texts"].push(cellData);
} else {
object[keys[j]] = cellData;
Expand Down Expand Up @@ -421,4 +462,4 @@ function arrayTranspose_(data) {
}

return ret;
}
}
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# localizable-sheet-script
A Google Sheets script that will take a sheet in a specific format and return iOS and Android localization files.
A Google Sheets script that will take a sheet in a specific format and return iOS, Android and Web (Json) localization files.

## What it does

For Android it creates an XML resources file with all of the strings. For iOS it creates a Localizable enum with String constants, and a .strings file.
For Android it creates an XML resources file with all of the strings. For iOS it creates a Localizable enum with String constants, and a .strings file. For Web it creates .json file with all of the strings.

## Installing

Expand All @@ -14,16 +14,16 @@ For Android it creates an XML resources file with all of the strings. For iOS it
## Usage

1. Open your sheet.
2. Go to **Custom Export** and select your **iOS** or **Android**.
2. Go to **Custom Export** and select your **iOS**, **Android** or **Web (Json)**.

## Sheet format

The script expects the sheet to be formatted in a specific way.

| ... arbitrary number of columns before iOS keys | **Identifier iOS** | **Identifier Android** | English text | German text | ... |
|-------------------------------------------------|--------------------|------------------------|--------------|-------------|-----|
| place whatever you want in here | login_button_title | login_button_title | Login | Einloggen | |
| | ... | ... | ... | ... | |
| ... arbitrary number of columns before iOS keys | **Identifier iOS** | **Identifier Android** | **Identifier Web** | English text | German text | ... |
|-------------------------------------------------|--------------------|------------------------|------------------------|--------------|-------------|-----|
| place whatever you want in here | login_button_title | login_button_title | login_button_title | Login | Einloggen | |
| | ... | ... | ... | ... | ... | |

**The texts in bold cannot be changed!** The script depends on them to know which identifier is which. The other texts don't matter.

Expand All @@ -41,7 +41,7 @@ The position of the first (iOS) column that is relevant to the script is changed

## Exported files

The exported files are the standard format (`strings.xml` or `Localizable.strings`) for the specific platforms. iOS also includes a `Localizable` `enum` which contains all of the keys as `static let` properties for code-completion and less typos. 
The exported files are the standard format (`strings.xml`, `Localizable.strings` or `strings.json`) for the specific platforms. iOS also includes a `Localizable` `enum` which contains all of the keys as `static let` properties for code-completion and less typos. 

## License: MIT

Expand Down
6 changes: 6 additions & 0 deletions Sample.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Identifier iOS,Identifier Android,Identifier Web,English text,Arabic text
ok,ok,ok,Ok,حسنا
cancel,cancel,cancel,Cancel,الغاء
resend,resend,resend,Resend,اعد الارسال
submit,submit,submit,Submit,تم
send,send,send,Send,ارسل