Skip to content

Instantly share code, notes, and snippets.

@larsxschneider
Last active December 11, 2015 09:19
Show Gist options
  • Save larsxschneider/4579205 to your computer and use it in GitHub Desktop.
Save larsxschneider/4579205 to your computer and use it in GitHub Desktop.
Generate localization files for iOS and Javascript (jquery globalize).
#pragma once
// Use this macro to localize strings in "common" code (code that is used in iOS and Javascript)
#define AIMLocalizedString(string, description) string
#!/bin/bash
#
# Generate localization files for iOS and Javascript.
#
# How does it work?
# Cross platform strings and Javascript strings are temporarily converted to NSLocalizedStrings.
# Afterwards the 'genstrings' tools is used to generate iOS localization files. These files are
# converted to jQuery localization files (https://github.com/jquery/globalize) to be used in Javascript.
#
# Attention: You need to add new languages manually at the end of the file
echo "Localize..."
BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
COMMON_SRC_DIR="$BASE_DIR/Source/Common"
IOS_SRC_DIR="$BASE_DIR/Source/iOS"
WEB_SRC_DIR="$BASE_DIR/Source/Emscripten"
GEN_LOCAL_DIR=$"$BASE_DIR/Generated/Localization"
IOS_LOCAL_DIR=$"$BASE_DIR/Resources/iOS/Localization"
WEB_LOCAL_DIR=$"$BASE_DIR/Resources/Web/Localization"
# Clean up generated folder
rm -rf "$GEN_LOCAL_DIR"
mkdir -p "$GEN_LOCAL_DIR"
# Generate cross platform strings
grep 'AIMLocalizedString([[:space:]]*' --no-filename --recursive --exclude Localize.h $COMMON_SRC_DIR | \
grep -v '^[[:space:]]*//' | \
sed -e 's/AIMLocalizedString(/NSLocalizedString(/g' | \
sed -e 's/([[:space:]]*"/(@"/g' | sed -e 's/,[[:space:]]*"/, @"Used in web and mobile build, /g' \
> $GEN_LOCAL_DIR/commonstrings.m
# Generate Web strings
grep 'Globalize.localize([[:space:]]*"' --no-filename --recursive $WEB_SRC_DIR |
grep -v '^[[:space:]]*//' | \
sed -e 's/Globalize.localize([[:space:]]*"/NSLocalizedString(@"/g' | \
sed -e 's/"[[:space:]]*)/", @"Used in web Build");/g' | \
grep -o 'NSLocalizedString(@".*", @".*")' \
> $GEN_LOCAL_DIR/webstrings.m
# Generate iOS localization files based on iOS, Web, and cross platform strings
find $IOS_SRC_DIR/Classes $GEN_LOCAL_DIR -iname "*.m*" | xargs genstrings -o $IOS_LOCAL_DIR/en.lproj
# Generate Javascript localization files
function generateJavascriptLocal {
LOCAL=$1
IOS_LOCAL_FILENAME=$2/Localizable.strings
WEB_LOCAL_FILENAME=$3
echo -e "/* Generated file. Don't modify */\nvar lang = \"$LOCAL\";\nvar culture = Globalize.findClosestCulture(lang);\nconsole.log('Loading localized strings for \"' + lang + '\"');\n" \
> $WEB_LOCAL_FILENAME
# genstrings generates UTF16 files; encode them in UTF8 to enable sed processing on them
iconv -f utf-16 -t utf-8 $IOS_LOCAL_FILENAME | \
sed -e 's/^"/culture.messages["/' | \
sed -e 's/" = "/"] = "/' >> $WEB_LOCAL_FILENAME
}
generateJavascriptLocal "en" $IOS_LOCAL_DIR/en.lproj $WEB_LOCAL_DIR/strings.default.js
generateJavascriptLocal "de" $IOS_LOCAL_DIR/de.lproj $WEB_LOCAL_DIR/strings.de.js
generateJavascriptLocal "fr" $IOS_LOCAL_DIR/fr.lproj $WEB_LOCAL_DIR/strings.fr.js
generateJavascriptLocal "ja" $IOS_LOCAL_DIR/ja.lproj $WEB_LOCAL_DIR/strings.ja.js
generateJavascriptLocal "pt" $IOS_LOCAL_DIR/pt.lproj $WEB_LOCAL_DIR/strings.pt-BR.js
generateJavascriptLocal "ru" $IOS_LOCAL_DIR/ru.lproj $WEB_LOCAL_DIR/strings.ru.js
generateJavascriptLocal "zh" $IOS_LOCAL_DIR/zh-Hans.lproj $WEB_LOCAL_DIR/strings.zh-Hans.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment