ObjecTips

Swift & Objective-C で iOS とか macOS とか

ibtool を使ってローカライズ

サンプルに使用する Main.storyboard ファイルは ViewController の上に1つ UILabel が載っているだけの状態

f:id:Koze:20160222034716p:plain

storyboard ファイルの中身のXMLは以下

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9532" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="tne-QT-ifu">
            <objects>
                <viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
                    <layoutGuides>
                        <viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
                        <viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
                    </layoutGuides>
                    <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
                        <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <subviews>
                            <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="H4E-4z-9nL">
                                <rect key="frame" x="20" y="20" width="80" height="21"/>
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                <nil key="highlightedColor"/>
                            </label>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                    </view>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="506" y="407"/>
        </scene>
    </scenes>
</document>
strings の生成

ibtool --generate-strings-file Main.strings Main.storyboard
または
ibtool --export-strings-file Main.strings Main.storyboard

生成された strings

/* Class = "UILabel"; text = "Label"; ObjectID = "H4E-4z-9nL"; */
"H4E-4z-9nL.text" = "Label";
xliff の生成

ibtool --export-xliff Main.xliff Main.storyboard

生成された .xliff

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ib="com.apple.InterfaceBuilder3" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
    <file original="Main.storyboard" datatype="x-com.apple.InterfaceBuilder3.Storyboard.XIB" tool-id="com.apple.ibtool" source-language="en">
        <header>
            <tool tool-id="com.apple.ibtool" tool-name="ibtool" tool-version="9532"></tool>
        </header>
        <body>
            <group ib:member-type="objects">
                <group ib:object-id="BYZ-38-t0r" ib:class="UIViewController">
                    <group ib:object-id="y3c-jy-aDJ" ib:class="NSObject"></group>
                    <group ib:object-id="wfy-db-euE" ib:class="NSObject"></group>
                    <group ib:object-id="8bC-Xf-vdC" ib:class="UIView">
                        <group ib:object-id="H4E-4z-9nL" ib:class="UILabel">
                            <trans-unit ib:key-path-category="string" id="H4E-4z-9nL.text" ib:key-path="text">
                                <source>Label</source>
                            </trans-unit>
                        </group>
                    </group>
                </group>
                <group ib:object-id="pQ7-8c-0OJ" ib:class="UIStoryboardEntryPointIndicator"></group>
            </group>
            <group ib:member-type="connections"></group>
        </body>
    </file>
</xliff>

ちなみに
ibtool --export-xliff Main.xliff --source-language ja --target-language ru Main.storyboard

とオプションを付けると xliff の3行目の末尾に以下のように source-language と target-language の指定が入る。

    <file original="Main.storyboard" datatype="x-com.apple.InterfaceBuilder3.Storyboard.XIB" tool-id="com.apple.ibtool" source-language="ja" target-language="ru">

storyboard 上で英語以外の言語でパーツを配置しているような場合にこのオプションを使って xliff を書き出すといいのかも知れない。


ibtool は xib や storyboard 自体がローカライズされて複数ファイル存在するケースでのローカライズを想定した機能になっているようで、翻訳したファイルと元の xib, storyboard ファイルを使って言語別の xib, storyboard 作ったり、生成済みの言語別 xib, storyboard ファイルに翻訳済みのファイルの内容を反映する事が出来るらしい。
でも今時は言語別に xib, storybpard ファイルを作らないで strings ファイルを複数作って適応させる Base Internationalization の方法が主流だと思うので ibtool での xib, storyboard ファイルの生成はあまり使わないかも。

strings ファイルの利用

翻訳した strings ファイル

/* Class = "UILabel"; text = "Label"; ObjectID = "H4E-4z-9nL"; */
"H4E-4z-9nL.text" = "ラベル";

以下のコマンドで翻訳済み strings を使って翻訳を反映した storyboard ファイルを生成する
--import-strings Main.strings --write Main_ja.storyboard Main.storyboard

xliff の利用

source タグの下に target タグを追加して翻訳する

<source>Label</source>
<target>ラベル</ラベル>>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ib="com.apple.InterfaceBuilder3" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
    <file original="Main.storyboard" datatype="x-com.apple.InterfaceBuilder3.Storyboard.XIB" tool-id="com.apple.ibtool" source-language="en">
        <header>
            <tool tool-id="com.apple.ibtool" tool-name="ibtool" tool-version="9532"></tool>
        </header>
        <body>
            <group ib:member-type="objects">
                <group ib:object-id="BYZ-38-t0r" ib:class="UIViewController">
                    <group ib:object-id="y3c-jy-aDJ" ib:class="NSObject"></group>
                    <group ib:object-id="wfy-db-euE" ib:class="NSObject"></group>
                    <group ib:object-id="8bC-Xf-vdC" ib:class="UIView">
                        <group ib:object-id="H4E-4z-9nL" ib:class="UILabel">
                            <trans-unit ib:key-path-category="string" id="H4E-4z-9nL.text" ib:key-path="text">
                                <source>Label</source>
                                <target>ラベル</target>
                            </trans-unit>
                        </group>
                    </group>
                </group>
                <group ib:object-id="h1d-p0-wP2" ib:class="UIStoryboardEntryPointIndicator"></group>
            </group>
            <group ib:member-type="connections"></group>
        </body>
    </file>
</xliff>

以下のコマンドで翻訳済み xliff を使って翻訳を反映した xib, storyboard ファイルを生成する
ibtool --import-xliff Main.xliff --write Main_ja.storyboard Main.storyboard

生成された Main_ja.storyboard を開いてみると翻訳が反映されている。

f:id:Koze:20160222034732p:plain


続き

関連