18e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/*
28e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
38e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
48e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Redistribution and use in source and binary forms, with or without
58e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * modification, are permitted provided that the following conditions
68e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * are met:
78e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
88e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 1.  Redistributions of source code must retain the above copyright
98e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     notice, this list of conditions and the following disclaimer.
108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 2.  Redistributions in binary form must reproduce the above copyright
118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     notice, this list of conditions and the following disclaimer in the
128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     documentation and/or other materials provided with the distribution.
138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     its contributors may be used to endorse or promote products derived
158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *     from this software without specific prior written permission.
168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project */
288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#import <WebKit/WebLocalizableStrings.h>
308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#import <wtf/Assertions.h>
328a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block#import <wtf/Threading.h>
338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectWebLocalizableStringsBundle WebKitLocalizableStringsBundle = { "com.apple.WebKit", 0 };
358e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectNSString *WebLocalizedString(WebLocalizableStringsBundle *stringsBundle, const char *key)
378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
388a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block    // This function is not thread-safe due at least to its unguarded use of the mainBundle static variable
398a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block    // and its use of [NSBundle localizedStringForKey:::], which is not guaranteed to be thread-safe. If
408a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block    // we decide we need to use this on background threads, we'll need to add locking here and make sure
418a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block    // it doesn't affect performance.
428a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block    ASSERT(isMainThread());
438a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block
448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    NSBundle *bundle;
458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (stringsBundle == NULL) {
468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        static NSBundle *mainBundle;
478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (mainBundle == nil) {
488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            mainBundle = [NSBundle mainBundle];
498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            ASSERT(mainBundle);
508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            CFRetain(mainBundle);
518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        }
528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        bundle = mainBundle;
538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    } else {
548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        bundle = stringsBundle->bundle;
558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (bundle == nil) {
568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            bundle = [NSBundle bundleWithIdentifier:[NSString stringWithUTF8String:stringsBundle->identifier]];
578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            ASSERT(bundle);
588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            stringsBundle->bundle = bundle;
598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        }
608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    NSString *notFound = @"localized string not found";
628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    CFStringRef keyString = CFStringCreateWithCStringNoCopy(NULL, key, kCFStringEncodingUTF8, kCFAllocatorNull);
638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    NSString *result = [bundle localizedStringForKey:(NSString *)keyString value:notFound table:nil];
648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    CFRelease(keyString);
658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    ASSERT_WITH_MESSAGE(result != notFound, "could not find localizable string %s in bundle", key);
668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return result;
678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
68